-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcomprehensive_example.py
More file actions
304 lines (255 loc) · 11.6 KB
/
comprehensive_example.py
File metadata and controls
304 lines (255 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""Comprehensive example demonstrating v3.1.0 features."""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from forexsmartbot.strategies import get_strategy
from forexsmartbot.optimization import (
GeneticOptimizer,
HyperparameterOptimizer,
WalkForwardAnalyzer,
MonteCarloSimulator,
ParameterSensitivityAnalyzer
)
from forexsmartbot.adapters.data import YFinanceProvider
from forexsmartbot.services.backtest import BacktestService
from forexsmartbot.services.enhanced_backtest import EnhancedBacktestService
from forexsmartbot.monitoring import StrategyMonitor, PerformanceTracker, HealthChecker
from forexsmartbot.marketplace import StrategyMarketplace, StrategyListing
from forexsmartbot.core.risk_engine import RiskConfig
from datetime import datetime
def main():
"""Comprehensive example of v3.1.0 features."""
print("=" * 70)
print("ForexSmartBot v3.1.0 - Comprehensive Feature Demonstration")
print("=" * 70)
# Initialize data provider
data_provider = YFinanceProvider()
# ========================================================================
# 1. ML Strategy Usage
# ========================================================================
print("\n1. ML Strategy Usage")
print("-" * 70)
try:
# Try to create an ML strategy
ml_strategy = get_strategy('Ensemble_ML_Strategy',
lookback_period=50,
n_estimators=50,
max_depth=5
)
print(f"✓ Created ML strategy: {ml_strategy.name}")
except Exception as e:
print(f"✗ ML strategy not available: {e}")
ml_strategy = get_strategy('SMA_Crossover')
print(f"✓ Using fallback strategy: {ml_strategy.name}")
# ========================================================================
# 2. Strategy Optimization
# ========================================================================
print("\n2. Strategy Optimization")
print("-" * 70)
def fitness_function(params):
"""Fitness function for optimization."""
try:
strategy = get_strategy('SMA_Crossover', **params)
backtest_service = BacktestService(data_provider)
results = backtest_service.run_backtest(
strategy=strategy,
symbol='EURUSD=X',
start_date='2023-01-01',
end_date='2023-06-30',
initial_balance=10000.0
)
if 'error' in results:
return -100.0
metrics = results.get('metrics', {})
return metrics.get('sharpe_ratio', 0.0)
except:
return -100.0
# Genetic Algorithm Optimization
print("Running Genetic Algorithm optimization...")
param_bounds = {
'fast_period': (10, 30),
'slow_period': (40, 80)
}
ga_optimizer = GeneticOptimizer(param_bounds, population_size=10, generations=5)
best_params, best_fitness = ga_optimizer.optimize(fitness_function, verbose=False)
print(f"✓ GA Best parameters: {best_params}")
print(f"✓ GA Best fitness: {best_fitness:.4f}")
# ========================================================================
# 3. Parameter Sensitivity Analysis
# ========================================================================
print("\n3. Parameter Sensitivity Analysis")
print("-" * 70)
def strategy_factory(params):
return get_strategy('SMA_Crossover', **params)
def performance_function(strategy):
backtest_service = BacktestService(data_provider)
results = backtest_service.run_backtest(
strategy=strategy,
symbol='EURUSD=X',
start_date='2023-01-01',
end_date='2023-06-30',
initial_balance=10000.0
)
if 'error' in results:
return 0.0
metrics = results.get('metrics', {})
return metrics.get('total_return', 0.0)
sensitivity_analyzer = ParameterSensitivityAnalyzer(n_points=5)
base_params = {'fast_period': 20, 'slow_period': 50}
param_ranges = {
'fast_period': (15, 25),
'slow_period': (45, 55)
}
sensitivity_results = sensitivity_analyzer.analyze(
strategy_factory, base_params, param_ranges, performance_function
)
print("Sensitivity Analysis Results:")
for param_name, result in sensitivity_results.items():
print(f" {param_name}:")
print(f" Sensitivity Score: {result.sensitivity_score:.4f}")
print(f" Optimal Value: {result.optimal_value:.2f}")
print(f" Impact Range: [{result.impact_range[0]:.4f}, {result.impact_range[1]:.4f}]")
# ========================================================================
# 4. Walk-Forward Analysis
# ========================================================================
print("\n4. Walk-Forward Analysis")
print("-" * 70)
# Get data for walk-forward
df = data_provider.get_data('EURUSD=X', '2023-01-01', '2023-12-31', '1h')
if not df.empty:
walk_forward = WalkForwardAnalyzer(train_period=60, test_period=20, step_size=10)
def optimize_function(train_data, initial_params):
# Simple optimization: return base params
return initial_params
def strategy_factory_wf(params):
return get_strategy('SMA_Crossover', **params)
wf_results = walk_forward.analyze(
df, strategy_factory_wf, optimize_function, base_params
)
if wf_results:
print(f"✓ Walk-forward analysis completed")
print(f" Periods analyzed: {wf_results.get('num_periods', 0)}")
print(f" Average Sharpe: {wf_results.get('avg_sharpe', 0):.4f}")
print(f" Win Rate: {wf_results.get('win_rate', 0):.2%}")
# ========================================================================
# 5. Monte Carlo Simulation
# ========================================================================
print("\n5. Monte Carlo Simulation")
print("-" * 70)
if not df.empty:
returns = df['Close'].pct_change().dropna()
mc_simulator = MonteCarloSimulator(n_simulations=100, confidence_level=0.95)
mc_results = mc_simulator.simulate(returns, initial_balance=10000.0, n_periods=252)
print(f"✓ Monte Carlo simulation completed")
print(f" Mean Return: {mc_results['mean_return']:.4f}")
print(f" VaR (95%): {mc_results['var']:.4f}")
print(f" CVaR (95%): {mc_results['cvar']:.4f}")
print(f" Probability of Profit: {mc_results['probability_of_profit']:.2%}")
# ========================================================================
# 6. Strategy Monitoring
# ========================================================================
print("\n6. Strategy Monitoring")
print("-" * 70)
monitor = StrategyMonitor()
monitor.register_strategy("DemoStrategy")
# Simulate some signals
for i in range(5):
monitor.record_signal("DemoStrategy", execution_time=0.05 + i * 0.01)
health = monitor.get_health("DemoStrategy")
print(f"✓ Strategy monitoring active")
print(f" Status: {health.status.value}")
print(f" Signal Count: {health.signal_count}")
print(f" Avg Execution Time: {health.execution_time_avg:.3f}s")
# Health check
health_checker = HealthChecker(monitor)
check_result = health_checker.check("DemoStrategy")
print(f" Health Check: {check_result['status']}")
# ========================================================================
# 7. Performance Tracking
# ========================================================================
print("\n7. Performance Tracking")
print("-" * 70)
tracker = PerformanceTracker()
# Simulate some trades
for i in range(10):
profit = (i % 3 - 1) * 50.0 # Mix of wins and losses
tracker.record_trade("DemoStrategy", {
'profit': profit,
'entry_price': 1.1000,
'exit_price': 1.1000 + profit / 10000,
'entry_time': datetime.now(),
'exit_time': datetime.now()
})
tracker.record_equity("DemoStrategy", 10000.0 + i * 10.0)
metrics = tracker.calculate_metrics("DemoStrategy")
if metrics:
print(f"✓ Performance tracking active")
print(f" Total Return: {metrics.total_return:.2%}")
print(f" Sharpe Ratio: {metrics.sharpe_ratio:.4f}")
print(f" Win Rate: {metrics.win_rate:.2%}")
print(f" Total Trades: {metrics.total_trades}")
# ========================================================================
# 8. Strategy Marketplace
# ========================================================================
print("\n8. Strategy Marketplace")
print("-" * 70)
marketplace = StrategyMarketplace(storage_path="marketplace_demo")
# Create a listing
listing = StrategyListing(
strategy_id="demo_strategy_001",
name="Demo SMA Crossover",
description="A simple SMA crossover strategy for demonstration",
author="Demo User",
version="1.0.0",
category="Trend Following",
tags=["SMA", "Crossover", "Trend"],
created_at=datetime.now(),
updated_at=datetime.now()
)
marketplace.add_listing(listing)
print(f"✓ Strategy listed in marketplace")
# Search listings
results = marketplace.search_listings(query="SMA", min_rating=0.0)
print(f"✓ Found {len(results)} strategies matching 'SMA'")
# ========================================================================
# 9. Enhanced Backtesting
# ========================================================================
print("\n9. Enhanced Backtesting")
print("-" * 70)
enhanced_service = EnhancedBacktestService(data_provider, use_parallel=False)
strategy = get_strategy('SMA_Crossover', fast_period=20, slow_period=50)
results = enhanced_service.run_backtest(
strategy=strategy,
symbol='EURUSD=X',
start_date='2023-01-01',
end_date='2023-06-30',
initial_balance=10000.0,
enable_logging=False
)
if 'error' not in results:
print(f"✓ Enhanced backtest completed")
print(f" Total Return: {results.get('total_return', 0):.2%}")
print(f" Total Trades: {results.get('total_trades', 0)}")
print(f" Error Count: {results.get('error_count', 0)}")
else:
print(f"✗ Backtest error: {results.get('error')}")
# ========================================================================
# Summary
# ========================================================================
print("\n" + "=" * 70)
print("Summary")
print("=" * 70)
print("✓ All v3.1.0 features demonstrated successfully!")
print("\nFeatures tested:")
print(" - ML Strategies")
print(" - Strategy Optimization (GA, Optuna)")
print(" - Parameter Sensitivity Analysis")
print(" - Walk-Forward Analysis")
print(" - Monte Carlo Simulation")
print(" - Strategy Monitoring")
print(" - Performance Tracking")
print(" - Strategy Marketplace")
print(" - Enhanced Backtesting")
print("\n" + "=" * 70)
if __name__ == "__main__":
main()