Skip to content

Commit 0c3a2d6

Browse files
Pigbibicodex
andcommitted
fix: preserve independent lifecycle baselines
Co-Authored-By: Codex <noreply@openai.com>
1 parent 2089407 commit 0c3a2d6

2 files changed

Lines changed: 30 additions & 26 deletions

File tree

scripts/run_walk_forward_backtest.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ def _result_payload(item: Any) -> dict[str, Any]:
4747
}
4848

4949

50-
def _baseline_param_set_id(profile: str, params: dict[str, Any], *, provenance: str | None = None) -> str:
50+
def _baseline_param_set_id(profile: str, params: dict[str, Any]) -> str:
5151
fingerprint = hashlib.sha256(json.dumps(params, sort_keys=True, default=str).encode("utf-8")).hexdigest()[:12]
52-
suffix = hashlib.sha256(str(provenance or "unknown").encode("utf-8")).hexdigest()[:8]
53-
return f"{profile}_baseline_{fingerprint}_{suffix}"
52+
return f"{profile}_baseline_{fingerprint}"
5453

5554

5655
def _build_runner(*, profile: str, synthetic_days: int, panel: Any = None, market_history: Any = None):
@@ -78,24 +77,37 @@ def run_walk_forward(
7877
raise ValueError(f"unsupported profile={profile!r}; supported={sorted(SUPPORTED_PROFILES)}")
7978

8079
params = dict(PROFILE_DEFAULTS.get(profile, {"min_history_days": DEFAULT_MIN_HISTORY_DAYS}))
80+
store = PerformanceStore(local_root=store_root or Path("/tmp/crypto_wf_store"))
81+
orchestrator = BacktestOrchestrator(store=store)
82+
83+
baseline_params = copy.deepcopy(params)
84+
baseline_store_params = copy.deepcopy(baseline_params)
8185
baseline_runner = _build_runner(
8286
profile=profile,
8387
panel=panel,
8488
market_history=market_history,
8589
synthetic_days=synthetic_days,
8690
)
91+
baseline_raw = baseline_runner.run(
92+
profile,
93+
baseline_params,
94+
start_date=None,
95+
end_date=None,
96+
)
97+
baseline = orchestrator.persist_result(
98+
baseline_raw,
99+
strategy_profile=profile,
100+
domain="crypto",
101+
params=baseline_store_params,
102+
param_set_id=_baseline_param_set_id(profile, baseline_store_params),
103+
)
87104
runner = _build_runner(
88105
profile=profile,
89-
panel=copy.deepcopy(panel),
90-
market_history=copy.deepcopy(market_history),
106+
panel=panel,
107+
market_history=market_history,
91108
synthetic_days=synthetic_days,
92109
)
93-
store = PerformanceStore(local_root=store_root or Path("/tmp/crypto_wf_store"))
94-
orchestrator = BacktestOrchestrator(store=store)
95110
orchestrator.register_runner("crypto", runner)
96-
97-
baseline_params = copy.deepcopy(params)
98-
baseline_raw = baseline_runner.run(profile, baseline_params)
99111
wf_params = copy.deepcopy(params)
100112
wf_results = orchestrator.walk_forward(
101113
profile,
@@ -104,13 +116,6 @@ def run_walk_forward(
104116
windows=windows,
105117
param_set_id=f"{profile}_wf",
106118
)
107-
baseline = orchestrator.persist_result(
108-
baseline_raw,
109-
strategy_profile=profile,
110-
domain="crypto",
111-
params=baseline_params,
112-
param_set_id=_baseline_param_set_id(profile, baseline_params, provenance=getattr(baseline_raw, "run_id", None) or getattr(baseline_raw, "computed_at", None)),
113-
)
114119
return {
115120
"strategy_profile": profile,
116121
"domain": "crypto",
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import json
34
import sys
45
from pathlib import Path
56

@@ -11,23 +12,21 @@
1112
if str(SRC) not in sys.path:
1213
sys.path.insert(0, str(SRC))
1314

14-
from quant_platform_kit.strategy_lifecycle.performance_store import PerformanceStore
1515
from scripts.run_walk_forward_backtest import run_walk_forward
1616

1717

18-
def test_run_walk_forward_persists_latest_backtest(tmp_path: Path) -> None:
18+
def test_run_walk_forward_persists_lifecycle_baseline(tmp_path: Path) -> None:
1919
payload = run_walk_forward(
2020
profile="crypto_live_pool_rotation",
2121
synthetic_days=2200,
2222
store_root=tmp_path,
2323
)
2424

25-
store = PerformanceStore(local_root=tmp_path)
26-
backtest = store.load_latest_backtest("crypto", "crypto_live_pool_rotation")
25+
records = [
26+
json.loads(path.read_text(encoding="utf-8"))
27+
for path in (tmp_path / "backtest" / "crypto" / "crypto_live_pool_rotation").glob("*.json")
28+
]
2729

2830
assert payload["baseline"]["sharpe_ratio"] is not None
29-
assert payload["baseline"]["run_id"] is not None
30-
assert backtest is not None
31-
assert backtest.strategy_profile == "crypto_live_pool_rotation"
32-
assert "_baseline_" in backtest.param_set_id
33-
assert not backtest.param_set_id.endswith("_unknown")
31+
assert any("_baseline_" in record["param_set_id"] for record in records)
32+
assert any("_wf" in record["param_set_id"] for record in records)

0 commit comments

Comments
 (0)