-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_runner.py
More file actions
599 lines (493 loc) · 20.6 KB
/
Copy pathexample_runner.py
File metadata and controls
599 lines (493 loc) · 20.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#!/usr/bin/env python
"""
Example runner: Complete trading pipeline
AIEngine → Multi-timeframe → News Sentiment → Claude AI → Backtest → Execution
This script demonstrates the full workflow of the trading system:
1. Fetch market data for multiple symbols
2. Run multi-timeframe analysis (M1/M5/H1)
3. Get news sentiment analysis
4. Use AIEngine to generate unified signals
5. Validate signals with Claude AI
6. Run backtest to validate strategy
7. Execute trades (demo mode)
"""
from __future__ import annotations
import argparse
import json
import logging
import os
from pathlib import Path
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from backtest import BacktestMetrics
try:
import MetaTrader5 as mt5
except ImportError:
mt5 = None # For non-Windows systems
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
handlers=[
logging.FileHandler("trading_pipeline.log"),
logging.StreamHandler(),
],
)
logger = logging.getLogger(__name__)
class TradingPipelineRunner:
"""Complete trading pipeline orchestrator"""
def __init__(
self,
symbols: list[str],
config_file: str = "config.json",
api_keys: dict[str, str] | None = None,
):
"""
Initialize pipeline runner
Args:
symbols: List of trading symbols (e.g., ["EURUSD", "GBPUSD"])
config_file: Path to config file
api_keys: Dict with 'claude_api_key' and 'newsapi_key'
"""
self.symbols = symbols
self.config = self._load_config(config_file)
self.api_keys = api_keys or {}
self.backtest_results: dict[str, BacktestMetrics] = {}
logger.info(f"Pipeline initialized with symbols: {symbols}")
# Initialize MT5 if available
self._init_mt5()
def _load_config(self, config_file: str) -> dict[str, Any]:
"""Load configuration from file"""
if Path(config_file).exists():
with open(config_file) as f:
return json.load(f)
return {
"timeframes": ["M1", "M5", "H1"],
"weights": {"M1": 0.4, "M5": 0.35, "H1": 0.25},
"backtest_days": 5,
"balance": 1000.0,
"risk_per_trade": 0.02,
"demo_mode": True,
}
def _init_mt5(self) -> bool:
"""Initialize MetaTrader5 connection"""
if mt5 is None:
logger.warning("MetaTrader5 not available (non-Windows system)")
return False
if not mt5.initialize():
logger.error("Failed to initialize MT5")
return False
logger.info(f"MT5 initialized. Account: {mt5.account_info()}")
return True
def step_1_fetch_market_data(self) -> dict[str, dict]:
"""
Step 1: Fetch current market data for all symbols
Returns dict: {symbol -> {timeframe -> OHLCV data}}
"""
logger.info("=" * 60)
logger.info("STEP 1: Fetching Market Data")
logger.info("=" * 60)
market_data = {}
for symbol in self.symbols:
logger.info(f"Fetching data for {symbol}")
symbol_data = {}
if mt5 is None:
# Mock data for demo
logger.info(f" [DEMO] Using mock data for {symbol}")
symbol_data["M1"] = self._get_mock_ohlcv(symbol, 100)
symbol_data["M5"] = self._get_mock_ohlcv(symbol, 100)
symbol_data["H1"] = self._get_mock_ohlcv(symbol, 100)
else:
# Real MT5 data
try:
for tf in ["M1", "M5", "H1"]:
tf_val = getattr(mt5, f"TIMEFRAME_{tf}")
rates = mt5.copy_rates_symbol(symbol, tf_val, 0, 100)
symbol_data[tf] = self._convert_mt5_rates(rates)
except Exception as e:
logger.error(f"Error fetching {symbol} {tf}: {e}")
symbol_data[tf] = self._get_mock_ohlcv(symbol, 100)
market_data[symbol] = symbol_data
logger.info(f" ✓ Fetched {symbol}")
return market_data
def step_2_multi_timeframe_analysis(
self, market_data: dict[str, dict]
) -> dict[str, Any]:
"""
Step 2: Run multi-timeframe analysis (M1/M5/H1)
Returns dict: {symbol -> analysis result}
"""
logger.info("=" * 60)
logger.info("STEP 2: Multi-Timeframe Analysis (M1/M5/H1)")
logger.info("=" * 60)
results = {}
for symbol in self.symbols:
logger.info(f"Analyzing {symbol}")
from multi_timeframe import MultiTimeframeAnalyzer
analyzer = MultiTimeframeAnalyzer(symbol=symbol)
try:
result = analyzer.analyze(market_data[symbol])
results[symbol] = result
logger.info(
f" Weighted Signal: {result.weighted_signal} "
f"(Confirmation: {result.confirmation_signal})"
)
for tf, sig in result.signals.items():
logger.info(
f" {tf}: {sig.signal} (confidence: {sig.confidence:.1f})"
)
except Exception as e:
logger.error(f"Error analyzing {symbol}: {e}")
results[symbol] = None
return results
def step_3_news_sentiment(self) -> dict[str, Any]:
"""
Step 3: Fetch and analyze news sentiment
Returns dict: {symbol -> sentiment analysis}
"""
logger.info("=" * 60)
logger.info("STEP 3: News Sentiment Analysis")
logger.info("=" * 60)
sentiment_results = {}
newsapi_key = self.api_keys.get("newsapi_key")
try:
# ForexFactory economic calendar
logger.info("Fetching ForexFactory economic calendar...")
from news_sentiment import ForexFactoryScraper, NewsAPIClient
scraper = ForexFactoryScraper()
calendar_events = scraper.fetch_calendar()
logger.info(f" ✓ Found {len(calendar_events)} events")
# NewsAPI news
if newsapi_key:
logger.info("Fetching NewsAPI articles...")
news_client = NewsAPIClient(api_key=newsapi_key)
for symbol in self.symbols:
try:
news = news_client.fetch_forex_news(
currencies=[symbol[:3], symbol[3:6]], limit=10
)
sentiment_results[symbol] = news
logger.info(f" ✓ {symbol}: {len(news)} articles")
except Exception as e:
logger.warning(f"Error fetching news for {symbol}: {e}")
sentiment_results[symbol] = []
else:
logger.warning("NewsAPI key not provided, skipping news sentiment")
except Exception as e:
logger.error(f"Error in sentiment analysis: {e}")
return sentiment_results
def step_4_ai_engine_unified_signals(self, mt_results: dict) -> dict[str, Any]:
"""
Step 4: Use AIEngine to generate unified signals
Returns dict: {symbol -> unified signal}
"""
logger.info("=" * 60)
logger.info("STEP 4: AI Engine Unified Signals")
logger.info("=" * 60)
from ai_engine import AIEngine, EngineConfig
engine_config = EngineConfig(
symbols=self.symbols,
timeframe="M1",
weights=self.config.get("weights", {"technical": 0.85, "sentiment": 0.15}),
min_confidence_threshold=60,
update_interval_seconds=60,
)
engine = AIEngine(config=engine_config)
unified_signals = {}
for symbol in self.symbols:
logger.info(f"Generating unified signal for {symbol}")
# Analyze symbol
signal = engine.analyze_all()
if symbol in signal:
unified_signals[symbol] = signal[symbol]
logger.info(f" Signal: {signal[symbol].primary_signal}")
logger.info(f" Confidence: {signal[symbol].confidence:.1f}")
else:
logger.warning(f"No signal generated for {symbol}")
return unified_signals
def step_5_claude_validation(
self, market_data: dict[str, dict], unified_signals: dict[str, Any]
) -> dict[str, Any]:
"""
Step 5: Validate signals with Claude AI
Returns dict: {symbol -> claude validated signal}
"""
logger.info("=" * 60)
logger.info("STEP 5: Claude AI Validation")
logger.info("=" * 60)
claude_key = self.api_keys.get("claude_api_key")
if not claude_key:
logger.warning("Claude API key not provided, skipping Claude validation")
return unified_signals
try:
from claude_ai import ClaudeAIClient, ClaudeAIIntegration
claude_client = ClaudeAIClient(api_key=claude_key)
claude_integration = ClaudeAIIntegration(client=claude_client)
validated_signals = {}
for symbol in self.symbols:
logger.info(f"Validating {symbol} with Claude")
if symbol not in unified_signals:
logger.warning(f"No signal to validate for {symbol}")
continue
engine_signal = unified_signals[symbol]
try:
validated = claude_integration.validate_signal(
symbol=symbol,
engine_signal=engine_signal.primary_signal,
engine_confidence=engine_signal.confidence,
market_data={
"close": (
market_data[symbol]["M1"]["close"].tail(10).tolist()
if "M1" in market_data[symbol]
else []
),
},
)
validated_signals[symbol] = validated
logger.info(
f" Claude Signal: {validated.signal} (conf: {validated.confidence})"
)
except Exception as e:
logger.error(f"Error validating {symbol}: {e}")
validated_signals[symbol] = engine_signal
return validated_signals
except Exception as e:
logger.error(f"Error initializing Claude: {e}")
return unified_signals
def step_6_backtesting(
self, market_data: dict[str, dict], validated_signals: dict
) -> dict[str, BacktestMetrics]:
"""
Step 6: Backtest signals on historical data
Returns dict: {symbol -> backtest metrics}
"""
logger.info("=" * 60)
logger.info("STEP 6: Backtesting")
logger.info("=" * 60)
backtest_results = {}
balance = self.config.get("balance", 1000.0)
risk_pct = self.config.get("risk_per_trade", 0.02)
for symbol in self.symbols:
logger.info(f"Backtesting {symbol}")
if symbol not in market_data:
logger.warning(f"No market data for {symbol}")
continue
try:
from backtest import BacktestEngine
engine = BacktestEngine(symbol=symbol)
# Create signal function from validated signal
if symbol in validated_signals:
signal_val = validated_signals[symbol].signal
def signal_func(ohlcv):
return signal_val
else:
def signal_func(ohlcv):
return "HOLD"
# Run backtest
metrics = engine.backtest(
signal_func=signal_func,
historical_data=market_data[symbol].get("M1"),
balance=balance,
risk_per_trade=risk_pct,
)
backtest_results[symbol] = metrics
logger.info(" Backtest Results:")
logger.info(f" Win Rate: {metrics.win_rate:.1f}%")
logger.info(f" Sharpe Ratio: {metrics.sharpe_ratio:.2f}")
logger.info(f" Max Drawdown: {metrics.max_drawdown:.1f}%")
logger.info(f" Total Return: {metrics.total_return:.2f}%")
except Exception as e:
logger.error(f"Error backtesting {symbol}: {e}")
self.backtest_results = backtest_results
return backtest_results
def step_7_execution_simulation(self, validated_signals: dict):
"""
Step 7: Simulate trade execution (demo mode)
"""
logger.info("=" * 60)
logger.info("STEP 7: Trade Execution (Demo Mode)")
logger.info("=" * 60)
balance = self.config.get("balance", 1000.0)
risk_pct = self.config.get("risk_per_trade", 0.02)
for symbol, signal in validated_signals.items():
if signal.signal == "HOLD":
logger.info(f"{symbol}: HOLD (no action)")
continue
try:
# Calculate position size
from calculator import position_size
lot_size = position_size(
balance=balance,
risk_pct=risk_pct,
sl_points=100,
point_value=10,
)
logger.info(f"{symbol}: {signal.signal}")
logger.info(f" Confidence: {signal.confidence}/100")
logger.info(f" Risk Level: {signal.risk}")
logger.info(f" Lot Size: {lot_size:.2f}")
logger.info(f" Reason: {signal.reason}")
if not self.config.get("demo_mode", True):
logger.info(
f" [EXECUTING] Would execute {signal.signal} on {symbol}"
)
# Would execute here with actual MT5 order
else:
logger.info(f" [DEMO] Would execute {signal.signal} on {symbol}")
except Exception as e:
logger.error(f"Error executing {symbol}: {e}")
def run_full_pipeline(self) -> dict[str, Any]:
"""Execute complete pipeline"""
logger.info("\n" + "=" * 60)
logger.info("FULL TRADING PIPELINE")
logger.info("=" * 60 + "\n")
try:
# Step 1: Fetch market data
market_data = self.step_1_fetch_market_data()
# Step 2: Multi-timeframe analysis
mt_results = self.step_2_multi_timeframe_analysis(market_data)
# Step 3: News sentiment
sentiment_results = self.step_3_news_sentiment()
# Step 4: AI Engine unified signals
unified_signals = self.step_4_ai_engine_unified_signals(mt_results)
# Step 5: Claude validation
validated_signals = self.step_5_claude_validation(
market_data, unified_signals
)
# Step 6: Backtesting
backtest_results = self.step_6_backtesting(market_data, validated_signals)
# Step 7: Execution simulation
self.step_7_execution_simulation(validated_signals)
# Summary
self._print_summary(backtest_results)
return {
"market_data": market_data,
"multi_timeframe_results": mt_results,
"sentiment_results": sentiment_results,
"unified_signals": unified_signals,
"validated_signals": validated_signals,
"backtest_results": backtest_results,
}
except Exception as e:
logger.error(f"Pipeline failed: {e}", exc_info=True)
raise
def _print_summary(self, backtest_results: dict[str, BacktestMetrics]):
"""Print pipeline summary"""
logger.info("\n" + "=" * 60)
logger.info("PIPELINE SUMMARY")
logger.info("=" * 60)
for symbol, metrics in backtest_results.items():
if metrics:
logger.info(f"{symbol}:")
logger.info(f" Trades: {metrics.total_trades}")
logger.info(f" Win Rate: {metrics.win_rate:.1f}%")
logger.info(f" Sharpe: {metrics.sharpe_ratio:.2f}")
logger.info(f" Max DD: {metrics.max_drawdown:.1f}%")
logger.info(f" Return: {metrics.total_return:.2f}%")
@staticmethod
def _get_mock_ohlcv(symbol: str, periods: int):
"""Generate mock OHLCV data for demo"""
import pandas as pd
import numpy as np
rng = np.random.default_rng(hash(symbol) % 1000)
base_price = 1.1
close = base_price + np.cumsum(rng.normal(0, 0.001, periods))
open_ = close + rng.normal(0, 0.001, periods)
high = np.maximum(open_, close) + np.abs(rng.normal(0, 0.0005, periods))
low = np.minimum(open_, close) - np.abs(rng.normal(0, 0.0005, periods))
volume = rng.integers(100, 1000, periods)
return pd.DataFrame(
{
"time": pd.date_range("2025-01-01", periods=periods, freq="1min"),
"open": open_,
"high": high,
"low": low,
"close": close,
"volume": volume,
}
)
@staticmethod
def _convert_mt5_rates(rates):
"""Convert MT5 rates to DataFrame"""
import pandas as pd
df = pd.DataFrame(rates)
df["time"] = pd.to_datetime(df["time"], unit="s")
return df[["time", "open", "high", "low", "close", "tick_volume"]]
def _run_ibkr_paper_demo(symbols: list[str]) -> int:
"""Demonstrate the safe IBKR paper adapter without touching MT5/Claude.
Works even when ``ib_insync`` is not installed and TWS / IB Gateway
is not running - the adapter degrades to a typed disconnected
response and a dry-run report.
"""
from dataclasses import asdict
from brokers.adapter_models import ExecutionDecision
from brokers.paper_factory import get_paper_broker_adapter
logger.info("=" * 60)
logger.info("IBKR Paper Adapter demo (dry-run, no live orders)")
logger.info("=" * 60)
adapter = get_paper_broker_adapter("ibkr-paper")
health = adapter.connect()
logger.info("Health: %s", asdict(health))
snapshot = adapter.account_snapshot()
logger.info("Account snapshot: %s", asdict(snapshot))
for idx, symbol in enumerate(symbols, start=1):
decision = ExecutionDecision(
decision_id=f"demo-{idx:03d}",
signal_id=f"sig-{idx:03d}",
symbol=symbol,
direction="BUY",
confidence=72.0,
dry_run=True,
reason="example_runner demo",
metadata={"source": "example_runner", "broker": "ibkr-paper"},
)
report = adapter.submit_dry_run_report(decision)
logger.info("Dry-run report for %s: %s", symbol, asdict(report))
adapter.disconnect()
logger.info("\nIBKR paper demo finished (dry-run only)")
return 0
def main():
"""Main entry point"""
parser = argparse.ArgumentParser(
description="Complete Trading Pipeline: MTF→Sentiment→AIEngine→Claude→Backtest"
)
parser.add_argument(
"--symbols",
nargs="+",
default=["EURUSD", "GBPUSD"],
help="Trading symbols",
)
parser.add_argument(
"--broker",
default="mt5-demo",
choices=["mt5-demo", "ibkr-paper"],
help="Broker adapter for the optional broker demo path",
)
parser.add_argument("--config", default="config.json", help="Config file path")
parser.add_argument("--claude-key", help="Claude API key")
parser.add_argument("--newsapi-key", help="NewsAPI key")
parser.add_argument("--demo", action="store_true", default=True, help="Demo mode")
args = parser.parse_args()
if args.broker == "ibkr-paper":
return _run_ibkr_paper_demo(args.symbols)
api_keys = {}
if args.claude_key:
api_keys["claude_api_key"] = args.claude_key
else:
api_keys["claude_api_key"] = os.getenv("CLAUDE_API_KEY")
if args.newsapi_key:
api_keys["newsapi_key"] = args.newsapi_key
else:
api_keys["newsapi_key"] = os.getenv("NEWSAPI_KEY")
runner = TradingPipelineRunner(
symbols=args.symbols, config_file=args.config, api_keys=api_keys
)
try:
runner.run_full_pipeline()
logger.info("\n✓ Pipeline completed successfully!")
return 0
except Exception as e:
logger.error(f"\n✗ Pipeline failed: {e}")
return 1
if __name__ == "__main__":
exit(main())