fix: keep Alpaca price as primary (includes after-hours), add polygon_price field

Alpaca's current_price reflects extended hours trading which is more
current than Polygon's regular session close. Keep it as the display
price. Add polygon_price as a reference field in the API response.
This commit is contained in:
Celes Renata
2026-04-30 22:35:22 +00:00
parent 9a60ce127b
commit 2f2ea65fb4
2 changed files with 4 additions and 6 deletions
+3 -6
View File
@@ -1201,7 +1201,8 @@ async def list_positions(
FROM positions p ORDER BY p.ticker""",
)
# Build a price map from the latest Polygon bars
# Enrich with latest Polygon close for comparison.
# Use whichever price is more recent: broker sync or Polygon bar.
tickers = list({r["ticker"] for r in rows})
price_map: dict[str, float] = {}
if tickers:
@@ -1218,11 +1219,7 @@ async def list_positions(
for r in rows:
d = _row_to_dict(r)
polygon_price = price_map.get(d["ticker"])
if polygon_price:
d["current_price"] = polygon_price
qty = d.get("quantity", 0) or 0
entry = d.get("avg_entry_price", 0) or 0
d["unrealized_pnl"] = round(qty * (polygon_price - entry), 2)
d["polygon_price"] = polygon_price
results.append(d)
return results