Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(factors): keep GTJA #17 missing while either power operand is missing

Haozhe Wu15a88f0c765c
diff --git a/agent/src/factors/zoo/gtja191/alpha_017.py b/agent/src/factors/zoo/gtja191/alpha_017.py
index 891f4c48a..682cb179a 100644
--- a/agent/src/factors/zoo/gtja191/alpha_017.py
+++ b/agent/src/factors/zoo/gtja191/alpha_017.py
@@ -43,8 +43,10 @@ def compute(panel: dict) -> pd.DataFrame:
# rank() is a percentile in (0, 1], so the report's power is real even when
# the 5-day close change is negative. Dropping the sign of that change
# turned 0.5 ** -2 into 0.25 instead of 4.
- out_arr = np.power(
- base.to_numpy(dtype=float, na_value=np.nan),
- expo.to_numpy(dtype=float, na_value=np.nan),
- )
+ base_arr = base.to_numpy(dtype=float, na_value=np.nan)
+ expo_arr = expo.to_numpy(dtype=float, na_value=np.nan)
+ out_arr = np.power(base_arr, expo_arr)
+ # IEEE pow returns 1 for NaN ** 0 and 1 ** NaN, which would turn an
+ # incomplete 15-bar VWAP window or 5-bar close change into a signal.
+ out_arr[np.isnan(base_arr) | np.isnan(expo_arr)] = np.nan
return pd.DataFrame(out_arr, index=c.index, columns=c.columns)
diff --git a/agent/tests/factors/test_gtja191_alpha017_power.py b/agent/tests/factors/test_gtja191_alpha017_power.py
index bcd343f47..bc8066532 100644
--- a/agent/tests/factors/test_gtja191_alpha017_power.py
+++ b/agent/tests/factors/test_gtja191_alpha017_power.py
@@ -35,3 +35,35 @@ def test_negative_close_change_inverts_the_rank_power(width):
assert got["A"] == pytest.approx((1.0 / width) ** -2)
assert got["B"] == 1.0
assert not np.isclose(got["A"], 0.25)
+
+
+def test_incomplete_windows_stay_missing_instead_of_powering_to_one():
+ """NaN ** 0 and 1 ** NaN are both 1 under IEEE pow.
+
+ A flat close makes the 5-day change exactly 0 while the 15-bar VWAP max
+ is still warming up, and the top-ranked name has a base of 1 while the
+ close change is still undefined; neither bar may report 1.0.
+ """
+ idx = pd.bdate_range("2024-01-01", periods=20)
+ columns = ["A", "B", "C"]
+ close = pd.DataFrame(10.0, index=idx, columns=columns)
+ # A missing close on bar 17 leaves that bar's 5-day change undefined
+ # while every VWAP window is complete; C ranks first, a base of 1.
+ close.iloc[16] = np.nan
+ vwap = pd.DataFrame([[9.0, 10.0, 11.0]] * len(idx), index=idx, columns=columns)
+ # A and B trade below their 15-bar VWAP high on that bar, C at it.
+ vwap.iloc[16, [0, 1]] = [8.0, 9.5]
+ panel = {
+ "close": close,
+ "vwap": vwap,
+ "volume": pd.DataFrame(1.0, index=idx, columns=columns),
+ "amount": pd.DataFrame(1.0, index=idx, columns=columns),
+ }
+
+ got = alpha_017.compute(panel)
+
+ # ts_max(VWAP, 15) is undefined on the first 14 bars, so is the base.
+ assert got.iloc[:14].isna().all().all()
+ assert got.iloc[16].isna().all()
+ # Elsewhere the window and the 5-day change are both complete.
+ assert got.iloc[[14, 15, 17, 18, 19]].notna().all().all()