fix: fetch current prices from market_snapshots before evaluating recommendations — fixes 'Invalid current price' skip
This commit is contained in:
@@ -599,6 +599,28 @@ class TradingEngine:
|
||||
"Polled %d recommendations (highest confidence=%.3f)",
|
||||
len(recs), recs[0].get("confidence", 0),
|
||||
)
|
||||
|
||||
# Fetch current prices for all tickers in this batch
|
||||
batch_tickers = list({r.get("ticker", "") for r in recs if r.get("ticker")})
|
||||
price_map: dict[str, float] = {}
|
||||
if batch_tickers and self.pool is not None:
|
||||
try:
|
||||
price_rows = await self.pool.fetch(
|
||||
"""SELECT DISTINCT ON (ticker) ticker, (data->>'c')::float AS price
|
||||
FROM market_snapshots
|
||||
WHERE ticker = ANY($1) AND snapshot_type = 'bar'
|
||||
ORDER BY ticker, captured_at DESC""",
|
||||
batch_tickers,
|
||||
)
|
||||
price_map = {r["ticker"]: r["price"] for r in price_rows if r["price"]}
|
||||
except Exception:
|
||||
logger.debug("Could not fetch prices from market_snapshots")
|
||||
|
||||
# Fall back to Polygon API for any missing prices
|
||||
missing = [t for t in batch_tickers if t not in price_map]
|
||||
if missing:
|
||||
fetched = await self._fetch_current_prices(missing)
|
||||
price_map.update(fetched)
|
||||
except Exception:
|
||||
logger.debug("Could not poll recommendations — table may not exist yet")
|
||||
continue
|
||||
@@ -606,6 +628,15 @@ class TradingEngine:
|
||||
for rec in recs:
|
||||
try:
|
||||
rec_id = str(rec.get("recommendation_id", rec.get("id", "")))
|
||||
ticker = rec.get("ticker", "")
|
||||
|
||||
# Inject current price from market data
|
||||
if not rec.get("current_price") and ticker in price_map:
|
||||
rec["current_price"] = price_map[ticker]
|
||||
|
||||
# Skip if no price available
|
||||
if not rec.get("current_price") or rec["current_price"] <= 0:
|
||||
continue
|
||||
|
||||
# Redis deduplication check
|
||||
if self.redis is not None:
|
||||
|
||||
Reference in New Issue
Block a user