-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_backtester.py
More file actions
211 lines (165 loc) · 7.17 KB
/
Copy pathtest_backtester.py
File metadata and controls
211 lines (165 loc) · 7.17 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
"""
Backtester Test Script
Demonstrates how to use the StrategyBacktester class to run backtests
on historical data and analyze the results.
"""
import logging
import os
from datetime import datetime, timedelta
from src.backtesting.backtester import StrategyBacktester
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger("backtest_demo")
def run_demo_backtest():
"""Run a demonstration backtest on a recent time period."""
# Define backtest parameters
end_date = datetime.now() - timedelta(days=1) # Yesterday
start_date = end_date - timedelta(days=90) # 90 days before end date
assets = ["BTC"] # Start with just Bitcoin
interval = "1d" # Daily data
prediction_timeframe = "24h" # 24-hour predictions
logger.info(
f"Running backtest from {start_date.date()} to {end_date.date()} for {assets}"
)
# Initialize backtester
backtester = StrategyBacktester(
start_date=start_date,
end_date=end_date,
assets=assets,
interval=interval,
prediction_timeframe=prediction_timeframe,
)
# Run backtest
results = backtester.run_backtest(
step_size=timedelta(days=7), # Generate predictions weekly
lookback_window=timedelta(days=60), # Use 60 days of data for each analysis
)
# Generate report and plots
output_dir = os.path.join(os.getcwd(), "backtest_results")
report_file = backtester.generate_report(output_dir)
plot_files = backtester.plot_results(output_dir)
# Print summary
print("\n" + "=" * 50)
print("BACKTEST SUMMARY")
print("=" * 50)
print(f"Period: {start_date.date()} to {end_date.date()}")
print(f"Assets: {', '.join(assets)}")
print(f"Interval: {interval}")
print(f"Prediction Timeframe: {prediction_timeframe}")
print(f"Total Predictions: {results['total_predictions']}")
print(f"Correct Predictions: {results['correct_predictions']}")
print(f"Accuracy: {results['accuracy']:.2%}")
print(f"Overall Profit/Loss: {results['profit_loss']:.2f}%")
print("\nTop Performing Patterns:")
# Sort patterns by effectiveness
pattern_performance = results.get("pattern_performance", {})
sorted_patterns = []
for pattern, stats in pattern_performance.items():
if stats["total"] >= 5: # Only include patterns with sufficient samples
effectiveness = stats["hits"] / stats["total"] if stats["total"] > 0 else 0
sorted_patterns.append((pattern, effectiveness, stats["total"]))
sorted_patterns.sort(key=lambda x: x[1], reverse=True)
for pattern, effectiveness, total in sorted_patterns[:5]:
print(f" - {pattern}: {effectiveness:.2%} hit rate (n={total})")
print("\nReport and plots saved to:")
print(f" - Report: {report_file}")
for plot in plot_files:
print(f" - Plot: {plot}")
return results, report_file, plot_files
def run_comparative_backtest():
"""Run backtests on multiple assets and compare performance."""
# Define backtest parameters
end_date = datetime.now() - timedelta(days=1) # Yesterday
start_date = end_date - timedelta(days=180) # 6 months before end date
assets = ["BTC", "ETH", "SOL"] # Test multiple assets
interval = "1d" # Daily data
prediction_timeframe = "24h" # 24-hour predictions
logger.info(
f"Running comparative backtest from {start_date.date()} to {end_date.date()} for {assets}"
)
# Initialize backtester
backtester = StrategyBacktester(
start_date=start_date,
end_date=end_date,
assets=assets,
interval=interval,
prediction_timeframe=prediction_timeframe,
)
# Run backtest
results = backtester.run_backtest(
step_size=timedelta(days=3), # Generate predictions every 3 days
lookback_window=timedelta(days=90), # Use 90 days of data for each analysis
)
# Generate report and plots
output_dir = os.path.join(os.getcwd(), "comparative_backtest_results")
report_file = backtester.generate_report(output_dir)
plot_files = backtester.plot_results(output_dir)
# Print summary
print("\n" + "=" * 50)
print("COMPARATIVE BACKTEST SUMMARY")
print("=" * 50)
print(f"Period: {start_date.date()} to {end_date.date()}")
print(f"Assets: {', '.join(assets)}")
# Compare asset performance
print("\nAsset Performance:")
for asset, asset_results in results["asset_results"].items():
print(
f" - {asset}: {asset_results['accuracy']:.2%} accuracy, {asset_results['profit_loss']:.2f}% P/L"
)
return results, report_file, plot_files
def analyze_pattern_effectiveness(results):
"""Analyze pattern effectiveness across different market regimes."""
# Extract pattern performance data
pattern_performance = results.get("pattern_performance", {})
# Group patterns by type
pattern_types = {"Divergence": [], "Volume": [], "Breakout": [], "Other": []}
for pattern, stats in pattern_performance.items():
if stats["total"] >= 3: # Only include patterns with sufficient samples
effectiveness = stats["hits"] / stats["total"] if stats["total"] > 0 else 0
if "Divergence" in pattern:
pattern_types["Divergence"].append(
(pattern, effectiveness, stats["total"])
)
elif "Volume" in pattern:
pattern_types["Volume"].append((pattern, effectiveness, stats["total"]))
elif "Breakout" in pattern or "Breakdown" in pattern:
pattern_types["Breakout"].append(
(pattern, effectiveness, stats["total"])
)
else:
pattern_types["Other"].append((pattern, effectiveness, stats["total"]))
# Print analysis
print("\n" + "=" * 50)
print("PATTERN EFFECTIVENESS ANALYSIS")
print("=" * 50)
for category, patterns in pattern_types.items():
if patterns:
patterns.sort(key=lambda x: x[1], reverse=True)
total_hits = sum(
stats["hits"]
for _, _, stats in pattern_performance.items()
if any(p[0] == stats for p, _, _ in patterns)
)
total_samples = sum(
stats["total"]
for _, _, stats in pattern_performance.items()
if any(p[0] == stats for p, _, _ in patterns)
)
category_effectiveness = (
total_hits / total_samples if total_samples > 0 else 0
)
print(
f"\n{category} Patterns: {category_effectiveness:.2%} overall effectiveness"
)
for pattern, effectiveness, total in patterns:
print(f" - {pattern}: {effectiveness:.2%} hit rate (n={total})")
if __name__ == "__main__":
print("\nCryptoWildfire Backtesting Demo")
print("==============================\n")
# Uncomment the test you want to run
results, report_file, plot_files = run_demo_backtest()
# results, report_file, plot_files = run_comparative_backtest()
# Analyze pattern effectiveness
analyze_pattern_effectiveness(results)