Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(factors): GTJA HIGHDAY and LOWDAY recency direction no longer inverted

Lanre Shittu3ec76be8127f
diff --git a/agent/src/factors/zoo/gtja191/alpha_103.py b/agent/src/factors/zoo/gtja191/alpha_103.py
index 94b41c448..6338fe0e3 100644
--- a/agent/src/factors/zoo/gtja191/alpha_103.py
+++ b/agent/src/factors/zoo/gtja191/alpha_103.py
@@ -9,7 +9,10 @@
Formula (verbatim from the report):
((20-LOWDAY(low,20))/20)*100
-Notes: LOWDAY -> ts_argmin (0-based); (20 - argmin)/20 * 100.
+Notes: this repo's LOWDAY convention (see gtja191/LICENSE.md) rebases the
+0-based ts_argmin index to the report's day-count via ``n - ts_argmin``, so
+substituting that into the formula's own ``n - LOWDAY`` term cancels back to
+the raw index: ``(n - (n - ts_argmin)) / n * 100 == ts_argmin / n * 100``.
"""
from __future__ import annotations
@@ -46,7 +49,7 @@ __alpha_meta__ = {
'frequency': ['1d'],
'decay_horizon': 20,
'min_warmup_bars': 20,
- 'notes': 'LOWDAY -> ts_argmin (0-based); (20 - argmin)/20 * 100.',
+ 'notes': 'LOWDAY rebase cancels against the formula\'s own (20-LOWDAY) term, leaving ts_argmin/20*100.',
}
@@ -61,5 +64,5 @@ def compute(panel):
"""
l = panel["low"]
am = ts_argmin(l, 20)
- out = (20.0 - am) / 20.0 * 100.0
+ out = am / 20.0 * 100.0
return out
diff --git a/agent/src/factors/zoo/gtja191/alpha_133.py b/agent/src/factors/zoo/gtja191/alpha_133.py
index 9521c903a..2ef03b36f 100644
--- a/agent/src/factors/zoo/gtja191/alpha_133.py
+++ b/agent/src/factors/zoo/gtja191/alpha_133.py
@@ -9,7 +9,10 @@
Formula (verbatim from the report):
((20-HIGHDAY(HIGH,20))/20)*100 - ((20-LOWDAY(LOW,20))/20)*100
-Notes:
+Notes: this repo's HIGHDAY/LOWDAY convention (see gtja191/LICENSE.md) rebases
+the 0-based ts_argmax/ts_argmin index to the report's day-count via
+``n - ts_argmax``, so substituting that into each term's own ``n - HIGHDAY``
+(or LOWDAY) cancels back to the raw index: ``ts_argmax/n*100 - ts_argmin/n*100``.
"""
from __future__ import annotations
@@ -46,7 +49,7 @@ __alpha_meta__ = {
'frequency': ['1d'],
'decay_horizon': 20,
'min_warmup_bars': 20,
- 'notes': '',
+ 'notes': 'HIGHDAY/LOWDAY rebase cancels against each term\'s own (20-x) wrapper, leaving ts_argmax/20*100 - ts_argmin/20*100.',
}
@@ -61,5 +64,5 @@ def compute(panel):
"""
h = panel["high"]
l = panel["low"]
- out = (20.0 - ts_argmax(h, 20)) / 20.0 * 100.0 - (20.0 - ts_argmin(l, 20)) / 20.0 * 100.0
+ out = ts_argmax(h, 20) / 20.0 * 100.0 - ts_argmin(l, 20) / 20.0 * 100.0
return out
diff --git a/agent/src/factors/zoo/gtja191/alpha_177.py b/agent/src/factors/zoo/gtja191/alpha_177.py
index a69d6a3a7..ad09fe736 100644
--- a/agent/src/factors/zoo/gtja191/alpha_177.py
+++ b/agent/src/factors/zoo/gtja191/alpha_177.py
@@ -9,7 +9,10 @@
Formula (verbatim from the report):
((20-HIGHDAY(HIGH,20))/20)*100
-Notes:
+Notes: this repo's HIGHDAY convention (see gtja191/LICENSE.md) rebases the
+0-based ts_argmax index to the report's day-count via ``n - ts_argmax``, so
+substituting that into the formula's own ``n - HIGHDAY`` term cancels back to
+the raw index: ``(n - (n - ts_argmax)) / n * 100 == ts_argmax / n * 100``.
"""
from __future__ import annotations
@@ -46,7 +49,7 @@ __alpha_meta__ = {
'frequency': ['1d'],
'decay_horizon': 20,
'min_warmup_bars': 20,
- 'notes': '',
+ 'notes': 'HIGHDAY rebase cancels against the formula\'s own (20-HIGHDAY) term, leaving ts_argmax/20*100.',
}
@@ -60,5 +63,5 @@ def compute(panel):
pd.DataFrame with index = panel["close"].index, columns = panel["close"].columns.
"""
h = panel["high"]
- out = (20.0 - ts_argmax(h, 20)) / 20.0 * 100.0
+ out = ts_argmax(h, 20) / 20.0 * 100.0
return out
diff --git a/agent/tests/factors/test_gtja191_highday_lowday_recency.py b/agent/tests/factors/test_gtja191_highday_lowday_recency.py
new file mode 100644
index 000000000..23920d56f
--- /dev/null
+++ b/agent/tests/factors/test_gtja191_highday_lowday_recency.py
@@ -0,0 +1,72 @@
+"""Semantic tests for ``zoo/gtja191/alpha_103``, ``alpha_133``, ``alpha_177``.
+
+These factors are the report's HIGHDAY/LOWDAY-based Aroon-style oscillators:
+a high or low that happened *today* must score near 100, one that happened at
+the oldest edge of the window must score near 0. ``ts_argmax``/``ts_argmin``
+are 0-based from the start of the window (index ``n-1`` is the most recent
+bar), and this repo's documented rebase to the report's day-count convention
+(``n - ts_argmax``) cancels algebraically against the formula's own
+``n - HIGHDAY`` term, leaving the raw index scaled by ``100/n``.
+"""
+
+from __future__ import annotations
+
+import pandas as pd
+import pytest
+
+from src.factors.zoo.gtja191.alpha_103 import compute as alpha_103
+from src.factors.zoo.gtja191.alpha_133 import compute as alpha_133
+from src.factors.zoo.gtja191.alpha_177 import compute as alpha_177
+
+N = 20
+
+
+def _panel_with_extreme_at(offset_from_end: int) -> dict[str, pd.DataFrame]:
+ """A 20-bar window whose low and high both sit at ``offset_from_end``.
+
+ ``offset_from_end=0`` puts the extreme on the most recent bar (today);
+ ``offset_from_end=N-1`` puts it on the oldest bar in the window.
+ """
+ close = [100.0] * N
+ low = [90.0] * N
+ high = [110.0] * N
+ idx = N - 1 - offset_from_end
+ low[idx] = 1.0
+ high[idx] = 1000.0
+ return {
+ "close": pd.DataFrame({"AAA": close}),
+ "low": pd.DataFrame({"AAA": low}),
+ "high": pd.DataFrame({"AAA": high}),
+ }
+
+
+def test_alpha_103_scores_a_todays_low_near_the_maximum() -> None:
+ panel = _panel_with_extreme_at(offset_from_end=0)
+ out = alpha_103(panel)
+ assert out["AAA"].iloc[-1] == pytest.approx(95.0)
+
+
+def test_alpha_103_scores_the_oldest_low_near_the_minimum() -> None:
+ panel = _panel_with_extreme_at(offset_from_end=N - 1)
+ out = alpha_103(panel)
+ assert out["AAA"].iloc[-1] == pytest.approx(0.0)
+
+
+def test_alpha_177_scores_todays_high_near_the_maximum() -> None:
+ panel = _panel_with_extreme_at(offset_from_end=0)
+ out = alpha_177(panel)
+ assert out["AAA"].iloc[-1] == pytest.approx(95.0)
+
+
+def test_alpha_177_scores_the_oldest_high_near_the_minimum() -> None:
+ panel = _panel_with_extreme_at(offset_from_end=N - 1)
+ out = alpha_177(panel)
+ assert out["AAA"].iloc[-1] == pytest.approx(0.0)
+
+
+def test_alpha_133_is_the_difference_of_the_two_recency_scores() -> None:
+ panel = _panel_with_extreme_at(offset_from_end=0)
+ out = alpha_133(panel)
+ # Both the high and the low sit on today's bar, so the Aroon-up and
+ # Aroon-down terms are equal and cancel.
+ assert out["AAA"].iloc[-1] == pytest.approx(0.0)