Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(attribution): honor recorded run annualisation (#1948)

Rudy Mizrahi Celekli6345d769d8e3
diff --git a/agent/src/api/attribution_core.py b/agent/src/api/attribution_core.py
index dd9046336..68fe5982b 100644
--- a/agent/src/api/attribution_core.py
+++ b/agent/src/api/attribution_core.py
@@ -360,7 +360,7 @@ def _attribution_factor_section(
dates: List[str],
portfolio_returns: List[float],
benchmark_returns: Optional[List[float]],
- bars_per_year: int,
+ bars_per_year: float,
notes: List[str],
) -> Optional[Dict[str, Any]]:
"""Build the factor section: full-sample OLS plus rolling and cumulative series.
@@ -787,6 +787,17 @@ def _build_attribution_payload(run_dir: Path) -> Dict[str, Any]:
if isinstance(raw_codes, list):
codes = [str(code) for code in raw_codes if str(code).strip()]
bars_per_year = _attribution_bars_per_year(config.get("interval"))
+ # The run card records the actual source/served-spacing convention, which
+ # can differ from the requested interval (e.g. crypto trades 365 days/year).
+ card = _attribution_load_json(run_dir / "run_card.json") or {}
+ annualisation = card.get("annualisation")
+ if isinstance(annualisation, dict):
+ raw_rate = annualisation.get("bars_per_year")
+ recorded_rate = (
+ None if isinstance(raw_rate, bool) else _attribution_finite_float(raw_rate)
+ )
+ if recorded_rate is not None and recorded_rate > 0:
+ bars_per_year = recorded_rate
# Mirror the factor scan's symlink rejection: a symlinked artifacts dir is
# treated as having no readable artifacts rather than followed.
diff --git a/agent/tests/test_attribution_recorded_annualisation.py b/agent/tests/test_attribution_recorded_annualisation.py
new file mode 100644
index 000000000..f5e99c6e5
--- /dev/null
+++ b/agent/tests/test_attribution_recorded_annualisation.py
@@ -0,0 +1,77 @@
+"""Attribution uses the annualisation convention recorded by the real runner."""
+
+import csv
+import json
+from datetime import datetime, timedelta
+from pathlib import Path
+
+import pytest
+
+from backtest.run_card import write_run_card
+from src.api.attribution_core import _build_attribution_payload
+
+
[email protected]("recorded_rate", [365, 8760, None])
+def test_alpha_uses_recorded_run_rate(
+ tmp_path: Path, recorded_rate: int | None
+) -> None:
+ config = {"codes": ["BTC-USDT"], "source": "okx", "interval": "1D"}
+ (tmp_path / "config.json").write_text(json.dumps(config), encoding="utf-8")
+ artifacts = tmp_path / "artifacts"
+ artifacts.mkdir()
+ wealth = 1000.0
+ with (artifacts / "equity.csv").open("w", newline="", encoding="utf-8") as handle:
+ writer = csv.writer(handle)
+ writer.writerow(["timestamp", "ret", "benchmark_equity"])
+ for index in range(70):
+ # Independent exact linear model: portfolio = 0.001 + 2 * benchmark.
+ benchmark_return = (index % 7 - 3) / 1000 if index else 0.0
+ wealth *= 1 + benchmark_return
+ writer.writerow(
+ [
+ (datetime(2024, 1, 1) + timedelta(days=index)).isoformat(),
+ 0.001 + 2 * benchmark_return,
+ wealth,
+ ]
+ )
+ if recorded_rate is not None:
+ write_run_card(
+ tmp_path, {**config, "_run_card_bars_per_year": recorded_rate}, {}
+ )
+ factor = _build_attribution_payload(tmp_path)["factor"]
+ assert factor["alpha_per_period"] == pytest.approx(0.001, abs=1e-12)
+ expected = 0.001 * (recorded_rate if recorded_rate is not None else 252)
+ assert factor["alpha_annualized"] == pytest.approx(expected, abs=1e-12)
+ assert factor["rolling"]
+ for point in factor["rolling"]:
+ assert point["alpha_annualized"] == pytest.approx(expected, abs=1e-12)
+
+
+ "annualisation",
+ [
+ None,
+ "365",
+ [],
+ {},
+ {"bars_per_year": 0},
+ {"bars_per_year": -1},
+ {"bars_per_year": float("inf")},
+ {"bars_per_year": float("nan")},
+ {"bars_per_year": "invalid"},
+ {"bars_per_year": True},
+ ],
+)
+def test_unusable_recorded_rates_keep_legacy_fallback(
+ tmp_path: Path, annualisation
+) -> None:
+ test_alpha_uses_recorded_run_rate(tmp_path, None)
+ (tmp_path / "run_card.json").write_text(
+ json.dumps({"annualisation": annualisation}), encoding="utf-8"
+ )
+ factor = _build_attribution_payload(tmp_path)["factor"]
+ assert factor["alpha_annualized"] == pytest.approx(0.252, abs=1e-12)
+ assert all(
+ point["alpha_annualized"] == pytest.approx(0.252, abs=1e-12)
+ for point in factor["rolling"]
+ )