-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_strategy.py
More file actions
70 lines (60 loc) · 2.12 KB
/
Copy pathtest_strategy.py
File metadata and controls
70 lines (60 loc) · 2.12 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
from agents.data_agent import DataAgent
from agents.strategy_agent import StrategyAgent
from agents.backtest_agent import BacktestAgent
from agents.performance_agent import PerformanceAgent
def test_momentum_strategy():
# Initialize agents
data_agent = DataAgent()
strategy_agent = StrategyAgent()
backtest_agent = BacktestAgent()
performance_agent = PerformanceAgent()
# Test parameters
symbol = "AAPL" # Let's start with Apple stock
start_date = "2022-01-01"
end_date = "2024-01-01"
# Strategy specification
strategy_spec = {
'type': 'momentum',
'params': {
'lookback': 30,
'zscore_threshold': 1.0
},
'position_sizing': {
'method': 'volatility_targeting',
'params': {
'target_vol': 0.20,
'lookback': 60
}
}
}
try:
# 1. Fetch data
print("Fetching data...")
data = data_agent.fetch_data(symbol, start_date, end_date)
# 2. Generate strategy code
print("Generating strategy code...")
strategy_code = strategy_agent.generate_complete_strategy(strategy_spec)
# 3. Run backtest
print("Running backtest...")
backtest_results = backtest_agent.run_backtest(
{symbol: data},
{**strategy_spec, **strategy_code}
)
# 4. Analyze performance
if backtest_results['status'] == 'success':
print("Analyzing performance...")
performance = performance_agent.analyze_performance(
backtest_results['equity_curve'],
backtest_results['trades']
)
# Print results
print("\nPerformance Metrics:")
print(performance['metrics'])
print("\nTrade Statistics:")
print(performance['trade_stats'])
else:
print(f"Backtest failed: {backtest_results['message']}")
except Exception as e:
print(f"Error: {str(e)}")
if __name__ == "__main__":
test_momentum_strategy()