"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(fundamentals): asset growth uses a calendar year lag, not a row lag
Lanre Shittuf96aa87e6708
diff --git a/agent/backtest/loaders/_fundamental_schema.py b/agent/backtest/loaders/_fundamental_schema.pyindex c6612849f..b2c39b4d2 100644--- a/agent/backtest/loaders/_fundamental_schema.py+++ b/agent/backtest/loaders/_fundamental_schema.py@@ -25,6 +25,23 @@ def _safe_divide(numerator: pd.Series, denominator: pd.Series) -> pd.Series:return numerator.astype("float64") / clean_denominator+def _lag_365d(series: pd.Series) -> pd.Series:+ """Look up each row's value from ~1 year earlier via an as-of match.++ A row-positional .shift(1) only reproduces a year-over-year lag when+ every row already sits exactly one annual period apart. The real pipeline+ (backtest.loaders.fundamentals_loader.load_fundamental_panel) densifies+ every dependency to the daily price index before a derived field runs, so+ a positional shift there compares yesterday to today, not this year to+ last -- reading as a flat 0% on ordinary days and a spurious one-day spike+ whenever a new filing updates the forward-filled value.+ """+ shifted_index = series.index - pd.Timedelta(days=365)+ lagged = series.reindex(shifted_index, method="ffill")+ lagged.index = series.index+ return lagged++RAW_FIELDS: dict[str, RawFieldSpec] = {"revenue": {"statement": "IS",@@ -166,13 +183,15 @@ DERIVED_FIELDS: dict[str, DerivedFieldSpec] = {"dependencies": ["gross_profit", "total_assets"],"compute": lambda data: _safe_divide(data["gross_profit"], data["total_assets"]),},- # Annual semantics: input is one observation per annual period_end, sorted by- # period_end. Growth is period-over-period, value / value.shift(1) - 1.- # Quarterly or TTM callers must pre-aggregate before applying this formula.+ # Year-over-year growth, looked up by calendar lag (see _lag_365d) rather+ # than row position, since the real pipeline feeds this a daily-densified+ # panel, not one row per annual period_end."asset_growth": {"dependencies": ["total_assets"],- "compute": lambda data: data["total_assets"].astype("float64")- / data["total_assets"].astype("float64").shift(1)+ "compute": lambda data: _safe_divide(+ data["total_assets"].astype("float64"),+ _lag_365d(data["total_assets"].astype("float64")),+ )- 1,},"accruals": {diff --git a/agent/tests/test_fundamental_schema.py b/agent/tests/test_fundamental_schema.pyindex 620002051..e742a0d3b 100644--- a/agent/tests/test_fundamental_schema.py+++ b/agent/tests/test_fundamental_schema.py@@ -98,6 +98,26 @@ def test_asset_growth_uses_annual_period_over_period_semantics() -> None:pd.testing.assert_series_equal(result, expected, check_names=False)+def test_asset_growth_reads_a_year_ahead_on_a_daily_densified_panel() -> None:+ # load_fundamental_panel densifies every dependency to the daily price+ # index before a derived field runs (backtest.runner passes index=+ # panel["close"].index), so total_assets arrives here as a forward-filled+ # daily series, never one row per annual period_end. A row-positional+ # shift(1) then compares yesterday to today (flat 0% every ordinary day,+ # a spurious one-day spike on a filing update) instead of this year to+ # last -- the growth reading must stay at the true year-over-year value+ # across the whole year following a step, not just on the step's own day.+ dates = pd.date_range("2024-01-01", "2025-12-31", freq="D")+ total_assets = pd.Series(100.0, index=dates)+ total_assets.loc["2024-07-01":] = 110.0+ data = {"total_assets": pd.DataFrame({"AAPL": total_assets})}++ result = DERIVED_FIELDS["asset_growth"]["compute"](data)++ assert result.loc["2025-01-15", "AAPL"] == pytest.approx(0.10)+ assert result.loc["2025-06-25", "AAPL"] == pytest.approx(0.10)++def test_resolve_field_and_list_supported_fields() -> None:kind, raw_spec = resolve_field("revenue")assert kind == "raw"