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:
@@ -389,6 +389,7 @@ export interface Position {
|
||||
quantity: number;
|
||||
avg_entry_price: number;
|
||||
current_price: number | null;
|
||||
polygon_price: number | null;
|
||||
unrealized_pnl: number | null;
|
||||
realized_pnl: number | null;
|
||||
updated_at: string;
|
||||
|
||||
+3
-6
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user