Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(factors): weight the most recent bar heaviest in decay_linear

Rudy Celekli085026f86e36
diff --git a/agent/src/factors/base.py b/agent/src/factors/base.py
index cf1ee6c0c..0ac7709a2 100644
--- a/agent/src/factors/base.py
+++ b/agent/src/factors/base.py
@@ -353,6 +353,9 @@ def delta(df: pd.DataFrame, d: int) -> pd.DataFrame:
def decay_linear(df: pd.DataFrame, n: int) -> pd.DataFrame:
"""Linear decay-weighted moving average, weights ``n, n-1, ..., 1`` normalized.
+ The most recent bar gets weight ``n`` and the oldest bar in the window
+ gets weight ``1`` (Kakushadze, *101 Formulaic Alphas*, Appendix A).
+
Warmup (first ``n-1`` rows) → NaN.
Uses numpy ``sliding_window_view`` + ``einsum`` for vectorized computation
@@ -361,7 +364,8 @@ def decay_linear(df: pd.DataFrame, n: int) -> pd.DataFrame:
"""
if n < 1:
raise ValueError(f"decay_linear window must be >= 1, got {n}")
- weights = np.arange(n, 0, -1, dtype=np.float64)
+ # Windows run oldest -> newest, so ascending weights put n on today's bar.
+ weights = np.arange(1, n + 1, dtype=np.float64)
weights /= weights.sum()
def _apply(arr: np.ndarray) -> float:
diff --git a/agent/tests/factors/fixtures/goldens/gtja191_130.csv b/agent/tests/factors/fixtures/goldens/gtja191_130.csv
index 2f9cc7ed2..89867e798 100644
--- a/agent/tests/factors/fixtures/goldens/gtja191_130.csv
+++ b/agent/tests/factors/fixtures/goldens/gtja191_130.csv
@@ -55,20 +55,20 @@
2024-02-23,,,,,
2024-02-24,,,,,
2024-02-25,,,,,
-2024-02-26,1.4999999999962499,0.99999999999875,0.3999999999996,1.666666666663889,0.999999999995
-2024-02-27,0.7999999999989333,,0.7999999999984001,0.9999999999989999,0.7999999999968002
-2024-02-28,0.7999999999984001,,0.7999999999968002,1.3333333333315556,0.5999999999993999
-2024-02-29,0.7999999999968002,,0.7999999999984001,1.3333333333315556,0.5999999999993999
-2024-03-01,0.39999999999920005,,2.3999999999904,1.3333333333315556,0.7999999999992
-2024-03-02,0.39999999999920005,,2.3999999999904,1.3333333333315556,0.7999999999992
-2024-03-03,0.7999999999968002,,1.1999999999976,1.3333333333315556,0.7999999999992
-2024-03-04,,,,1.999999999996,0.7999999999992
-2024-03-05,,,,1.999999999996,0.7999999999992
+2024-02-26,0.1999999999998,2.9999999999849996,1.3333333333311113,1.2499999999984375,0.9999999999975001
+2024-02-27,0.39999999999920005,,3.1999999999872006,1.3333333333315556,0.5999999999993999
+2024-02-28,0.39999999999920005,,3.1999999999872006,1.3333333333315556,0.5999999999993999
+2024-02-29,0.7999999999984001,,2.3999999999904,1.3333333333315556,0.7999999999992
+2024-03-01,1.5999999999936003,,1.1999999999976,1.3333333333315556,0.7999999999992
+2024-03-02,1.5999999999936003,,1.1999999999976,1.3333333333315556,0.7999999999992
+2024-03-03,1.5999999999936003,,1.1999999999976,1.0666666666652445,0.9999999999989999
+2024-03-04,,,,1.5999999999968002,0.9999999999989999
+2024-03-05,,,,1.5999999999968002,0.9999999999989999
2024-03-06,,,,0.7999999999992,
-2024-03-07,0.5999999999993999,,,1.5999999999968002,
-2024-03-08,0.5999999999993999,,,1.5999999999968002,
-2024-03-09,0.3999999999996,,,1.5999999999968002,
-2024-03-10,0.3999999999996,,,1.5999999999968002,
+2024-03-07,0.3999999999996,,,1.5999999999968002,
+2024-03-08,0.3999999999996,,,1.5999999999968002,
+2024-03-09,0.3999999999996,,,1.999999999996,
+2024-03-10,0.3999999999996,,,1.999999999996,
2024-03-11,,,,,
2024-03-12,,,,,
2024-03-13,,,,,
@@ -76,53 +76,53 @@
2024-03-15,,,,,
2024-03-16,,,,,
2024-03-17,0.1999999999998,,,,1.5999999999968002
-2024-03-18,0.1999999999998,,0.7999999999984001,1.3333333333315556,3.1999999999872006
-2024-03-19,0.2666666666663111,,0.7999999999984001,0.9999999999989999,3.1999999999872006
-2024-03-20,0.1999999999998,,0.7999999999984001,1.3333333333315556,3.1999999999872006
-2024-03-21,,,0.1999999999998,1.999999999996,
+2024-03-18,0.5333333333326222,,0.39999999999920005,0.7999999999992,2.3999999999904
+2024-03-19,0.9999999999989999,,0.39999999999920005,0.7999999999989333,1.5999999999936003
+2024-03-20,0.9999999999989999,,0.39999999999920005,0.7999999999989333,1.5999999999936003
+2024-03-21,,,0.1999999999998,1.5999999999968002,
2024-03-22,,,,,
2024-03-23,,,,,
2024-03-24,,,,,
-2024-03-25,,1.5999999999968002,,0.3999999999996,
-2024-03-26,,0.7999999999984001,,0.5999999999993999,
-2024-03-27,,0.7999999999984001,,0.5999999999993999,
-2024-03-28,,0.39999999999920005,,0.7999999999992,
-2024-03-29,,0.39999999999920005,,0.7999999999992,
-2024-03-30,,0.1999999999998,,1.5999999999968002,
-2024-03-31,,0.1999999999998,,1.1999999999976,
-2024-04-01,0.5333333333326222,0.39999999999920005,,2.3999999999904,0.9999999999989999
-2024-04-02,0.3333333333327778,0.9999999999975001,0.99999999999875,2.9999999999849996,0.9999999999989999
-2024-04-03,0.2499999999996875,0.9999999999975001,1.3333333333311113,2.9999999999849996,0.9999999999989999
-2024-04-04,0.2499999999996875,1.4999999999962499,1.3333333333311113,1.99999999999,0.9999999999989999
-2024-04-05,0.3333333333327778,1.4999999999962499,0.99999999999875,1.99999999999,0.9999999999989999
-2024-04-06,,1.1999999999976,0.7999999999992,1.5999999999936003,1.3333333333315556
-2024-04-07,,1.5999999999968002,0.7999999999989333,1.5999999999936003,0.9999999999989999
-2024-04-08,,1.5999999999968002,0.5333333333326222,2.3999999999904,0.9999999999989999
-2024-04-09,0.1999999999998,1.9999999999950002,0.6666666666655556,2.9999999999849996,1.2499999999984375
-2024-04-10,0.1999999999998,,,,1.999999999996
-2024-04-11,0.3999999999996,,,,1.999999999996
-2024-04-12,0.3999999999996,,,,1.999999999996
-2024-04-13,0.3999999999996,,,,1.999999999996
+2024-03-25,,0.39999999999920005,,0.5999999999993999,
+2024-03-26,,0.39999999999920005,,0.5999999999993999,
+2024-03-27,,0.39999999999920005,,0.5999999999993999,
+2024-03-28,,0.39999999999920005,,0.5999999999993999,
+2024-03-29,,0.7999999999984001,,0.5999999999993999,
+2024-03-30,,0.3999999999996,,1.1999999999976,
+2024-03-31,,0.3999999999996,,1.1999999999976,
+2024-04-01,0.2666666666663111,1.1999999999976,,1.5999999999936003,0.9999999999989999
+2024-04-02,0.2499999999996875,1.4999999999962499,1.3333333333311113,1.99999999999,0.9999999999989999
+2024-04-03,0.2499999999996875,1.4999999999962499,1.3333333333311113,1.99999999999,0.9999999999989999
+2024-04-04,0.3333333333327778,1.9999999999950002,0.7499999999990624,1.99999999999,0.9999999999989999
+2024-04-05,0.3333333333327778,1.9999999999950002,0.499999999999375,2.9999999999849996,0.9999999999989999
+2024-04-06,,1.5999999999968002,0.5333333333326222,2.3999999999904,0.9999999999989999
+2024-04-07,,1.5999999999968002,0.5333333333326222,2.3999999999904,0.9999999999989999
+2024-04-08,,1.5999999999968002,0.2666666666663111,2.3999999999904,0.9999999999989999
+2024-04-09,0.3999999999996,2.9999999999849996,0.3333333333327778,1.9999999999950002,1.2499999999984375
+2024-04-10,0.9999999999989999,,,,1.1999999999976
+2024-04-11,0.9999999999989999,,,,0.7999999999984001
+2024-04-12,0.9999999999989999,,,,0.7999999999984001
+2024-04-13,0.9999999999989999,,,,0.7999999999984001
2024-04-14,0.9999999999989999,,,,0.7999999999984001
2024-04-15,0.9999999999989999,,,,0.7999999999984001
2024-04-16,0.9999999999989999,,,,0.7999999999984001
2024-04-17,0.9999999999989999,,,,0.7999999999984001
-2024-04-18,0.9999999999989999,,,,0.7999999999984001
-2024-04-19,0.9999999999989999,,,,0.7999999999984001
-2024-04-20,0.9999999999989999,,,,1.1999999999976
-2024-04-21,0.7999999999992,,,,1.1999999999976
+2024-04-18,0.7999999999992,,,,1.1999999999976
+2024-04-19,0.5999999999993999,,,,1.5999999999968002
+2024-04-20,0.5999999999993999,,,,1.5999999999968002
+2024-04-21,0.3999999999996,,,,1.5999999999968002
2024-04-22,,,,,
2024-04-23,,,,,
2024-04-24,,,,,
2024-04-25,,,,,
2024-04-26,,,,,
-2024-04-27,0.7999999999984001,,,,0.7999999999992
-2024-04-28,0.7999999999984001,,,,0.7999999999992
-2024-04-29,1.1999999999976,,,,0.7999999999992
+2024-04-27,1.1999999999976,,,,0.9999999999989999
+2024-04-28,1.5999999999968002,,,,0.9999999999989999
+2024-04-29,1.1999999999976,,,,0.9999999999989999
2024-04-30,1.1999999999976,,,,0.9999999999989999
-2024-05-01,1.1999999999976,,,,0.9999999999989999
-2024-05-02,1.1999999999976,,,,0.9999999999989999
-2024-05-03,0.7999999999992,,,,1.999999999996
+2024-05-01,1.1999999999976,,,,0.7999999999992
+2024-05-02,1.5999999999968002,,,,0.5999999999993999
+2024-05-03,1.5999999999968002,,,,0.5999999999993999
2024-05-04,,,,,
2024-05-05,,,,,
2024-05-06,,,,,
@@ -136,19 +136,19 @@
2024-05-14,,,,,
2024-05-15,,,,,
2024-05-16,,,,,
-2024-05-17,0.7999999999984001,,0.1999999999998,,
-2024-05-18,0.7999999999984001,,0.1999999999998,,
-2024-05-19,1.1999999999976,0.5333333333326222,0.1999999999998,,3.9999999999840004
+2024-05-17,1.1999999999976,,0.3999999999996,,
+2024-05-18,1.1999999999976,,0.3999999999996,,
+2024-05-19,1.1999999999976,0.2666666666663111,0.3999999999996,,3.9999999999840004
2024-05-20,1.1999999999976,0.2666666666663111,0.3999999999996,,3.9999999999840004
-2024-05-21,1.1999999999976,0.2666666666663111,0.3999999999996,,3.9999999999840004
+2024-05-21,1.5999999999968002,0.2666666666663111,0.3999999999996,,3.9999999999840004
2024-05-22,,0.1999999999998,,,1.999999999996
-2024-05-23,,0.1999999999998,,,1.999999999996
-2024-05-24,,0.1999999999998,,,1.999999999996
-2024-05-25,,0.1999999999998,,,1.999999999996
-2024-05-26,,0.3999999999996,,,1.999999999996
+2024-05-23,,0.3999999999996,,,1.999999999996
+2024-05-24,,0.3999999999996,,,1.999999999996
+2024-05-25,,0.3999999999996,,,1.999999999996
+2024-05-26,,0.5999999999993999,,,1.999999999996
2024-05-27,,0.3999999999996,,,1.999999999996
2024-05-28,,0.3999999999996,,,1.999999999996
-2024-05-29,,0.5999999999993999,,,1.999999999996
+2024-05-29,,0.7999999999984001,,,0.9999999999989999
2024-05-30,,,,,
2024-05-31,,,,,
2024-06-01,,,,,
@@ -156,6 +156,6 @@
2024-06-03,,,,,
2024-06-04,,,,,
2024-06-05,,,,,
-2024-06-06,0.3999999999996,,1.1999999999976,,
-2024-06-07,1.5999999999968002,,0.3999999999996,,
-2024-06-08,1.5999999999968002,,0.5999999999993999,,
+2024-06-06,1.5999999999968002,,0.5999999999993999,,
+2024-06-07,1.1999999999976,,0.7999999999992,,
+2024-06-08,1.1999999999976,,0.7999999999992,,
diff --git a/agent/tests/factors/test_base.py b/agent/tests/factors/test_base.py
index 8767ea436..b64d6c3b3 100644
--- a/agent/tests/factors/test_base.py
+++ b/agent/tests/factors/test_base.py
@@ -147,8 +147,15 @@ def test_delta_positive_shift() -> None:
def test_decay_linear_weights() -> None:
df = _frame([[1.0], [2.0], [3.0]])
out = decay_linear(df, 3)
- # weights 3,2,1 / 6 applied to [1,2,3] → (1*3 + 2*2 + 3*1)/6 = 10/6
- assert pytest.approx(out.iloc[2, 0]) == 10.0 / 6.0
+ # newest bar weighted 3: (1*1 + 2*2 + 3*3)/6 = 14/6
+ assert pytest.approx(out.iloc[2, 0]) == 14.0 / 6.0
+
+
+def test_decay_linear_weights_most_recent_bar_heaviest() -> None:
+ df = _frame([[0.0], [0.0], [0.0], [0.0], [10.0]])
+ out = decay_linear(df, 5)
+ # a shock on today's bar carries weight 5/15, not 1/15
+ assert pytest.approx(out.iloc[4, 0]) == 10.0 * 5.0 / 15.0
def test_decay_linear_warmup() -> None:
diff --git a/agent/tests/test_factor_operators.py b/agent/tests/test_factor_operators.py
index cb6fe4802..23497be00 100644
--- a/agent/tests/test_factor_operators.py
+++ b/agent/tests/test_factor_operators.py
@@ -72,7 +72,7 @@ def _ref_ts_argmin(df: pd.DataFrame, n: int) -> pd.DataFrame:
def _ref_decay_linear(df: pd.DataFrame, n: int) -> pd.DataFrame:
- weights = np.arange(n, 0, -1, dtype=np.float64)
+ weights = np.arange(1, n + 1, dtype=np.float64)
weights /= weights.sum()
def _apply(arr: np.ndarray) -> float: