Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(factors): give GTJA #101 the report's HIGH/VWAP blend against raw volume

Rudy Celekli5a2581effd66
diff --git a/agent/src/factors/zoo/gtja191/alpha_101.py b/agent/src/factors/zoo/gtja191/alpha_101.py
index ce55b044a..a82d4b442 100644
--- a/agent/src/factors/zoo/gtja191/alpha_101.py
+++ b/agent/src/factors/zoo/gtja191/alpha_101.py
@@ -7,7 +7,7 @@
"""GTJA Alpha 101 (国泰君安 191 短周期交易型 alpha 因子, 2014).
Formula (verbatim from the report):
- ((RANK(CORR(close, SUM(MEAN(volume,30),37), 15)) < RANK(CORR(RANK(high), RANK(MEAN(volume,10)),11))) * -1)
+ ((RANK(CORR(CLOSE, SUM(MEAN(VOLUME,30), 37), 15)) < RANK(CORR(RANK(((HIGH * 0.1) + (VWAP * 0.9))), RANK(VOLUME), 11))) * -1)
Notes: Inequality cast to float via .astype('float64').
"""
@@ -33,6 +33,7 @@ from src.factors.base import (
ts_min,
ts_rank,
ts_std,
+ vwap,
)
ALPHA_ID = "gtja191_101"
@@ -40,7 +41,7 @@ ALPHA_ID = "gtja191_101"
__alpha_meta__ = {
'id': 'gtja191_101',
'theme': ['volume', 'momentum'],
- 'formula_latex': '((rank(ts\\_corr(close, sum(ts\\_mean(volume,30),37), 15)) < rank(ts\\_corr(rank(high), rank(ts\\_mean(volume,10)), 11))) * -1)',
+ 'formula_latex': '((rank(ts\\_corr(close, sum(ts\\_mean(volume,30),37), 15)) < rank(ts\\_corr(rank(high*0.1 + vwap*0.9), rank(volume), 11))) * -1)',
'columns_required': ['open', 'high', 'low', 'close', 'volume', 'amount'],
'extras_required': [],
'universe': ['equity_cn'],
@@ -64,8 +65,9 @@ def compute(panel):
h = panel["high"]
v = panel["volume"]
left = rank(ts_corr(c, ts_mean(v, 30).rolling(37).sum(), 15))
- right = rank(ts_corr(rank(h), rank(ts_mean(v, 10)), 11))
+ vw = vwap(panel, "equity_cn")
+ right = rank(ts_corr(rank(h * 0.1 + vw * 0.9), rank(v), 11))
out = (left < right).astype("float64") * -1.0
# Reach of each input through the nested windows; a gap inside it is not a verdict (#1463).
- # v: mean 30 + sum 37 + corr 15; c: corr 15; h: corr 11.
- return out.where(observed_over((v, 80), (c, 15), (h, 11)))
+ # v: mean 30 + sum 37 + corr 15; c: corr 15; h, vwap: corr 11.
+ return out.where(observed_over((v, 80), (c, 15), (h, 11), (vw, 11)))
diff --git a/agent/tests/factors/fixtures/goldens/gtja191_101.csv b/agent/tests/factors/fixtures/goldens/gtja191_101.csv
index 9eeb8a684..75ff94228 100644
--- a/agent/tests/factors/fixtures/goldens/gtja191_101.csv
+++ b/agent/tests/factors/fixtures/goldens/gtja191_101.csv
@@ -80,30 +80,30 @@
2024-03-19,,,,,
2024-03-20,-1.0,-0.0,-0.0,-0.0,-0.0
2024-03-21,-1.0,-0.0,-0.0,-0.0,-0.0
-2024-03-22,-1.0,-0.0,-0.0,-0.0,-0.0
+2024-03-22,-1.0,-0.0,-1.0,-0.0,-0.0
2024-03-23,-1.0,-0.0,-0.0,-0.0,-0.0
-2024-03-24,-1.0,-0.0,-0.0,-0.0,-0.0
-2024-03-25,-0.0,-0.0,-0.0,-0.0,-0.0
+2024-03-24,-1.0,-0.0,-1.0,-0.0,-0.0
+2024-03-25,-0.0,-0.0,-1.0,-0.0,-0.0
2024-03-26,-0.0,-1.0,-0.0,-0.0,-0.0
2024-03-27,-0.0,-1.0,-0.0,-0.0,-0.0
2024-03-28,-0.0,-1.0,-0.0,-0.0,-0.0
2024-03-29,-0.0,-1.0,-0.0,-0.0,-0.0
2024-03-30,-0.0,-1.0,-0.0,-0.0,-0.0
-2024-03-31,-1.0,-0.0,-0.0,-0.0,-1.0
+2024-03-31,-1.0,-1.0,-1.0,-0.0,-0.0
2024-04-01,-1.0,-0.0,-0.0,-0.0,-1.0
2024-04-02,-1.0,-0.0,-0.0,-0.0,-1.0
2024-04-03,-1.0,-0.0,-0.0,-0.0,-1.0
2024-04-04,-1.0,-0.0,-0.0,-0.0,-1.0
2024-04-05,-1.0,-0.0,-0.0,-0.0,-1.0
-2024-04-06,-1.0,-0.0,-0.0,-0.0,-0.0
-2024-04-07,-1.0,-0.0,-0.0,-1.0,-0.0
-2024-04-08,-0.0,-1.0,-0.0,-1.0,-0.0
-2024-04-09,-0.0,-1.0,-0.0,-1.0,-0.0
-2024-04-10,-0.0,-1.0,-0.0,-1.0,-0.0
-2024-04-11,-0.0,-1.0,-0.0,-1.0,-0.0
-2024-04-12,-0.0,-1.0,-0.0,-1.0,-0.0
-2024-04-13,-0.0,-1.0,-0.0,-0.0,-1.0
-2024-04-14,-0.0,-1.0,-0.0,-0.0,-0.0
+2024-04-06,-1.0,-0.0,-0.0,-0.0,-1.0
+2024-04-07,-1.0,-0.0,-0.0,-0.0,-1.0
+2024-04-08,-1.0,-0.0,-0.0,-0.0,-0.0
+2024-04-09,-1.0,-1.0,-0.0,-0.0,-0.0
+2024-04-10,-0.0,-1.0,-0.0,-0.0,-0.0
+2024-04-11,-1.0,-1.0,-0.0,-0.0,-0.0
+2024-04-12,-1.0,-0.0,-0.0,-0.0,-0.0
+2024-04-13,-1.0,-1.0,-0.0,-1.0,-0.0
+2024-04-14,-1.0,-0.0,-0.0,-0.0,-0.0
2024-04-15,-1.0,-0.0,-0.0,-0.0,-0.0
2024-04-16,-1.0,-0.0,-0.0,-0.0,-0.0
2024-04-17,-1.0,-0.0,-0.0,-0.0,-0.0
@@ -111,50 +111,50 @@
2024-04-19,-1.0,-0.0,-0.0,-0.0,-0.0
2024-04-20,-1.0,-0.0,-0.0,-0.0,-0.0
2024-04-21,-1.0,-0.0,-0.0,-0.0,-0.0
-2024-04-22,-0.0,-0.0,-0.0,-0.0,-0.0
-2024-04-23,-0.0,-0.0,-0.0,-0.0,-0.0
-2024-04-24,-0.0,-0.0,-0.0,-0.0,-0.0
+2024-04-22,-1.0,-0.0,-0.0,-0.0,-0.0
+2024-04-23,-1.0,-0.0,-0.0,-0.0,-0.0
+2024-04-24,-1.0,-0.0,-0.0,-0.0,-0.0
2024-04-25,-0.0,-0.0,-0.0,-0.0,-0.0
-2024-04-26,-0.0,-0.0,-0.0,-0.0,-1.0
-2024-04-27,-1.0,-0.0,-0.0,-0.0,-0.0
+2024-04-26,-1.0,-0.0,-0.0,-0.0,-0.0
+2024-04-27,-0.0,-0.0,-0.0,-0.0,-1.0
2024-04-28,-1.0,-0.0,-0.0,-0.0,-1.0
2024-04-29,-0.0,-0.0,-0.0,-0.0,-1.0
2024-04-30,-0.0,-0.0,-0.0,-0.0,-1.0
2024-05-01,-0.0,-0.0,-0.0,-0.0,-1.0
-2024-05-02,-1.0,-0.0,-0.0,-0.0,-1.0
-2024-05-03,-1.0,-0.0,-0.0,-0.0,-1.0
+2024-05-02,-0.0,-0.0,-0.0,-0.0,-1.0
+2024-05-03,-0.0,-0.0,-0.0,-0.0,-1.0
2024-05-04,-1.0,-0.0,-0.0,-0.0,-1.0
2024-05-05,-1.0,-0.0,-0.0,-0.0,-1.0
2024-05-06,-1.0,-0.0,-0.0,-0.0,-1.0
2024-05-07,-1.0,-0.0,-0.0,-0.0,-1.0
-2024-05-08,-1.0,-0.0,-0.0,-0.0,-1.0
-2024-05-09,-1.0,-0.0,-0.0,-0.0,-1.0
-2024-05-10,-1.0,-0.0,-0.0,-0.0,-1.0
-2024-05-11,-1.0,-0.0,-0.0,-0.0,-1.0
+2024-05-08,-0.0,-0.0,-0.0,-0.0,-0.0
+2024-05-09,-0.0,-0.0,-0.0,-0.0,-0.0
+2024-05-10,-0.0,-0.0,-0.0,-0.0,-0.0
+2024-05-11,-0.0,-0.0,-0.0,-0.0,-0.0
2024-05-12,-0.0,-0.0,-0.0,-0.0,-0.0
2024-05-13,-0.0,-0.0,-0.0,-0.0,-0.0
2024-05-14,-0.0,-0.0,-0.0,-0.0,-0.0
-2024-05-15,-0.0,-0.0,-0.0,-0.0,-0.0
-2024-05-16,-0.0,-0.0,-0.0,-0.0,-0.0
-2024-05-17,-0.0,-0.0,-0.0,-0.0,-1.0
-2024-05-18,-0.0,-0.0,-0.0,-0.0,-1.0
-2024-05-19,-0.0,-0.0,-0.0,-0.0,-1.0
-2024-05-20,-0.0,-1.0,-0.0,-0.0,-1.0
-2024-05-21,-0.0,-1.0,-0.0,-0.0,-1.0
-2024-05-22,-0.0,-0.0,-0.0,-0.0,-1.0
-2024-05-23,-0.0,-1.0,-0.0,-0.0,-1.0
+2024-05-15,-1.0,-0.0,-1.0,-0.0,-0.0
+2024-05-16,-1.0,-0.0,-1.0,-0.0,-0.0
+2024-05-17,-1.0,-1.0,-1.0,-0.0,-0.0
+2024-05-18,-1.0,-1.0,-1.0,-0.0,-0.0
+2024-05-19,-0.0,-1.0,-1.0,-0.0,-0.0
+2024-05-20,-0.0,-0.0,-1.0,-0.0,-0.0
+2024-05-21,-0.0,-0.0,-1.0,-0.0,-0.0
+2024-05-22,-0.0,-1.0,-1.0,-0.0,-0.0
+2024-05-23,-0.0,-1.0,-1.0,-0.0,-0.0
2024-05-24,-0.0,-1.0,-0.0,-0.0,-1.0
2024-05-25,-0.0,-1.0,-0.0,-0.0,-1.0
2024-05-26,-0.0,-1.0,-0.0,-0.0,-1.0
2024-05-27,-0.0,-1.0,-0.0,-0.0,-1.0
2024-05-28,-0.0,-1.0,-0.0,-0.0,-1.0
2024-05-29,-0.0,-1.0,-0.0,-0.0,-1.0
-2024-05-30,-0.0,-1.0,-0.0,-0.0,-1.0
-2024-05-31,-0.0,-1.0,-0.0,-0.0,-1.0
-2024-06-01,-0.0,-1.0,-0.0,-0.0,-1.0
+2024-05-30,-0.0,-1.0,-0.0,-0.0,-0.0
+2024-05-31,-0.0,-1.0,-0.0,-0.0,-0.0
+2024-06-01,-0.0,-1.0,-0.0,-0.0,-0.0
2024-06-02,-0.0,-1.0,-0.0,-0.0,-1.0
-2024-06-03,-0.0,-1.0,-0.0,-0.0,-0.0
-2024-06-04,-0.0,-0.0,-1.0,-0.0,-0.0
+2024-06-03,-0.0,-0.0,-0.0,-0.0,-0.0
+2024-06-04,-1.0,-0.0,-1.0,-0.0,-0.0
2024-06-05,-0.0,-0.0,-1.0,-0.0,-0.0
2024-06-06,-0.0,-0.0,-1.0,-0.0,-0.0
2024-06-07,-0.0,-0.0,-1.0,-0.0,-0.0
diff --git a/agent/tests/factors/test_gtja191_alpha101_formula.py b/agent/tests/factors/test_gtja191_alpha101_formula.py
new file mode 100644
index 000000000..a24a4c3bd
--- /dev/null
+++ b/agent/tests/factors/test_gtja191_alpha101_formula.py
@@ -0,0 +1,40 @@
+"""GTJA #101's right leg correlates RANK(HIGH*0.1 + VWAP*0.9) with RANK(VOLUME).
+
+Report: ((RANK(CORR(CLOSE, SUM(MEAN(VOLUME,30),37),15))
+ < RANK(CORR(RANK(((HIGH*0.1)+(VWAP*0.9))), RANK(VOLUME), 11))) * -1)
+"""
+
+from __future__ import annotations
+
+import numpy as np
+import pandas as pd
+
+from src.factors.base import rank, ts_corr, ts_mean
+from src.factors.zoo.gtja191 import alpha_101
+
+
+def _panel() -> dict[str, pd.DataFrame]:
+ rng = np.random.default_rng(101)
+ n = 160
+ idx = pd.bdate_range("2024-01-01", periods=n)
+ cols = [f"S{i}" for i in range(12)]
+ close = pd.DataFrame(20 + rng.normal(0, 0.5, (n, 12)).cumsum(axis=0), index=idx, columns=cols)
+ high = close + rng.uniform(0.05, 0.5, (n, 12))
+ low = close - rng.uniform(0.05, 0.5, (n, 12))
+ vw = low + (high - low) * rng.uniform(0.2, 0.8, (n, 12))
+ vol = pd.DataFrame(rng.uniform(1e5, 5e5, (n, 12)), index=idx, columns=cols)
+ return {"open": close.shift(1).fillna(close), "high": high, "low": low,
+ "close": close, "volume": vol, "amount": vw * vol * 0.1, "vwap": vw}
+
+
+def test_right_leg_uses_high_vwap_blend_against_raw_volume():
+ p = _panel()
+ c, h, v, vw = p["close"], p["high"], p["volume"], p["vwap"]
+ left = rank(ts_corr(c, ts_mean(v, 30).rolling(37).sum(), 15))
+ right = rank(ts_corr(rank(h * 0.1 + vw * 0.9), rank(v), 11))
+ expected = (left < right).astype("float64") * -1.0
+
+ got = alpha_101.compute(p)
+
+ tail = slice(90, None)
+ pd.testing.assert_frame_equal(got.iloc[tail], expected.iloc[tail])