-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
130 lines (109 loc) · 4.15 KB
/
run.py
File metadata and controls
130 lines (109 loc) · 4.15 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
#!/usr/bin/env python
"""
Main entry point for the Pairwise Alpha Trading System.
This script provides a command-line interface to run different components of the system.
"""
import argparse
import os
import sys
from strategy import Strategy, DEFAULT_CONFIG
from ensemble_strategy import EnsembleStrategy
from real_time_trader import RealTimeTrader
import subprocess
import time
def run_dashboard():
"""Run the Streamlit dashboard"""
print("Starting dashboard...")
subprocess.Popen(["streamlit", "run", "dashboard.py"])
def run_trader(interval=5):
"""Run the real-time trader"""
print(f"Starting real-time trader with {interval} minute updates...")
trader = RealTimeTrader()
trader.start(update_interval_minutes=interval)
try:
while True:
time.sleep(60)
print(f"Trader running... Last update: {trader.last_update}")
except KeyboardInterrupt:
trader.stop()
print("Trading stopped by user")
def run_backtest():
"""Run backtest on historical data"""
print("Running backtest...")
# Standard strategy backtest
print("\n=== Standard Strategy Backtest ===")
strategy = Strategy()
os.system("python strategy.py")
# Ensemble strategy backtest
print("\n=== Ensemble Strategy Backtest ===")
ensemble = EnsembleStrategy()
# Get data and train model
from fetch_data import DataFetcher
data_fetcher = DataFetcher()
target_data = data_fetcher.get_recent_data(DEFAULT_CONFIG["target"]["symbol"], DEFAULT_CONFIG["target"]["timeframe"])
# Get anchor data
anchor_data = None
if target_data is not None:
anchor_data = {}
for anchor in DEFAULT_CONFIG["anchors"]:
symbol = anchor["symbol"]
timeframe = anchor["timeframe"]
data = data_fetcher.get_recent_data(symbol, timeframe)
if data is not None:
anchor_data[symbol] = data
if target_data is not None and anchor_data:
# Train and test ensemble strategy
ensemble.train_model((target_data, anchor_data))
signals = ensemble.generate_signals(target_data, anchor_data)
print(f"Generated {len(signals)} signals")
print(signals['signal'].value_counts())
else:
print("Insufficient data for ensemble strategy backtest")
def train_model():
"""Train the ensemble model"""
print("Training ensemble model...")
ensemble = EnsembleStrategy()
# Get data
from fetch_data import DataFetcher
data_fetcher = DataFetcher()
target_data = data_fetcher.get_recent_data(DEFAULT_CONFIG["target"]["symbol"], DEFAULT_CONFIG["target"]["timeframe"])
# Get anchor data
anchor_data = None
if target_data is not None:
anchor_data = {}
for anchor in DEFAULT_CONFIG["anchors"]:
symbol = anchor["symbol"]
timeframe = anchor["timeframe"]
data = data_fetcher.get_recent_data(symbol, timeframe)
if data is not None:
anchor_data[symbol] = data
if target_data is not None and anchor_data:
# Train model
ensemble.train_model((target_data, anchor_data))
print("Model training complete")
else:
print("Insufficient data for model training")
def main():
"""Main entry point"""
parser = argparse.ArgumentParser(description="Pairwise Alpha Trading System")
parser.add_argument("command", choices=["dashboard", "trader", "backtest", "train"],
help="Command to run")
parser.add_argument("--interval", type=int, default=5,
help="Update interval in minutes for trader (default: 5)")
args = parser.parse_args()
# Create necessary directories
os.makedirs("data", exist_ok=True)
os.makedirs("models", exist_ok=True)
os.makedirs("results", exist_ok=True)
if args.command == "dashboard":
run_dashboard()
elif args.command == "trader":
run_trader(args.interval)
elif args.command == "backtest":
run_backtest()
elif args.command == "train":
train_model()
else:
parser.print_help()
if __name__ == "__main__":
main()