Coverage for evaluation/lean_report.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""`aq evaluate --lean-report` (V6.3.0): the run-folder tools for one finished Lean run, in the order the README needs 

2them. This module is the pure plan (which `aq evaluate` flags, with which extra arguments); `aq_cli._run_lean_report` 

3executes it step by step, each step best-effort so one failing tool never hides the others' results.""" 

4 

5from __future__ import annotations 

6 

7# (label, the `aq evaluate` flag attribute set on a copy of the parsed args) 

8LEAN_REPORT_FLAGS = ( 

9 ("lean statistics", "lean_stats"), 

10 ("Lean Monte Carlo", "lean_monte_carlo"), 

11 ("order-level audit", "audit_backtest"), 

12 ("P&L attribution", "attribute_lean"), 

13 ("fill prices", "audit_fills"), 

14 ("proof of alpha", "alpha_proof"), 

15 ("backtest statistics", "backtest_stats"), 

16 ("classification parity", "classification_parity"), 

17) 

18NEEDS_BOOK_HISTORY = frozenset({"attribute_lean", "classification_parity"}) 

19 

20 

21def split_periods(config: dict, names: tuple[str, ...] = ("backtest", "holdout")) -> list[str]: 

22 """`START..END` of the configured windows a Lean run spans (the README's Lean-vs-offline block reads one per split).""" 

23 windows = (config.get("phase1", {}) or {}).get("windows", {}) or {} 

24 periods = [] 

25 for name in names: 

26 window = windows.get(name) or {} 

27 if window.get("start") and window.get("end"): 

28 periods.append(f"{window['start']}..{window['end']}") 

29 return periods 

30 

31 

32def lean_report_steps(config: dict) -> list[tuple[str, dict]]: 

33 """[(label, {arg: value})]: every step switches its own flag on and the others off; the audit and the attribution 

34 get the split periods. A target-weights run has no book, so the tools that read its book history are left out.""" 

35 flags = [flag for _label, flag in LEAN_REPORT_FLAGS] 

36 no_book = bool(((config.get("phase_v2") or {}).get("target_weights") or {}).get("enabled")) 

37 steps = [] 

38 for label, flag in LEAN_REPORT_FLAGS: 

39 if no_book and flag in NEEDS_BOOK_HISTORY: 

40 continue 

41 overrides = {other: other == flag for other in flags} 

42 overrides["lean_report"] = False 

43 if flag in ("audit_backtest", "attribute_lean"): 

44 overrides["period"] = split_periods(config) 

45 steps.append((label, overrides)) 

46 return steps