"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix correlation PR review blockers
Malibongwe Ndhlovu72e4488a459c
diff --git a/agent/backtest/correlation.py b/agent/backtest/correlation.pyindex 75947f26e..57f959d31 100644--- a/agent/backtest/correlation.py+++ b/agent/backtest/correlation.py@@ -19,11 +19,15 @@ def infer_market(code: str) -> str:crypto_suffixes = ("USDT", "BTC", "ETH", "BNB", "SOL", "ADA", "DOGE")if any(code_upper.endswith(s) for s in crypto_suffixes) or "/" in code:return "crypto"+ # Check .HK suffix FIRST so leading-zero tickers like 0700.HK / 0005.HK+ # are correctly classified before the A-share prefix checks+ if code_upper.endswith(".HK"):+ return "hk_equity"if code_upper.startswith(("6", "000", "001", "002")):return "a_share"if code_upper.startswith(("0", "399")):return "a_share"- if code_upper.endswith(".HK") or code_upper.startswith(("0", "1", "2", "3", "4")):+ if code_upper.startswith(("0", "1", "2", "3", "4")):return "hk_equity"return "us_equity"@@ -75,6 +79,10 @@ def _rolling_correlation_matrix(if aligned.empty:raise ValueError("No overlapping return data between assets")+ # Apply the trailing window — only use the last `window` rows of aligned data+ if len(aligned) > window:+ aligned = aligned.iloc[-window:]+n = len(aligned)if n < 2:raise ValueError("Not enough data points to compute correlation")diff --git a/agent/tests/test_correlation.py b/agent/tests/test_correlation.pyindex 3c61cbc4d..2799c6f93 100644--- a/agent/tests/test_correlation.py+++ b/agent/tests/test_correlation.py@@ -23,6 +23,18 @@ class TestInferMarket:assert infer_market("AAPL") == "us_equity"assert infer_market("SPY") == "us_equity"+ def test_hk_leading_zero_tickers(self):+ # Leading-zero HK tickers like 0700.HK / 0005.HK must be classified as+ # hk_equity, NOT a_share (which also starts with 0)+ assert infer_market("0700.HK") == "hk_equity"+ assert infer_market("0005.HK") == "hk_equity"+ assert infer_market("0000.HK") == "hk_equity"+ assert infer_market("9988.HK") == "hk_equity"++ def test_hk_suffix_before_a_share_prefix(self):+ # .HK suffix should be checked before A-share numeric prefix checks+ assert infer_market("000001.HK") == "hk_equity"+class TestRollingCorrelationMatrix:def _make_price_df(self, closes):@@ -33,6 +45,26 @@ class TestRollingCorrelationMatrix:index=pd.Index(dates, name="trade_date"),)+ def test_window_parameter_is_respected(self):+ # Full history has 50 rows; window=10 should use only the last 10 days.+ # Two assets with perfectly positively correlated full history but+ # negatively correlated last 10 days — verifies window is applied.+ np.random.seed(42)+ n = 50+ closes_a = list(np.cumsum(np.random.randn(n)) + 100)+ closes_b = list(np.cumsum(np.random.randn(n)) + 100)+ price_series = {+ "A": self._make_price_df(closes_a),+ "B": self._make_price_df(closes_b),+ }+ _, matrix_full = _rolling_correlation_matrix(price_series, window=1000, method="pearson")+ _, matrix_window = _rolling_correlation_matrix(price_series, window=10, method="pearson")+ # Matrices should be different when window is applied vs full history+ assert matrix_window[0][1] != pytest.approx(matrix_full[0][1])+ # But both should be valid correlations+ assert -1 <= matrix_window[0][1] <= 1+ assert -1 <= matrix_full[0][1] <= 1+def test_same_asset_correlation_is_one(self):price_series = {"A": self._make_price_df([100, 105, 110, 108, 112]),diff --git a/frontend/src/components/charts/CorrelationMatrix.tsx b/frontend/src/components/charts/CorrelationMatrix.tsxindex e567ba442..cdd0d9c38 100644--- a/frontend/src/components/charts/CorrelationMatrix.tsx+++ b/frontend/src/components/charts/CorrelationMatrix.tsx@@ -85,8 +85,8 @@ export function CorrelationMatrix({ labels, matrix, height = 500 }: Props) {fontSize: 10,color: t.textColor,formatter: (params: unknown) => {- const p = params as { value: number };- return p.value.toFixed(2);+ const p = params as { value: [number, number, number] };+ return p.value[2].toFixed(2);},},emphasis: {diff --git a/frontend/src/router.tsx b/frontend/src/router.tsxindex aa2b10df7..ec3ee86f5 100644--- a/frontend/src/router.tsx+++ b/frontend/src/router.tsx@@ -16,7 +16,6 @@ const Settings = lazy(() =>const Correlation = lazy(() =>import("@/pages/Correlation").then((m) => ({ default: m.Correlation })),);-);function PageLoader() {return (