From 23834a93333c82ba70454bbc8acb08e64fa6b409 Mon Sep 17 00:00:00 2001 From: Celes Renata Date: Wed, 13 May 2026 19:06:37 +0000 Subject: [PATCH] fix: filter correlation matrix to tracked companies and make non-blocking The correlation query was fetching 12K+ tickers from market_snapshots instead of just the 50 tracked companies, causing OOM during startup. - JOIN with companies table to filter to active tracked tickers only - Move correlation computation to background task so engine starts trading immediately - Add 30s timeout to prevent indefinite hangs --- services/trading/engine.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/services/trading/engine.py b/services/trading/engine.py index fc47f59..6f91f9a 100644 --- a/services/trading/engine.py +++ b/services/trading/engine.py @@ -598,8 +598,9 @@ class TradingEngine: initial_capital, invested, available, reserve_balance, open_count, ) - # Compute initial correlation matrix from market data - await self._compute_correlation_matrix() + # Compute initial correlation matrix from market data (non-blocking) + # Runs in background so the engine can start trading immediately + asyncio.create_task(self._compute_correlation_matrix(), name="correlation_matrix") async def _decision_loop(self) -> None: """Poll recommendations and evaluate them in a continuous loop. @@ -1351,12 +1352,19 @@ class TradingEngine: return try: - rows = await self.pool.fetch( - "SELECT ticker, captured_at::date AS dt, (data->>'c')::float AS close " - "FROM market_snapshots " - "WHERE snapshot_type = 'bar' AND captured_at > NOW() - INTERVAL '30 days' " - "ORDER BY ticker, captured_at" + rows = await asyncio.wait_for( + self.pool.fetch( + "SELECT ms.ticker, ms.captured_at::date AS dt, (ms.data->>'c')::float AS close " + "FROM market_snapshots ms " + "JOIN companies c ON c.ticker = ms.ticker AND c.active = TRUE " + "WHERE ms.snapshot_type = 'bar' AND ms.captured_at > NOW() - INTERVAL '30 days' " + "ORDER BY ms.ticker, ms.captured_at" + ), + timeout=30.0, ) + except asyncio.TimeoutError: + logger.warning("Correlation matrix query timed out — skipping") + return except Exception: logger.debug("Could not query market_snapshots for correlation matrix") return