"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(factors): give GTJA #55 the swing index's third denominator branch
Rudy Celekli8862a07b49bb
diff --git a/agent/src/factors/zoo/gtja191/alpha_055.py b/agent/src/factors/zoo/gtja191/alpha_055.pyindex 8e6e19ca2..3a162540a 100644--- a/agent/src/factors/zoo/gtja191/alpha_055.py+++ b/agent/src/factors/zoo/gtja191/alpha_055.py@@ -6,7 +6,7 @@# ============================================================"""GTJA Alpha #55.-Formula: SUM(16*(CLOSE-DELAY(CLOSE,1)+(CLOSE-OPEN)/2+DELAY(CLOSE,1)-DELAY(OPEN,1))/((ABS(HIGH-DELAY(CLOSE,1))>ABS(LOW-DELAY(CLOSE,1)) && ABS(HIGH-DELAY(CLOSE,1))>ABS(HIGH-DELAY(LOW,1))?ABS(HIGH-DELAY(CLOSE,1))+ABS(LOW-DELAY(CLOSE,1))/2+ABS(DELAY(CLOSE,1)-DELAY(OPEN,1))/4:ABS(LOW-DELAY(CLOSE,1))+ABS(HIGH-DELAY(CLOSE,1))/2+ABS(DELAY(CLOSE,1)-DELAY(OPEN,1))/4))*MAX(ABS(HIGH-DELAY(CLOSE,1)),ABS(LOW-DELAY(CLOSE,1))),20)+Formula: SUM(16*(CLOSE-DELAY(CLOSE,1)+(CLOSE-OPEN)/2+DELAY(CLOSE,1)-DELAY(OPEN,1))/((ABS(HIGH-DELAY(CLOSE,1))>ABS(LOW-DELAY(CLOSE,1)) && ABS(HIGH-DELAY(CLOSE,1))>ABS(HIGH-DELAY(LOW,1))?ABS(HIGH-DELAY(CLOSE,1))+ABS(LOW-DELAY(CLOSE,1))/2+ABS(DELAY(CLOSE,1)-DELAY(OPEN,1))/4:(ABS(LOW-DELAY(CLOSE,1))>ABS(HIGH-DELAY(LOW,1)) && ABS(LOW-DELAY(CLOSE,1))>ABS(HIGH-DELAY(CLOSE,1))?ABS(LOW-DELAY(CLOSE,1))+ABS(HIGH-DELAY(CLOSE,1))/2+ABS(DELAY(CLOSE,1)-DELAY(OPEN,1))/4:ABS(HIGH-DELAY(LOW,1))+ABS(DELAY(CLOSE,1)-DELAY(OPEN,1))/4)))*MAX(ABS(HIGH-DELAY(CLOSE,1)),ABS(LOW-DELAY(CLOSE,1))),20)Source: 国泰君安 191 alpha 研报 (2014), alpha 55."""from __future__ import annotations@@ -34,7 +34,7 @@ from src.factors.base import (__alpha_meta__ = {"id": "gtja191_055","theme": ['microstructure'],- "formula_latex": 'SUM(16*(CLOSE-DELAY(CLOSE,1)+(CLOSE-OPEN)/2+DELAY(CLOSE,1)-DELAY(OPEN,1))/((ABS(HIGH-DELAY(CLOSE,1))>ABS(LOW-DELAY(CLOSE,1)) && ABS(HIGH-DELAY(CLOSE,1))>ABS(HIGH-DELAY(LOW,1))?ABS(HIGH-DELAY(CLOSE,1))+ABS(LOW-DELAY(CLOSE,1))/2+ABS(DELAY(CLOSE,1)-DELAY(OPEN,1))/4:ABS(LOW-DELAY(CLOSE,1))+ABS(HIGH-DELAY(CLOSE,1))/2+ABS(DELAY(CLOSE,1)-DELAY(OPEN,1))/4))*MAX(ABS(HIGH-DELAY(CLOSE,1)),ABS(LOW-DELAY(CLOSE,1))),20)',+ "formula_latex": 'SUM(16*(CLOSE-DELAY(CLOSE,1)+(CLOSE-OPEN)/2+DELAY(CLOSE,1)-DELAY(OPEN,1))/((ABS(HIGH-DELAY(CLOSE,1))>ABS(LOW-DELAY(CLOSE,1)) && ABS(HIGH-DELAY(CLOSE,1))>ABS(HIGH-DELAY(LOW,1))?ABS(HIGH-DELAY(CLOSE,1))+ABS(LOW-DELAY(CLOSE,1))/2+ABS(DELAY(CLOSE,1)-DELAY(OPEN,1))/4:(ABS(LOW-DELAY(CLOSE,1))>ABS(HIGH-DELAY(LOW,1)) && ABS(LOW-DELAY(CLOSE,1))>ABS(HIGH-DELAY(CLOSE,1))?ABS(LOW-DELAY(CLOSE,1))+ABS(HIGH-DELAY(CLOSE,1))/2+ABS(DELAY(CLOSE,1)-DELAY(OPEN,1))/4:ABS(HIGH-DELAY(LOW,1))+ABS(DELAY(CLOSE,1)-DELAY(OPEN,1))/4)))*MAX(ABS(HIGH-DELAY(CLOSE,1)),ABS(LOW-DELAY(CLOSE,1))),20)',"columns_required": ['close', 'high', 'low', 'open'],"extras_required": [],"requires_sector": False,@@ -57,10 +57,13 @@ def compute(panel: dict) -> pd.DataFrame:a = (h - pc).abs()b = (l - pc).abs()d = (pc - po).abs()- cond1 = (a > b) & (a > (h - pl).abs())+ e = (h - pl).abs()+ cond1 = (a > b) & (a > e)+ cond2 = (b > e) & (b > a)branch1 = a + b / 2.0 + d / 4.0branch2 = b + a / 2.0 + d / 4.0- denom = branch1.where(cond1, branch2)+ branch3 = e + d / 4.0+ denom = branch3.where(~cond2, branch2).where(~cond1, branch1)factor = pd.DataFrame(np.maximum(a.to_numpy(), b.to_numpy()), index=c.index, columns=c.columns)per_day = safe_div(numer, denom) * factorreturn per_day.rolling(20, min_periods=20).sum()diff --git a/agent/tests/factors/test_gtja191_alpha055_swing_index.py b/agent/tests/factors/test_gtja191_alpha055_swing_index.pynew file mode 100644index 000000000..b9d3ba318--- /dev/null+++ b/agent/tests/factors/test_gtja191_alpha055_swing_index.py@@ -0,0 +1,45 @@+"""GTJA #55 is the 20-day sum of the swing-index term that #137 reports per+day, so the two must agree, including on bars where |HIGH - DELAY(LOW,1)| is+the widest range and the third denominator branch applies."""++from __future__ import annotations++import numpy as np+import pandas as pd++from src.factors.zoo.gtja191 import alpha_055, alpha_137+++def _panel() -> dict[str, pd.DataFrame]:+ rng = np.random.default_rng(7)+ idx = pd.bdate_range("2024-01-01", periods=80)+ cols = ["A", "B", "C"]+ close = pd.DataFrame(100 + rng.normal(0, 1, (80, 3)).cumsum(axis=0), index=idx, columns=cols)+ open_ = close.shift(1).fillna(close) + rng.normal(0, 0.3, (80, 3))+ high = np.maximum(open_, close) + rng.uniform(0.1, 1.0, (80, 3))+ low = np.minimum(open_, close) - rng.uniform(0.1, 1.0, (80, 3))+ # A tight bar that closes near its high, followed by a bar whose high sits+ # just above that close: |H - prior L| is then the widest of the three.+ for day in range(10, 80, 7):+ prev = idx[day - 1]+ low.loc[prev] = close.loc[prev] - 3.0+ high.loc[prev] = close.loc[prev] + 0.1+ open_.loc[idx[day]] = close.loc[prev]+ high.loc[idx[day]] = close.loc[prev] + 0.5+ low.loc[idx[day]] = close.loc[prev] - 0.2+ close.loc[idx[day]] = close.loc[prev] + 0.3+ return {"open": open_, "high": high, "low": low, "close": close}+++def test_third_branch_is_exercised():+ p = _panel()+ pc, pl = p["close"].shift(1), p["low"].shift(1)+ a, b, e = (p["high"] - pc).abs(), (p["low"] - pc).abs(), (p["high"] - pl).abs()+ assert ((e >= a) & (e >= b)).to_numpy().sum() >= 10+++def test_alpha055_is_rolling_sum_of_alpha137():+ p = _panel()+ expected = alpha_137.compute(p).rolling(20, min_periods=20).sum()+ got = alpha_055.compute(p)+ pd.testing.assert_frame_equal(got, expected, check_exact=False, rtol=1e-9)