feat: add portfolio snapshot read layer
This commit is contained in:
@@ -11,6 +11,7 @@ import threading
|
||||
from typing import Optional
|
||||
|
||||
from trex_core.application.broker_port import (
|
||||
BalanceItem,
|
||||
FillStatus,
|
||||
OrderAck,
|
||||
OrderRequest,
|
||||
@@ -94,3 +95,19 @@ class PaperBroker:
|
||||
return False
|
||||
o.cancelled = True
|
||||
return True
|
||||
|
||||
def get_balance(self) -> list[BalanceItem]:
|
||||
"""Deterministic per-symbol fill aggregate (REQ-031 AC-3).
|
||||
|
||||
Aggregates buys − sells across all filled (or partially filled)
|
||||
paper orders, drops zero-qty rows (Design Decision 7).
|
||||
"""
|
||||
with self._lock:
|
||||
balances: dict[str, int] = {}
|
||||
for o in self._orders.values():
|
||||
if o.filled <= 0:
|
||||
continue
|
||||
sign = 1 if o.side == "buy" else -1
|
||||
balances[o.symbol] = balances.get(o.symbol, 0) + sign * int(o.filled)
|
||||
return [BalanceItem(symbol=sym, hldg_qty=qty)
|
||||
for sym, qty in balances.items() if qty != 0]
|
||||
|
||||
Reference in New Issue
Block a user