Fix cross-session avg_entry_price contamination in positions view

Parking positions now use parking_state.avg_price instead of Alpaca's
blended avg_entry_price, which gets polluted when multiple sessions
share one broker account.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
main
I Luk Kim 4 months ago
parent bba1ce6789
commit df10d1291f

@ -314,13 +314,19 @@ def get_positions(session_id: str) -> dict[str, Any]:
is_parking = parking_symbol and p.symbol == parking_symbol is_parking = parking_symbol and p.symbol == parking_symbol
if ss is None and not is_parking: if ss is None and not is_parking:
continue # Only show positions tracked by this session continue # Only show positions tracked by this session
# Use locally-recorded entry price/qty for strategy positions to avoid # Use locally-recorded entry price/qty to avoid cross-session contamination
# orphaned-share contamination of Alpaca's blended avg_entry_price. # of Alpaca's blended avg_entry_price (multiple sessions share one broker account).
ot = open_trades.get(p.symbol) if not is_parking else None ot = open_trades.get(p.symbol) if not is_parking else None
qty = float(ot["shares"]) if ot and ot.get("shares") else float(p.qty) if is_parking and parking_state:
entry = float(ot["entry_price"]) if ot and ot.get("entry_price") else ( qty = float(parking_state.get("qty") or p.qty)
float(p.avg_entry_price) if p.avg_entry_price else 0.0 entry = float(parking_state["avg_price"]) if parking_state.get("avg_price") else (
) float(p.avg_entry_price) if p.avg_entry_price else 0.0
)
else:
qty = float(ot["shares"]) if ot and ot.get("shares") else float(p.qty)
entry = float(ot["entry_price"]) if ot and ot.get("entry_price") else (
float(p.avg_entry_price) if p.avg_entry_price else 0.0
)
cur_price = float(p.current_price) if p.current_price else None cur_price = float(p.current_price) if p.current_price else None
pnl = (cur_price - entry) * qty if cur_price and entry and qty else float(p.unrealized_pl) pnl = (cur_price - entry) * qty if cur_price and entry and qty else float(p.unrealized_pl)
pnl_pct = pnl / (entry * qty) * 100 if entry and qty else 0.0 pnl_pct = pnl / (entry * qty) * 100 if entry and qty else 0.0

Loading…
Cancel
Save