Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix: Align daily correlation and regime bars by each source local calendar date before joining.

Rudy Celekli8639a2140638
diff --git a/README.md b/README.md
index b359b3ac8..6986d2a61 100644
--- a/README.md
+++ b/README.md
@@ -370,6 +370,8 @@ It is designed for research, simulation, and backtesting — and, when you choos
| **Bench a pre-built alpha zoo** | One-line IC + alive/reversed/dead categorisation across 462 alphas (Qlib 158 + Kakushadze 101 + GTJA 191 + academic + PIT-safe fundamental) on your universe. |
| **Spot correlation regimes** | An edge-density + hysteresis timeline on the `/correlation` surface showing when markets fuse into one bloc — descriptive risk context, not a signal. |
+Daily correlation and regime requests align each source by its local calendar date, including mixed timezone-aware and timezone-naive histories.
+
---
## ⚡ Quick Example
diff --git a/agent/backtest/correlation.py b/agent/backtest/correlation.py
index 8a3e1321a..6b07dc294 100644
--- a/agent/backtest/correlation.py
+++ b/agent/backtest/correlation.py
@@ -164,7 +164,7 @@ def _rolling_correlation_matrix(
# Normalize to date-only (midnight) so that cross-market assets
# (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()
+ ts.index = ts.index.tz_localize(None).normalize()
# ``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.
diff --git a/agent/backtest/regime.py b/agent/backtest/regime.py
index 6e77ed640..00f477c99 100644
--- a/agent/backtest/regime.py
+++ b/agent/backtest/regime.py
@@ -111,7 +111,7 @@ def _aligned_returns(price_series: Dict[str, pd.DataFrame]) -> pd.DataFrame:
returns_frames = []
for code in sorted(price_series):
ts = _close_series(code, price_series[code])
- ts.index = ts.index.normalize()
+ ts.index = ts.index.tz_localize(None).normalize()
rets = ts.pct_change(fill_method=None).dropna()
rets.name = code
returns_frames.append(rets)
diff --git a/agent/tests/test_crossmarket_dates_regressions.py b/agent/tests/test_crossmarket_dates_regressions.py
new file mode 100644
index 000000000..adb342ea2
--- /dev/null
+++ b/agent/tests/test_crossmarket_dates_regressions.py
@@ -0,0 +1,29 @@
+import pandas as pd
+import pytest
+from backtest.correlation import compute_correlation_matrix
+from backtest.regime import compute_regime_timeline
+
+
+def _prices(tz):
+ return pd.DataFrame(
+ {"close": [100, 105, 101, 109, 107]},
+ index=pd.date_range("2025-01-01", periods=5, tz=tz),
+ )
+
+
[email protected]("tz", ["America/New_York", None])
+def test_correlation_aligns_local_calendar_dates_across_timezones(monkeypatch, tz):
+ prices = {"A": _prices("UTC"), "B": _prices(tz)}
+ monkeypatch.setattr(
+ "backtest.correlation._fetch_price_series", lambda *args: prices
+ )
+ actual = compute_correlation_matrix(["A", "B"], days=30)
+ assert actual["matrix"] == [[1.0, 1.0], [1.0, 1.0]]
+
+
+def test_regime_uses_same_calendar_alignment(monkeypatch):
+ prices = {"A": _prices("UTC"), "B": _prices("America/New_York")}
+ monkeypatch.setattr("backtest.regime._fetch_price_series", lambda *args: prices)
+ actual = compute_regime_timeline(["A", "B"], corr_window=2, smooth_window=1)
+ assert actual["dates"] == ["2025-01-02", "2025-01-03", "2025-01-04", "2025-01-05"]
+ assert actual["density"] == [None, 1.0, 1.0, 1.0]