-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_benchmarks.py
More file actions
421 lines (343 loc) · 13.9 KB
/
run_benchmarks.py
File metadata and controls
421 lines (343 loc) · 13.9 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
"""
Benchmark Runner - Full HFT Benchmark Suite Testing
====================================================
Runs the 50-scenario benchmark suite with microstructure-aware backtest
and walk-forward validation to ensure robust strategy performance.
Usage:
python run_benchmarks.py # Run with default settings
python run_benchmarks.py --verbose # Detailed output
python run_benchmarks.py --optimize # Run parameter optimization
"""
import sys
import json
import random
import argparse
from datetime import datetime
from typing import Dict, List, Optional
import statistics
# Add src to path
sys.path.insert(0, 'src')
from backtesting.benchmark_suite import create_benchmark_suite, BenchmarkSuite, ScenarioType
from backtesting.microstructure_backtest import (
MicrostructureBacktestEngine,
WalkForwardValidator,
TradingCosts
)
from strategies.robust_strategy import (
RobustStrategy,
RobustStrategyConfig,
create_robust_strategy,
get_strategy_signal_fn,
PARAMETER_GRID
)
from strategies.momentum_strategy import (
MomentumStrategy,
MomentumConfig,
create_momentum_strategy,
get_momentum_signal_fn
)
def run_full_benchmark(
strategy: RobustStrategy,
initial_capital: float = 75.0,
seed: int = 42,
verbose: bool = False
) -> Dict:
"""
Run full benchmark suite with given strategy.
Returns comprehensive performance report.
"""
print("\n" + "=" * 70)
print("POLYMARKET BOT - HFT BENCHMARK SUITE")
print("=" * 70)
print(f"Initial Capital: ${initial_capital}")
print(f"Seed: {seed}")
print(f"Strategy: {strategy.config.min_raw_edge:.0%} min raw edge, "
f"{strategy.config.min_net_edge:.0%} min net edge")
# Create suite
suite = create_benchmark_suite(seed=seed)
print(f"\nLoaded {suite.scenario_count} scenarios")
print(f" Normal: {len(suite.normal_scenarios)}")
print(f" Edge Cases: {len(suite.edge_case_scenarios)}")
# Create engine
engine = MicrostructureBacktestEngine(initial_capital=initial_capital)
# Get signal function
signal_fn = get_strategy_signal_fn(strategy)
# Run full suite
print("\n" + "-" * 70)
print("RUNNING BENCHMARK SUITE...")
print("-" * 70)
results = engine.run_suite(suite, signal_fn, verbose=verbose)
# Print summary
print("\n" + "=" * 70)
print("BENCHMARK RESULTS")
print("=" * 70)
summary = results["summary"]
print(f"\nOVERALL PERFORMANCE:")
print(f" Scenarios: {summary['total_scenarios']}")
print(f" Total Net P&L: ${summary['total_net_pnl']}")
print(f" Average Return per Scenario: {summary['avg_net_return']}")
print(f" Total Trades: {summary['total_trades']}")
print(f" Overall Win Rate: {summary['overall_win_rate']}")
print(f" Profitable Scenarios: {summary['profitable_scenarios']}/{summary['total_scenarios']}")
print(f"\nCOST BREAKDOWN:")
print(f" Total Fees: ${summary['total_fees']}")
print(f" Total Slippage: ${summary['total_slippage']}")
normal = results["normal_scenarios"]
edge = results["edge_case_scenarios"]
print(f"\nNORMAL SCENARIOS ({normal['count']}):")
print(f" Average Return: {normal['avg_return']}")
print(f" Profitable: {normal['profitable']}/{normal['count']}")
print(f"\nEDGE CASE SCENARIOS ({edge['count']}):")
print(f" Average Return: {edge['avg_return']}")
print(f" Profitable: {edge['profitable']}/{edge['count']}")
# Calculate weekly return estimate
# Average 1 week per scenario, so total weekly return = avg return
avg_return_str = summary['avg_net_return']
try:
avg_return_pct = float(avg_return_str.replace('%', '')) / 100
except:
avg_return_pct = 0
weekly_estimate = avg_return_pct * 100 # Already per scenario which is ~1 week
print(f"\n" + "=" * 70)
print(f"ESTIMATED WEEKLY RETURN: {weekly_estimate:.1f}%")
if weekly_estimate >= 10:
print("TARGET ACHIEVED: 10%+ weekly returns")
elif weekly_estimate >= 5:
print("PARTIAL SUCCESS: 5-10% weekly returns")
else:
print("NEEDS IMPROVEMENT: Below 5% weekly returns")
print("=" * 70)
return results
def run_walk_forward_validation(
strategy: RobustStrategy,
initial_capital: float = 75.0,
seed: int = 42,
num_folds: int = 5
) -> Dict:
"""
Run walk-forward validation to check for overfitting.
"""
print("\n" + "=" * 70)
print("WALK-FORWARD VALIDATION")
print("=" * 70)
print(f"Folds: {num_folds}")
# Create suite and engine
random.seed(seed)
suite = create_benchmark_suite(seed=seed)
engine = MicrostructureBacktestEngine(initial_capital=initial_capital)
# Create validator
validator = WalkForwardValidator(engine)
# Get signal function
signal_fn = get_strategy_signal_fn(strategy)
# Run validation
wf_results = validator.validate(suite, signal_fn, num_folds=num_folds)
print(f"\nVALIDATION RESULTS:")
print(f" Average Return per Scenario: {wf_results['avg_return_per_scenario']}")
print(f" Return Std Dev: {wf_results['return_std']}")
print(f" Estimated Sharpe: {wf_results['sharpe_estimate']:.2f}")
print(f" Is Robust: {wf_results['is_robust']}")
print(f"\nFOLD BREAKDOWN:")
for fold in wf_results["fold_results"]:
print(f" Fold {fold['fold']}: "
f"P&L ${fold['net_pnl']:.2f}, "
f"Return {fold['avg_return']:.1%}, "
f"WR {fold['win_rate']:.0%}")
return wf_results
def run_parameter_optimization(
initial_capital: float = 75.0,
seed: int = 42,
max_combinations: int = 50
) -> Dict:
"""
Run grid search to find optimal parameters.
"""
print("\n" + "=" * 70)
print("PARAMETER OPTIMIZATION")
print("=" * 70)
# Generate combinations
from itertools import product
params_to_test = {
"min_raw_edge": [0.12, 0.15, 0.18],
"min_net_edge": [0.06, 0.08, 0.10],
"min_confidence": [0.55, 0.60, 0.65],
"max_position_pct": [0.08, 0.12],
}
keys = list(params_to_test.keys())
values = list(params_to_test.values())
combinations = [dict(zip(keys, combo)) for combo in product(*values)]
# Limit combinations
if len(combinations) > max_combinations:
random.seed(seed)
combinations = random.sample(combinations, max_combinations)
print(f"Testing {len(combinations)} parameter combinations...")
# Create suite
suite = create_benchmark_suite(seed=seed)
best_result = None
best_params = None
best_score = -float('inf')
results = []
for i, params in enumerate(combinations):
# Create strategy with these params
config = RobustStrategyConfig(
min_raw_edge=params["min_raw_edge"],
min_net_edge=params["min_net_edge"],
min_confidence=params["min_confidence"],
max_position_pct=params["max_position_pct"],
)
strategy = RobustStrategy(config)
# Create engine
engine = MicrostructureBacktestEngine(initial_capital=initial_capital)
signal_fn = get_strategy_signal_fn(strategy)
# Run suite (quietly)
suite_results = engine.run_suite(suite, signal_fn, verbose=False)
# Extract metrics
summary = suite_results["summary"]
try:
avg_return = float(summary['avg_net_return'].replace('%', '')) / 100
except:
avg_return = 0
profitable_ratio = summary['profitable_scenarios'] / summary['total_scenarios']
win_rate_str = summary['overall_win_rate'].replace('%', '')
try:
win_rate = float(win_rate_str) / 100
except:
win_rate = 0
# Score: weighted combination of return, profitability, and win rate
score = avg_return * 0.5 + profitable_ratio * 0.3 + win_rate * 0.2
results.append({
"params": params,
"avg_return": avg_return,
"profitable_ratio": profitable_ratio,
"win_rate": win_rate,
"score": score,
})
if score > best_score:
best_score = score
best_params = params
best_result = suite_results
# Progress
if (i + 1) % 10 == 0:
print(f" Tested {i + 1}/{len(combinations)} combinations...")
# Sort results
results.sort(key=lambda x: x["score"], reverse=True)
print(f"\n" + "-" * 70)
print("TOP 5 PARAMETER COMBINATIONS:")
print("-" * 70)
for i, r in enumerate(results[:5], 1):
print(f"\n{i}. Score: {r['score']:.4f}")
print(f" Return: {r['avg_return']:.1%}, Profitable: {r['profitable_ratio']:.0%}, WR: {r['win_rate']:.0%}")
print(f" Params: {r['params']}")
print(f"\n" + "=" * 70)
print("BEST PARAMETERS:")
print("=" * 70)
for key, value in best_params.items():
print(f" {key}: {value}")
return {
"best_params": best_params,
"best_score": best_score,
"all_results": results,
}
def generate_report(
benchmark_results: Dict,
validation_results: Optional[Dict] = None,
optimization_results: Optional[Dict] = None
) -> str:
"""
Generate a markdown report of all results.
"""
report = []
report.append("# Polymarket Bot - Benchmark Report")
report.append(f"\nGenerated: {datetime.now().isoformat()}")
report.append("\n## Summary")
summary = benchmark_results["summary"]
report.append(f"- **Total Scenarios**: {summary['total_scenarios']}")
report.append(f"- **Total Net P&L**: ${summary['total_net_pnl']}")
report.append(f"- **Average Return**: {summary['avg_net_return']}")
report.append(f"- **Total Trades**: {summary['total_trades']}")
report.append(f"- **Win Rate**: {summary['overall_win_rate']}")
report.append(f"- **Profitable Scenarios**: {summary['profitable_scenarios']}")
report.append("\n## Cost Analysis")
report.append(f"- **Total Fees**: ${summary['total_fees']}")
report.append(f"- **Total Slippage**: ${summary['total_slippage']}")
report.append("\n## Scenario Breakdown")
report.append("\n### Normal Scenarios")
normal = benchmark_results["normal_scenarios"]
report.append(f"- Count: {normal['count']}")
report.append(f"- Average Return: {normal['avg_return']}")
report.append(f"- Profitable: {normal['profitable']}")
report.append("\n### Edge Case Scenarios")
edge = benchmark_results["edge_case_scenarios"]
report.append(f"- Count: {edge['count']}")
report.append(f"- Average Return: {edge['avg_return']}")
report.append(f"- Profitable: {edge['profitable']}")
if validation_results:
report.append("\n## Walk-Forward Validation")
report.append(f"- **Average Return**: {validation_results['avg_return_per_scenario']}")
report.append(f"- **Std Dev**: {validation_results['return_std']}")
report.append(f"- **Is Robust**: {validation_results['is_robust']}")
if optimization_results:
report.append("\n## Optimization Results")
report.append(f"- **Best Score**: {optimization_results['best_score']:.4f}")
report.append("\n### Best Parameters")
for key, value in optimization_results['best_params'].items():
report.append(f"- {key}: {value}")
return "\n".join(report)
def main():
parser = argparse.ArgumentParser(description="Run Polymarket Bot Benchmarks")
parser.add_argument("--verbose", "-v", action="store_true", help="Verbose output")
parser.add_argument("--optimize", "-o", action="store_true", help="Run parameter optimization")
parser.add_argument("--validate", action="store_true", help="Run walk-forward validation")
parser.add_argument("--capital", type=float, default=75.0, help="Initial capital")
parser.add_argument("--seed", type=int, default=42, help="Random seed")
parser.add_argument("--risk", choices=["conservative", "moderate", "aggressive"],
default="moderate", help="Risk level")
parser.add_argument("--report", action="store_true", help="Generate markdown report")
args = parser.parse_args()
# Create strategy
strategy = create_robust_strategy(capital=args.capital, risk_level=args.risk)
print(f"\nStrategy Configuration ({args.risk}):")
print(f" Min raw edge: {strategy.config.min_raw_edge:.0%}")
print(f" Min net edge: {strategy.config.min_net_edge:.0%}")
print(f" Min confidence: {strategy.config.min_confidence:.0%}")
print(f" Max spread: {strategy.config.max_spread:.0%}")
print(f" Max position: {strategy.config.max_position_pct:.0%}")
# Run benchmark
benchmark_results = run_full_benchmark(
strategy,
initial_capital=args.capital,
seed=args.seed,
verbose=args.verbose
)
validation_results = None
optimization_results = None
# Run validation if requested
if args.validate:
validation_results = run_walk_forward_validation(
strategy,
initial_capital=args.capital,
seed=args.seed,
num_folds=5
)
# Run optimization if requested
if args.optimize:
optimization_results = run_parameter_optimization(
initial_capital=args.capital,
seed=args.seed,
max_combinations=50
)
# Generate report if requested
if args.report:
report = generate_report(
benchmark_results,
validation_results,
optimization_results
)
report_path = "benchmark_report.md"
with open(report_path, "w") as f:
f.write(report)
print(f"\nReport saved to: {report_path}")
print("\n" + "=" * 70)
print("BENCHMARK COMPLETE")
print("=" * 70)
if __name__ == "__main__":
main()