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