Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(factors): raise GTJA #17 to the signed 5-day close change

Rudy Celekli6892d1cb7900
diff --git a/agent/src/factors/zoo/gtja191/alpha_017.py b/agent/src/factors/zoo/gtja191/alpha_017.py
index 03d177235..b43eeefe2 100644
--- a/agent/src/factors/zoo/gtja191/alpha_017.py
+++ b/agent/src/factors/zoo/gtja191/alpha_017.py
@@ -43,7 +43,7 @@ __alpha_meta__ = {
"frequency": ["1d"],
"decay_horizon": 15,
"min_warmup_bars": 16,
- "notes": 'rank(vwap - 15d max(vwap)) is non-positive; we use signed_power for safety.',
+ "notes": 'The base is a percentile rank in (0, 1], so a negative 5-day close change is a real power, not a complex one.',
}
def compute(panel: dict) -> pd.DataFrame:
@@ -52,9 +52,11 @@ def compute(panel: dict) -> pd.DataFrame:
vw = vwap(panel, "equity_cn")
base = rank(vw - ts_max(vw, 15))
expo = delta(c, 5)
- # Combine via signed_power(base, mean_exp): use elementwise sign-preserving |base|**expo proxy
- out_arr = np.sign(base.to_numpy(dtype=float, na_value=np.nan)) * np.power(
- np.abs(base.to_numpy(dtype=float, na_value=np.nan)),
- np.abs(expo.to_numpy(dtype=float, na_value=np.nan)),
+ # 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),
)
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
new file mode 100644
index 000000000..c35bc6079
--- /dev/null
+++ b/agent/tests/factors/test_gtja191_alpha017_power.py
@@ -0,0 +1,34 @@
+"""GTJA #17 raises the percentile rank to the signed 5-day close change.
+
+A rank of 0.5 and a close change of -2 is 0.5 ** -2 = 4. Taking the absolute
+value of the exponent reported 0.25.
+"""
+
+from __future__ import annotations
+
+import numpy as np
+import pandas as pd
+
+from src.factors.zoo.gtja191 import alpha_017
+
+
+def test_negative_close_change_inverts_the_rank_power():
+ idx = pd.bdate_range("2024-01-01", periods=20)
+ close = pd.DataFrame(10.0, index=idx, columns=["A", "B"])
+ close.iloc[-1] = [8.0, 12.0]
+ vwap = pd.DataFrame(10.0, index=idx, columns=["A", "B"])
+ vwap.iloc[-1] = [8.0, 10.0]
+ panel = {
+ "close": close,
+ "vwap": vwap,
+ "volume": pd.DataFrame(1.0, index=idx, columns=["A", "B"]),
+ "amount": pd.DataFrame(1.0, index=idx, columns=["A", "B"]),
+ }
+
+ got = alpha_017.compute(panel).iloc[-1]
+
+ # vwap - 15-day max is -2 and 0, so the percentile ranks are 0.5 and 1.
+ # Deltas are -2 and +2.
+ assert got["A"] == 4.0
+ assert got["B"] == 1.0
+ assert not np.isclose(got["A"], 0.25)