"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(fundamentals): stop ttm sums spanning a missing quarter (#2042)
Rudy Celekli4a2396a54cd9
diff --git a/agent/backtest/loaders/fundamentals_loader.py b/agent/backtest/loaders/fundamentals_loader.pyindex 9afb2c8ac..d9e4f3cac 100644--- a/agent/backtest/loaders/fundamentals_loader.py+++ b/agent/backtest/loaders/fundamentals_loader.py@@ -310,6 +310,10 @@ def _is_flow_concept_series(concepts: Iterable[str]) -> bool:def _to_ttm_flow_series(df: pd.DataFrame) -> pd.DataFrame:ttm = df.sort_values("period_end").copy()ttm["value"] = ttm["value"].rolling(window=4, min_periods=4).sum()+ # Four rows are a trailing year only when they are four adjacent quarters;+ # a quarter missing from the facts would otherwise sum a 15-month window.+ window_days = (ttm["period_end"] - ttm["period_start"].shift(3)).dt.days+ ttm.loc[~window_days.between(*ANNUAL_SPAN_DAYS), "value"] = float("nan")filed_values = list(ttm["filed"])ttm["filed"] = [pd.NaT if pos < 3 else max(filed_values[pos - 3 : pos + 1])diff --git a/agent/tests/test_fundamentals_ttm_quarter_gap.py b/agent/tests/test_fundamentals_ttm_quarter_gap.pynew file mode 100644index 000000000..b51aa1ef5--- /dev/null+++ b/agent/tests/test_fundamentals_ttm_quarter_gap.py@@ -0,0 +1,48 @@+"""TTM flow sums need four adjacent quarters, not any four quarterly rows.++IPO-year shape: the first 10-Q is for Q2, so the 10-K full year has only two+quarters inside it and fiscal Q4 cannot be synthesized. The next four rows+(Q2, Q3, Q1, Q2) cover 15 months with Q4 missing; summing them is not TTM.+"""++from __future__ import annotations++import pandas as pd+import pytest++from tests.test_fundamentals_pit import _fact_row, _facts, _install_schema_stub, _patch_sec+from backtest.loaders import fundamentals_loader+++def _load(index):+ return fundamentals_loader.load_fundamental_panel(+ ["NEWCO"],+ ["revenue"],+ str(index[0].date()),+ str(index[-1].date()),+ freq="ttm",+ index=index,+ )["revenue"]["NEWCO"]+++def test_missing_quarter_does_not_yield_a_fifteen_month_ttm(monkeypatch: pytest.MonkeyPatch):+ _install_schema_stub(monkeypatch)+ rows = [+ _fact_row("2022-06-30", "2022-08-10", 20.0, start="2022-04-01"),+ _fact_row("2022-09-30", "2022-11-10", 30.0, start="2022-07-01"),+ _fact_row("2022-12-31", "2023-03-01", 110.0, form="10-K", start="2022-01-01"),+ _fact_row("2023-03-31", "2023-05-10", 25.0, start="2023-01-01"),+ _fact_row("2023-06-30", "2023-08-10", 35.0, start="2023-04-01"),+ _fact_row("2023-09-30", "2023-11-10", 45.0, start="2023-07-01"),+ _fact_row("2023-12-31", "2024-03-01", 150.0, form="10-K", start="2023-01-01"),+ ]+ _patch_sec(monkeypatch, {"NEWCO": _facts({"Revenues": rows})})+ index = pd.to_datetime(["2023-08-10", "2023-11-10", "2024-03-01"])++ series = _load(index)++ # Q2'22 + Q3'22 + Q1'23 + Q2'23 skips Q4'22: no trailing year exists yet.+ assert pd.isna(series.loc[pd.Timestamp("2023-08-10")])+ assert pd.isna(series.loc[pd.Timestamp("2023-11-10")])+ # FY2023's Q4 is synthesized (150 - 105 = 45); Q1..Q4 2023 is a real TTM.+ assert series.loc[pd.Timestamp("2024-03-01")] == pytest.approx(150.0)