-
Notifications
You must be signed in to change notification settings - Fork 0
feat: migrate backtest result timing identity #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
95b752c
feat: migrate backtest result timing identity
Pigbibi 61eb21d
fix: preserve backtest timing store compatibility
Pigbibi 48fea73
fix: finalize backtest timing latest selection
Pigbibi c0475ac
fix: enforce durable backtest persistence metadata
Pigbibi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import tempfile | ||
| import unittest | ||
| from datetime import date | ||
| from pathlib import Path | ||
|
|
||
| from quant_platform_kit.strategy_lifecycle.contracts import BacktestResult | ||
| from quant_platform_kit.strategy_lifecycle.backtest_orchestrator import BacktestOrchestrator | ||
| from quant_platform_kit.strategy_lifecycle.performance_store import ( | ||
| LEGACY_EXECUTION_TIMING, | ||
| PerformanceStore, | ||
| ) | ||
|
|
||
|
|
||
| def _result(*, timing: str | None, computed_at: str, profile: str = "SOXL") -> BacktestResult: | ||
| return BacktestResult( | ||
| strategy_profile=profile, | ||
| domain="us_equity", | ||
| param_set_id="baseline", | ||
| params={"lookback": 20}, | ||
| execution_timing=timing, | ||
| result_identity_version=2, | ||
| persist_mode="durable", | ||
| computed_at=computed_at, | ||
| start_date=date(2020, 1, 1), | ||
| end_date=date(2024, 1, 1), | ||
| ) | ||
|
|
||
|
|
||
| class BacktestResultStoreMigrationTests(unittest.TestCase): | ||
| def test_old_positional_backtest_result_order_is_unchanged(self) -> None: | ||
| result = BacktestResult("s", "d", "p", {}, 3, 1.1, 0.9, 0.8, -0.2, 0.1, 0.3, 0.5, 0.4) | ||
| self.assertEqual(result.param_version, 3) | ||
| self.assertEqual(result.sharpe_ratio, 1.1) | ||
| self.assertEqual(result.total_return, 0.4) | ||
| self.assertIsNone(result.execution_timing) | ||
|
|
||
| def test_v1_record_without_metadata_remains_readable(self) -> None: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| store = PerformanceStore(local_root=Path(tmp)) | ||
| key = "backtest/us_equity/SOXL/backtest_v1_legacy.json" | ||
| store._write(key, {"strategy_profile": "SOXL", "domain": "us_equity", "param_set_id": "legacy", "params": {}}) | ||
| loaded = store.load_latest_backtest("us_equity", "SOXL", execution_timing=LEGACY_EXECUTION_TIMING) | ||
| self.assertIsNotNone(loaded) | ||
| self.assertIsNone(loaded.execution_timing) | ||
| self.assertEqual(loaded.result_identity_version, 1) | ||
| self.assertEqual(loaded.persist_mode, "durable") | ||
|
|
||
| def test_mixed_timing_records_have_distinct_keys_and_filters(self) -> None: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| store = PerformanceStore(local_root=Path(tmp)) | ||
| store.save_backtest_result(_result(timing="next_open", computed_at="2026-01-01T00:00:00+00:00")) | ||
| store.save_backtest_result(_result(timing="next_close", computed_at="2026-01-01T00:00:00+00:00")) | ||
| keys = store._list_local_json_keys("backtest/us_equity/SOXL/") | ||
| latest = store.load_latest_backtest("us_equity", "SOXL") | ||
| opened = store.load_latest_backtest("us_equity", "SOXL", execution_timing="next_open") | ||
| closed = store.load_latest_backtest("us_equity", "SOXL", execution_timing="next_close") | ||
| self.assertEqual(len(keys), 2) | ||
| self.assertEqual(latest.execution_timing, "next_close") | ||
| self.assertEqual(opened.execution_timing, "next_open") | ||
| self.assertEqual(closed.execution_timing, "next_close") | ||
|
|
||
| def test_reserved_looking_timing_does_not_collide_with_missing_timing(self) -> None: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| store = PerformanceStore(local_root=Path(tmp)) | ||
| store.save_backtest_result(_result(timing=None, computed_at="2026-01-01T00:00:00+00:00")) | ||
| store.save_backtest_result( | ||
| _result(timing="legacy_unknown", computed_at="2026-01-01T00:00:00+00:00") | ||
| ) | ||
| keys = store._list_local_json_keys("backtest/us_equity/SOXL/") | ||
| self.assertEqual(len(keys), 2) | ||
|
|
||
| def test_save_records_write_revision_for_equal_logical_slots(self) -> None: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| store = PerformanceStore(local_root=Path(tmp)) | ||
| store.save_backtest_result(_result(timing="next_open", computed_at="2026-01-01T00:00:00+00:00")) | ||
| store.save_backtest_result(_result(timing="next_close", computed_at="2026-01-01T00:00:00+00:00")) | ||
| payloads = [store._read(key) for key in store._list_local_json_keys("backtest/us_equity/SOXL/")] | ||
| revisions = [payload["store_write_revision"] for payload in payloads] | ||
| self.assertEqual(len(set(revisions)), 2) | ||
| self.assertTrue(all(payload.get("store_write_timestamp") for payload in payloads)) | ||
|
|
||
| def test_explicit_legacy_sentinel_is_distinct_from_omitted_filter(self) -> None: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| store = PerformanceStore(local_root=Path(tmp)) | ||
| store.save_backtest_result(_result(timing=None, computed_at="2026-01-01T00:00:00+00:00")) | ||
| store.save_backtest_result(_result(timing="next_open", computed_at="2026-01-02T00:00:00+00:00")) | ||
| omitted = store.load_latest_backtest("us_equity", "SOXL") | ||
| explicit_none = store.load_latest_backtest("us_equity", "SOXL", execution_timing=None) | ||
| legacy = store.load_latest_backtest("us_equity", "SOXL", execution_timing=LEGACY_EXECUTION_TIMING) | ||
| self.assertEqual(omitted.execution_timing, "next_open") | ||
| self.assertEqual(explicit_none.execution_timing, "next_open") | ||
| self.assertIsNone(legacy.execution_timing) | ||
|
|
||
| def test_empty_timing_is_rejected_on_write_and_codec(self) -> None: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| store = PerformanceStore(local_root=Path(tmp)) | ||
| with self.assertRaises(ValueError): | ||
| store.save_backtest_result(_result(timing="", computed_at="2026-01-01T00:00:00+00:00")) | ||
| store._write( | ||
| "backtest/us_equity/SOXL/invalid.json", | ||
| {"strategy_profile": "SOXL", "domain": "us_equity", "execution_timing": "", "params": {}}, | ||
| ) | ||
| self.assertIsNone(store.load_latest_backtest("us_equity", "SOXL", execution_timing="")) | ||
|
|
||
| def test_non_durable_persist_mode_is_rejected_before_write(self) -> None: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| store = PerformanceStore(local_root=Path(tmp)) | ||
| result = _result(timing="next_open", computed_at="2026-01-01T00:00:00+00:00") | ||
| result = BacktestResult(**{**result.__dict__, "persist_mode": "ephemeral"}) | ||
| with self.assertRaises(ValueError): | ||
| store.save_backtest_result(result) | ||
| self.assertEqual(list(Path(tmp).rglob("*.json")), []) | ||
|
|
||
| def test_orchestrator_persistence_preserves_result_metadata(self) -> None: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| store = PerformanceStore(local_root=Path(tmp)) | ||
| orchestrator = BacktestOrchestrator(store=store) | ||
| source = _result(timing="next_close", computed_at="2026-01-01T00:00:00+00:00") | ||
| persisted = orchestrator.persist_result( | ||
| source, | ||
| strategy_profile=source.strategy_profile, | ||
| domain=source.domain, | ||
| params=source.params, | ||
| param_set_id=source.param_set_id, | ||
| param_version=source.param_version, | ||
| ) | ||
| loaded = store.load_latest_backtest("us_equity", "SOXL", execution_timing="next_close") | ||
| self.assertEqual(persisted.execution_timing, "next_close") | ||
| self.assertEqual(persisted.result_identity_version, 2) | ||
| self.assertEqual(persisted.persist_mode, "durable") | ||
| self.assertEqual(loaded.execution_timing, "next_close") | ||
| self.assertEqual(loaded.result_identity_version, 2) | ||
|
|
||
| def test_result_json_round_trip_preserves_dates_and_nan(self) -> None: | ||
| result = _result(timing="next_open", computed_at="2026-01-01T00:00:00+00:00") | ||
| payload = result.to_dict() | ||
| self.assertEqual(payload["start_date"], "2020-01-01") | ||
| self.assertEqual(payload["end_date"], "2024-01-01") | ||
| self.assertEqual(json.loads(json.dumps(payload))["execution_timing"], "next_open") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.