-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerate_report.py
More file actions
817 lines (701 loc) · 24 KB
/
generate_report.py
File metadata and controls
817 lines (701 loc) · 24 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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
#!/usr/bin/env python3
"""
generate_report.py — Convert benchmark JSON output into a beautiful HTML report.
Usage:
python generate_report.py benchmark_results.json -o report.html
python generate_report.py benchmark_results.json # writes to benchmark_report.html
"""
import argparse
import json
import os
import sys
from datetime import datetime
from typing import Any, Dict, List, Optional
def load_json(path: str) -> Dict[str, Any]:
with open(path) as f:
return json.load(f)
def _pct_class(val: Optional[float]) -> str:
if val is None:
return "neutral"
return "positive" if val >= 0 else "negative"
def _fmt_pct(val: Optional[float]) -> str:
if val is None:
return "N/A"
return f"{val:+.2f}%"
def _fmt_sharpe(val: Optional[float]) -> str:
if val is None:
return "N/A"
return f"{val:.4f}"
def _sharpe_class(val: Optional[float]) -> str:
if val is None:
return "neutral"
if val >= 1.0:
return "positive"
if val >= 0:
return "neutral"
return "negative"
def _fmt_duration(seconds: float) -> str:
if seconds < 60:
return f"{seconds:.1f}s"
mins = int(seconds // 60)
secs = seconds % 60
return f"{mins}m {secs:.1f}s"
def _status_badge(status: str) -> str:
cls = {"pass": "badge-pass", "fail": "badge-fail", "skip": "badge-skip",
"error": "badge-fail", "ok": "badge-pass"}.get(status, "badge-skip")
label = status.upper()
return f'<span class="badge {cls}">{label}</span>'
def _build_summary_card(data: Dict[str, Any]) -> str:
"""Build the top-level summary card."""
summary = data.get("summary", {})
total = summary.get("total", 0)
passed = summary.get("passed", 0)
failed = summary.get("failed", 0)
skipped = summary.get("skipped", 0)
duration = summary.get("duration_s", 0)
mode = data.get("mode", "full")
timestamp = data.get("timestamp", "")
pass_pct = (passed / max(total, 1)) * 100
fail_pct = (failed / max(total, 1)) * 100
skip_pct = 100 - pass_pct - fail_pct
overall_status = "PASS" if failed == 0 else "FAIL"
overall_class = "summary-pass" if failed == 0 else "summary-fail"
return f"""
<div class="summary-card {overall_class}">
<div class="summary-header">
<h2>Benchmark Summary</h2>
<span class="badge {'badge-pass' if failed == 0 else 'badge-fail'} badge-lg">{overall_status}</span>
</div>
<div class="summary-meta">
<span>Mode: <strong>{mode.title()}</strong></span>
<span>Duration: <strong>{_fmt_duration(duration)}</strong></span>
<span>Date: <strong>{timestamp[:19] if timestamp else 'N/A'}</strong></span>
</div>
<div class="progress-bar">
<div class="progress-pass" style="width:{pass_pct:.1f}%"></div>
<div class="progress-fail" style="width:{fail_pct:.1f}%"></div>
<div class="progress-skip" style="width:{skip_pct:.1f}%"></div>
</div>
<div class="summary-counts">
<div class="count-box count-pass">
<div class="count-number">{passed}</div>
<div class="count-label">Passed</div>
</div>
<div class="count-box count-fail">
<div class="count-number">{failed}</div>
<div class="count-label">Failed</div>
</div>
<div class="count-box count-skip">
<div class="count-number">{skipped}</div>
<div class="count-label">Skipped</div>
</div>
<div class="count-box count-total">
<div class="count-number">{total}</div>
<div class="count-label">Total</div>
</div>
</div>
</div>
"""
def _build_data_integrity_section(data: Dict[str, Any]) -> str:
"""Build the data integrity section."""
di = data.get("data_integrity", {})
if not di:
return ""
status = di.get("status", "skip")
details = di.get("details", {})
duration = di.get("duration_s", 0)
rows = ""
for key, label in [
("total_files", "Total Files"),
("valid_files", "Valid Files"),
("invalid_files", "Invalid Files"),
("unique_assets", "Unique Assets"),
]:
val = details.get(key, "N/A")
rows += f"<tr><td>{label}</td><td>{val}</td></tr>\n"
tfs = details.get("timeframes", [])
if tfs:
rows += f"<tr><td>Timeframes</td><td>{', '.join(tfs)}</td></tr>\n"
# Show expected vs actual file count
expected = details.get("unique_assets", 0)
if isinstance(expected, int) and expected > 0 and tfs:
expected_total = expected * len(tfs)
actual_total = details.get("total_files", 0)
coverage = f"{actual_total}/{expected_total}" if isinstance(actual_total, int) else "N/A"
rows += f"<tr><td>Coverage</td><td>{coverage} (assets × timeframes)</td></tr>\n"
# Data source indicator
data_source = details.get("data_source", "")
if data_source:
rows += f"<tr><td>Data Source</td><td>{data_source}</td></tr>\n"
invalid_list = details.get("invalid_list", [])
invalid_html = ""
if invalid_list:
items = "".join(f"<li>{f}</li>" for f in invalid_list)
invalid_html = f'<div class="alert alert-warn"><strong>Invalid files:</strong><ul>{items}</ul></div>'
return f"""
<div class="section">
<div class="section-header">
<h3>Data Integrity Check</h3>
{_status_badge(status)}
<span class="duration">{_fmt_duration(duration)}</span>
</div>
<table class="detail-table">
<tbody>{rows}</tbody>
</table>
{invalid_html}
</div>
"""
def _build_alpha_section(data: Dict[str, Any]) -> str:
"""Build the alpha factor smoke test section."""
alpha = data.get("alpha_smoke_test", {})
if not alpha:
return ""
status = alpha.get("status", "skip")
duration = alpha.get("duration_s", 0)
details = alpha.get("details", {})
rows = ""
for name, info in details.items():
cols = ", ".join(info.get("columns_added", []))
row_count = info.get("rows", "N/A")
# Collect sample values for display
sample_vals = []
for k, v in info.items():
if k not in ("rows", "columns_added") and isinstance(v, (int, float)):
sample_vals.append(f"{k}={v}")
samples_str = "; ".join(sample_vals) if sample_vals else "—"
rows += f"""
<tr>
<td><strong>{name.upper()}</strong></td>
<td>{row_count} rows</td>
<td class="mono">{cols}</td>
<td class="mono" style="font-size:0.75rem">{samples_str}</td>
</tr>
"""
return f"""
<div class="section">
<div class="section-header">
<h3>Alpha Factor Smoke Test</h3>
{_status_badge(status)}
<span class="duration">{_fmt_duration(duration)}</span>
</div>
<table class="detail-table" style="max-width:100%">
<thead><tr><th>Factor</th><th>Data</th><th>Columns Added</th><th>Sample Values</th></tr></thead>
<tbody>{rows}</tbody>
</table>
</div>
"""
def _build_pipeline_section(data: Dict[str, Any]) -> str:
"""Build the standalone portfolio pipeline section."""
pipe = data.get("portfolio_pipeline", {})
if not pipe:
return ""
status = pipe.get("status", "skip")
duration = pipe.get("duration_s", 0)
metrics = pipe.get("metrics", {})
if not metrics:
return f"""
<div class="section">
<div class="section-header">
<h3>Standalone Portfolio Pipeline</h3>
{_status_badge(status)}
<span class="duration">{_fmt_duration(duration)}</span>
</div>
<p class="muted">No metrics available.</p>
</div>
"""
rows = ""
for key, label in [
("total_return_pct", "Total Return"),
("annualised_return_pct", "Annualised Return"),
("annualised_sharpe", "Sharpe Ratio"),
("annualised_sortino", "Sortino Ratio"),
("max_drawdown_pct", "Max Drawdown"),
("calmar", "Calmar Ratio"),
("n_bars", "Bars"),
("profit_abs", "Absolute Profit"),
]:
val = metrics.get(key)
if val is None:
formatted = "N/A"
cls = ""
elif key.endswith("_pct"):
formatted = _fmt_pct(val)
cls = _pct_class(val) if "drawdown" not in key else ("negative" if val < -10 else "neutral")
elif key in ("annualised_sharpe", "annualised_sortino", "calmar"):
formatted = _fmt_sharpe(val)
cls = _sharpe_class(val)
elif key == "profit_abs":
formatted = f"${val:,.2f}" if isinstance(val, (int, float)) else str(val)
cls = _pct_class(val) if isinstance(val, (int, float)) else ""
else:
formatted = str(val)
cls = ""
rows += f'<tr><td>{label}</td><td class="{cls}">{formatted}</td></tr>\n'
return f"""
<div class="section">
<div class="section-header">
<h3>Standalone Portfolio Pipeline</h3>
{_status_badge(status)}
<span class="duration">{_fmt_duration(duration)}</span>
</div>
<table class="detail-table">
<tbody>{rows}</tbody>
</table>
</div>
"""
def _build_backtest_section(title: str, backtests: List[Dict]) -> str:
"""Build a backtest results section with leaderboard and detail grid."""
if not backtests:
return ""
passed = sum(1 for r in backtests if r.get("status") == "pass")
failed = sum(1 for r in backtests if r.get("status") == "fail")
total = len(backtests)
# Leaderboard: best return per strategy
strat_best: Dict[str, Dict] = {}
for r in backtests:
name = r.get("strategy", "?")
m = r.get("metrics", {})
ret = m.get("total_return_pct")
if ret is None:
continue
if name not in strat_best or ret > strat_best[name]["return"]:
strat_best[name] = {
"return": ret,
"sharpe": m.get("sharpe"),
"sortino": m.get("sortino"),
"calmar": m.get("calmar"),
"trades": m.get("trades", 0),
"asset_class": r.get("asset_class", "?"),
"timeframe": r.get("timeframe", "?"),
"drawdown": m.get("max_drawdown_pct"),
"win_rate": m.get("win_rate_pct"),
"profit_factor": m.get("profit_factor"),
"avg_profit": m.get("avg_profit_pct"),
"avg_duration": m.get("avg_duration"),
}
ranked = sorted(strat_best.items(), key=lambda x: x[1]["return"], reverse=True)
leaderboard_rows = ""
for i, (name, info) in enumerate(ranked, 1):
medal = {1: "gold", 2: "silver", 3: "bronze"}.get(i, "")
medal_icon = {1: "🥇", 2: "🥈", 3: "🥉"}.get(i, str(i))
ret_cls = _pct_class(info["return"])
sharpe_cls = _sharpe_class(info.get("sharpe"))
sortino_cls = _sharpe_class(info.get("sortino"))
dd_cls = "negative" if info.get("drawdown") is not None and info["drawdown"] < -10 else "neutral"
leaderboard_rows += f"""
<tr class="{medal}">
<td class="rank">{medal_icon}</td>
<td class="strat-name">{name}</td>
<td class="{ret_cls}">{_fmt_pct(info['return'])}</td>
<td class="{sharpe_cls}">{_fmt_sharpe(info.get('sharpe'))}</td>
<td class="{sortino_cls}">{_fmt_sharpe(info.get('sortino'))}</td>
<td>{info.get('trades', 'N/A')}</td>
<td class="{dd_cls}">{_fmt_pct(info.get('drawdown'))}</td>
<td>{_fmt_pct(info.get('win_rate'))}</td>
<td>{_fmt_sharpe(info.get('profit_factor'))}</td>
<td>{_fmt_pct(info.get('avg_profit'))}</td>
<td class="mono">{info['asset_class']}/{info['timeframe']}</td>
</tr>
"""
# Detail grid: per strategy x asset x timeframe
detail_rows = ""
for r in backtests:
status = r.get("status", "?")
m = r.get("metrics", {})
dd_cls = "negative" if m.get("max_drawdown_pct") is not None and m["max_drawdown_pct"] < -10 else "neutral"
detail_rows += f"""
<tr>
<td>{r.get('strategy', '?')}</td>
<td>{r.get('asset_class', '?')}</td>
<td>{r.get('timeframe', '?')}</td>
<td>{_status_badge(status)}</td>
<td class="{_pct_class(m.get('total_return_pct'))}">{_fmt_pct(m.get('total_return_pct'))}</td>
<td class="{_sharpe_class(m.get('sharpe'))}">{_fmt_sharpe(m.get('sharpe'))}</td>
<td class="{_sharpe_class(m.get('sortino'))}">{_fmt_sharpe(m.get('sortino'))}</td>
<td class="{dd_cls}">{_fmt_pct(m.get('max_drawdown_pct'))}</td>
<td>{_fmt_pct(m.get('win_rate_pct'))}</td>
<td>{_fmt_sharpe(m.get('profit_factor'))}</td>
<td>{_fmt_pct(m.get('avg_profit_pct'))}</td>
<td>{m.get('trades', 'N/A')}</td>
<td class="mono">{m.get('avg_duration', 'N/A')}</td>
<td>{_fmt_duration(r.get('duration_s', 0))}</td>
</tr>
"""
return f"""
<div class="section">
<div class="section-header">
<h3>{title}</h3>
<span class="badge badge-info">{passed}/{total} passed</span>
</div>
<h4 class="subsection-title">Leaderboard</h4>
<div class="table-wrapper">
<table class="leaderboard-table">
<thead>
<tr>
<th>#</th>
<th>Strategy</th>
<th>Best Return</th>
<th>Sharpe</th>
<th>Sortino</th>
<th>Trades</th>
<th>Max DD</th>
<th>Win Rate</th>
<th>Profit Factor</th>
<th>Avg Profit</th>
<th>Best On</th>
</tr>
</thead>
<tbody>{leaderboard_rows}</tbody>
</table>
</div>
<details class="detail-toggle">
<summary>Show all {total} backtest results</summary>
<div class="table-wrapper">
<table class="detail-grid-table">
<thead>
<tr>
<th>Strategy</th>
<th>Asset Class</th>
<th>Timeframe</th>
<th>Status</th>
<th>Return</th>
<th>Sharpe</th>
<th>Sortino</th>
<th>Max DD</th>
<th>Win Rate</th>
<th>Profit Factor</th>
<th>Avg Profit</th>
<th>Trades</th>
<th>Avg Duration</th>
<th>Runtime</th>
</tr>
</thead>
<tbody>{detail_rows}</tbody>
</table>
</div>
</details>
</div>
"""
def generate_html(data: Dict[str, Any]) -> str:
"""Generate the complete HTML report."""
timestamp = data.get("timestamp", datetime.now().isoformat())
summary_html = _build_summary_card(data)
data_integrity_html = _build_data_integrity_section(data)
alpha_html = _build_alpha_section(data)
pipeline_html = _build_pipeline_section(data)
trading_html = _build_backtest_section(
"Trading Strategy Backtests", data.get("trading_backtests", [])
)
portfolio_html = _build_backtest_section(
"Portfolio Strategy Backtests", data.get("portfolio_backtests", [])
)
return f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PortfolioBench — Benchmark Report</title>
<style>
:root {{
--bg: #0d1117;
--surface: #161b22;
--surface-2: #1c2129;
--border: #30363d;
--text: #e6edf3;
--text-dim: #8b949e;
--accent: #58a6ff;
--green: #3fb950;
--green-bg: rgba(63,185,80,0.12);
--red: #f85149;
--red-bg: rgba(248,81,73,0.12);
--yellow: #d29922;
--yellow-bg: rgba(210,153,34,0.12);
--blue: #58a6ff;
--blue-bg: rgba(88,166,255,0.12);
--purple: #bc8cff;
--gold: #f0c040;
--silver: #c0c0c0;
--bronze: #cd7f32;
--radius: 8px;
--shadow: 0 1px 3px rgba(0,0,0,0.3);
}}
* {{ box-sizing: border-box; margin: 0; padding: 0; }}
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.6;
padding: 0;
}}
.container {{
max-width: 1200px;
margin: 0 auto;
padding: 24px 20px;
}}
/* Header */
.report-header {{
text-align: center;
padding: 40px 20px 32px;
background: linear-gradient(135deg, #161b22 0%, #1a2332 50%, #161b22 100%);
border-bottom: 1px solid var(--border);
margin-bottom: 24px;
}}
.report-header h1 {{
font-size: 2rem;
font-weight: 700;
background: linear-gradient(135deg, var(--accent), var(--purple));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
margin-bottom: 8px;
}}
.report-header .subtitle {{
color: var(--text-dim);
font-size: 0.95rem;
}}
/* Summary Card */
.summary-card {{
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--radius);
padding: 24px;
margin-bottom: 24px;
box-shadow: var(--shadow);
}}
.summary-pass {{ border-left: 4px solid var(--green); }}
.summary-fail {{ border-left: 4px solid var(--red); }}
.summary-header {{
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 16px;
}}
.summary-header h2 {{ font-size: 1.3rem; font-weight: 600; }}
.summary-meta {{
display: flex;
gap: 24px;
color: var(--text-dim);
font-size: 0.875rem;
margin-bottom: 16px;
flex-wrap: wrap;
}}
.summary-meta strong {{ color: var(--text); }}
.progress-bar {{
display: flex;
height: 8px;
border-radius: 4px;
overflow: hidden;
background: var(--surface-2);
margin-bottom: 20px;
}}
.progress-pass {{ background: var(--green); }}
.progress-fail {{ background: var(--red); }}
.progress-skip {{ background: var(--yellow); }}
.summary-counts {{
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 12px;
}}
.count-box {{
text-align: center;
padding: 16px 8px;
border-radius: var(--radius);
border: 1px solid var(--border);
}}
.count-pass {{ background: var(--green-bg); }}
.count-fail {{ background: var(--red-bg); }}
.count-skip {{ background: var(--yellow-bg); }}
.count-total {{ background: var(--blue-bg); }}
.count-number {{
font-size: 1.75rem;
font-weight: 700;
line-height: 1.2;
}}
.count-pass .count-number {{ color: var(--green); }}
.count-fail .count-number {{ color: var(--red); }}
.count-skip .count-number {{ color: var(--yellow); }}
.count-total .count-number {{ color: var(--accent); }}
.count-label {{
font-size: 0.75rem;
text-transform: uppercase;
letter-spacing: 0.05em;
color: var(--text-dim);
margin-top: 4px;
}}
/* Badges */
.badge {{
display: inline-block;
padding: 2px 10px;
border-radius: 12px;
font-size: 0.75rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.04em;
}}
.badge-pass {{ background: var(--green-bg); color: var(--green); border: 1px solid rgba(63,185,80,0.3); }}
.badge-fail {{ background: var(--red-bg); color: var(--red); border: 1px solid rgba(248,81,73,0.3); }}
.badge-skip {{ background: var(--yellow-bg); color: var(--yellow); border: 1px solid rgba(210,153,34,0.3); }}
.badge-info {{ background: var(--blue-bg); color: var(--accent); border: 1px solid rgba(88,166,255,0.3); }}
.badge-lg {{ padding: 4px 16px; font-size: 0.85rem; }}
/* Sections */
.section {{
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--radius);
padding: 24px;
margin-bottom: 20px;
box-shadow: var(--shadow);
}}
.section-header {{
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 16px;
flex-wrap: wrap;
}}
.section-header h3 {{
font-size: 1.1rem;
font-weight: 600;
margin: 0;
}}
.duration {{
color: var(--text-dim);
font-size: 0.8rem;
margin-left: auto;
}}
.subsection-title {{
font-size: 0.95rem;
font-weight: 600;
color: var(--accent);
margin: 16px 0 12px;
padding-bottom: 6px;
border-bottom: 1px solid var(--border);
}}
/* Tables */
.table-wrapper {{
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}}
table {{
width: 100%;
border-collapse: collapse;
font-size: 0.875rem;
}}
th, td {{
padding: 10px 12px;
text-align: left;
border-bottom: 1px solid var(--border);
}}
th {{
font-weight: 600;
color: var(--text-dim);
text-transform: uppercase;
font-size: 0.75rem;
letter-spacing: 0.04em;
background: var(--surface-2);
position: sticky;
top: 0;
}}
tbody tr:hover {{ background: rgba(88,166,255,0.04); }}
.detail-table {{ max-width: 500px; }}
.detail-table td:first-child {{ color: var(--text-dim); width: 40%; }}
/* Leaderboard */
.leaderboard-table .rank {{ width: 40px; text-align: center; font-size: 1.1rem; }}
.leaderboard-table .strat-name {{ font-weight: 600; }}
tr.gold td {{ background: rgba(240,192,64,0.06); }}
tr.silver td {{ background: rgba(192,192,192,0.04); }}
tr.bronze td {{ background: rgba(205,127,50,0.04); }}
/* Value coloring */
.positive {{ color: var(--green); font-weight: 600; }}
.negative {{ color: var(--red); font-weight: 600; }}
.neutral {{ color: var(--text-dim); }}
.mono {{ font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace; font-size: 0.8rem; }}
/* Detail toggle */
.detail-toggle {{
margin-top: 16px;
}}
.detail-toggle summary {{
cursor: pointer;
color: var(--accent);
font-size: 0.875rem;
font-weight: 500;
padding: 8px 0;
user-select: none;
}}
.detail-toggle summary:hover {{ text-decoration: underline; }}
.detail-toggle[open] summary {{ margin-bottom: 12px; }}
/* Alerts */
.alert {{
padding: 12px 16px;
border-radius: var(--radius);
margin-top: 12px;
font-size: 0.85rem;
}}
.alert-warn {{
background: var(--yellow-bg);
border: 1px solid rgba(210,153,34,0.3);
color: var(--yellow);
}}
.alert ul {{ margin: 8px 0 0 20px; }}
.alert li {{ margin-bottom: 2px; }}
.muted {{ color: var(--text-dim); font-style: italic; }}
/* Footer */
.report-footer {{
text-align: center;
padding: 24px;
color: var(--text-dim);
font-size: 0.8rem;
border-top: 1px solid var(--border);
margin-top: 32px;
}}
.report-footer a {{
color: var(--accent);
text-decoration: none;
}}
.report-footer a:hover {{ text-decoration: underline; }}
/* Responsive */
@media (max-width: 640px) {{
.summary-counts {{ grid-template-columns: repeat(2, 1fr); }}
.summary-meta {{ flex-direction: column; gap: 4px; }}
.report-header h1 {{ font-size: 1.5rem; }}
.container {{ padding: 12px 10px; }}
}}
</style>
</head>
<body>
<div class="report-header">
<h1>PortfolioBench</h1>
<div class="subtitle">Benchmark Report — {timestamp[:19] if timestamp else 'N/A'}</div>
</div>
<div class="container">
{summary_html}
{data_integrity_html}
{alpha_html}
{pipeline_html}
{trading_html}
{portfolio_html}
</div>
<div class="report-footer">
Generated by <a href="https://github.com/mlsys-io/PortfolioBench">PortfolioBench</a>
· {timestamp[:10] if timestamp else ''}
</div>
</body>
</html>"""
def main():
parser = argparse.ArgumentParser(
description="Generate an HTML benchmark report from JSON results.",
)
parser.add_argument("input", help="Path to benchmark JSON file")
parser.add_argument("-o", "--output", default="benchmark_report.html",
help="Output HTML file (default: benchmark_report.html)")
args = parser.parse_args()
if not os.path.isfile(args.input):
print(f"Error: input file not found: {args.input}", file=sys.stderr)
sys.exit(1)
data = load_json(args.input)
html = generate_html(data)
with open(args.output, "w") as f:
f.write(html)
print(f"Report generated: {args.output}")
if __name__ == "__main__":
main()