Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(factors): fix gtja191_038 NaN propagation

Lanre Shittu05eb51eb86f0
diff --git a/agent/src/factors/zoo/gtja191/alpha_038.py b/agent/src/factors/zoo/gtja191/alpha_038.py
index b1a6d62be..60c73d7b3 100644
--- a/agent/src/factors/zoo/gtja191/alpha_038.py
+++ b/agent/src/factors/zoo/gtja191/alpha_038.py
@@ -49,4 +49,8 @@ def compute(panel: dict) -> pd.DataFrame:
h = panel["high"]
m20 = ts_mean(h, 20)
cond = m20 < h
- return (-1.0 * delta(h, 2)).where(cond, 0.0)
+ out = (-1.0 * delta(h, 2)).where(cond, 0.0)
+ # A NaN comparison is False, not NaN, so cond is False (never NaN) for
+ # every bar in m20's warmup or a gap's 20-day window, and out falls
+ # through to the finite 0.0 branch instead of NaN.
+ return out.where(m20.notna())
diff --git a/agent/tests/factors/test_gtja191_alpha038_nan_propagation.py b/agent/tests/factors/test_gtja191_alpha038_nan_propagation.py
new file mode 100644
index 000000000..41beda82d
--- /dev/null
+++ b/agent/tests/factors/test_gtja191_alpha038_nan_propagation.py
@@ -0,0 +1,61 @@
+"""Regression: gtja191_038 must propagate NaN through a data gap instead
+of fabricating a 0.0 "below MA20" signal.
+
+compute() builds m20 as a 20-day rolling mean that correctly goes NaN
+across a gap, but out.where(cond, 0.0) falls through to the hard-coded
+0.0 branch whenever m20 is NaN, since a NaN comparison evaluates False,
+not NaN. Same bug class already fixed in gtja191_003/004/059/069.
+"""
+
+from __future__ import annotations
+
+import numpy as np
+import pandas as pd
+
+from src.factors.registry import Registry
+
+N_ROWS = 60
+GAP_ROW = 30 # past min_warmup_bars=21
+WINDOW = 20
+
+
+def _panel_with_one_gap() -> dict[str, pd.DataFrame]:
+ rng = np.random.default_rng(2)
+ idx = pd.date_range("2024-01-01", periods=N_ROWS, freq="D")
+ cols = ["SYM0", "SYM1"]
+
+ high = pd.DataFrame(
+ 100.0 + np.cumsum(rng.normal(0.0, 1.0, size=(N_ROWS, 2)), axis=0),
+ index=idx,
+ columns=cols,
+ )
+ high.loc[idx[GAP_ROW], "SYM1"] = np.nan
+
+ return {"high": high}
+
+
+def test_gap_stays_nan_through_the_full_affected_window():
+ panel = _panel_with_one_gap()
+ out = Registry().compute("gtja191_038", panel)
+
+ affected = out["SYM1"].iloc[GAP_ROW : GAP_ROW + WINDOW]
+ assert affected.isna().all(), (
+ f"gtja191_038: every window still touching the gap must stay NaN, "
+ f"got {affected.tolist()}"
+ )
+
+ after = out["SYM1"].iloc[GAP_ROW + WINDOW]
+ assert not pd.isna(
+ after
+ ), "gtja191_038: NaN must not leak past the gap's own window"
+
+
+def test_unaffected_symbol_still_computes_real_values():
+ panel = _panel_with_one_gap()
+ out = Registry().compute("gtja191_038", panel)
+
+ unaffected = out["SYM0"].iloc[25:]
+ assert not unaffected.isna().any(), (
+ "gtja191_038: a symbol with no gap must not pick up stray NaN "
+ "from another symbol's column"
+ )