Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(backtest): stop forward-filling missing closes in the correlation matrix

ddy4633b913ef67ebb8
diff --git a/agent/backtest/correlation.py b/agent/backtest/correlation.py
index 50c88ed44..ae5249f4c 100644
--- a/agent/backtest/correlation.py
+++ b/agent/backtest/correlation.py
@@ -142,7 +142,10 @@ def _rolling_correlation_matrix(
# (e.g. crypto via OKX/CCXT at UTC midnight vs US equity via
# yfinance at EDT midnight = 04:00 UTC) align correctly.
ts.index = ts.index.normalize()
- rets = ts.pct_change().dropna()
+ # ``fill_method=None`` is explicit because under the project's
+ # pandas>=2,<3 pin the ``pct_change`` default forward-fills missing
+ # prices, silently manufacturing 0% returns on halted sessions.
+ rets = ts.pct_change(fill_method=None).dropna()
rets.name = code
returns_frames.append(rets)
diff --git a/agent/tests/test_correlation.py b/agent/tests/test_correlation.py
index bba3fd77b..c57e83c3a 100644
--- a/agent/tests/test_correlation.py
+++ b/agent/tests/test_correlation.py
@@ -231,6 +231,23 @@ class TestRollingCorrelationMatrix:
assert -1 <= p_matrix[0][1] <= 1
assert -1 <= s_matrix[0][1] <= 1
+ def test_does_not_forward_fill_missing_closes(self):
+ # Under pandas>=2,<3 a bare pct_change() forward-fills NaN closes,
+ # manufacturing a 0% return on the halted session and pairing it
+ # against the peer's real move. GAP tracks PEER exactly, so once the
+ # halted session is dropped instead of filled the two are perfectly
+ # correlated. Mirrors TestAlignedReturns in test_regime.py.
+ peer_closes = [100.0, 110.0, 115.5, 112.035, 121.0, 123.42]
+ gap_closes = [c / 2.0 for c in peer_closes]
+ gap_closes[2] = np.nan # trading halt: no close printed
+ price_series = {
+ "GAP": self._make_price_df(gap_closes),
+ "PEER": self._make_price_df(peer_closes),
+ }
+ labels, matrix = _rolling_correlation_matrix(price_series, window=30, method="pearson")
+ i, j = labels.index("GAP"), labels.index("PEER")
+ assert matrix[i][j] == pytest.approx(1.0)
+
def test_empty_dict_returns_empty(self):
labels, matrix = _rolling_correlation_matrix({}, window=30, method="pearson")
assert labels == []