feat: add portfolio snapshot read layer
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
"""Position reconciliation tests (REQ-031)."""
|
||||
from __future__ import annotations
|
||||
|
||||
from trex_core.application.broker_port import BalanceItem
|
||||
from trex_core.application.portfolio import PortfolioItem, SnapshotStore
|
||||
from trex_core.application.reconciliation import reconcile_positions
|
||||
|
||||
|
||||
class FakeBroker:
|
||||
def __init__(self, balances):
|
||||
self._b = balances
|
||||
def name(self): return "fake"
|
||||
def submit_order(self, req): ...
|
||||
def poll_fill(self, *args, **kw): ...
|
||||
def cancel_order(self, *args, **kw): ...
|
||||
def get_balance(self): return list(self._b)
|
||||
|
||||
|
||||
def _store_with(*items):
|
||||
store = SnapshotStore()
|
||||
store.swap(list(items))
|
||||
return store
|
||||
|
||||
|
||||
def test_mismatch_raises_warning_and_masks_asset():
|
||||
store = _store_with(
|
||||
PortfolioItem(portfolio_id="pf-A", symbol="005930",
|
||||
cash_balance=0, asset_quantity=10),
|
||||
)
|
||||
broker = FakeBroker([BalanceItem(symbol="005930", hldg_qty=7)]) # mismatch
|
||||
decisions = reconcile_positions(store=store, broker=broker)
|
||||
assert decisions["pf-A"] == "warning"
|
||||
assert store.warnings() == {"pf-A"}
|
||||
|
||||
|
||||
def test_subsequent_match_auto_clears_warning():
|
||||
store = _store_with(
|
||||
PortfolioItem(portfolio_id="pf-A", symbol="005930",
|
||||
cash_balance=0, asset_quantity=10),
|
||||
)
|
||||
# round 1 — mismatch
|
||||
reconcile_positions(store=store, broker=FakeBroker([BalanceItem("005930", 7)]))
|
||||
assert "pf-A" in store.warnings()
|
||||
# round 2 — match
|
||||
decisions = reconcile_positions(store=store, broker=FakeBroker([BalanceItem("005930", 10)]))
|
||||
assert decisions["pf-A"] == "cleared"
|
||||
assert store.warnings() == set()
|
||||
|
||||
|
||||
def test_multiple_portfolios_same_symbol_aggregate():
|
||||
store = _store_with(
|
||||
PortfolioItem(portfolio_id="pf-A", symbol="005930",
|
||||
cash_balance=0, asset_quantity=6),
|
||||
PortfolioItem(portfolio_id="pf-B", symbol="005930",
|
||||
cash_balance=0, asset_quantity=4),
|
||||
)
|
||||
# hub total 6+4=10 == broker 10 → both ok
|
||||
decisions = reconcile_positions(
|
||||
store=store, broker=FakeBroker([BalanceItem("005930", 10)]),
|
||||
)
|
||||
assert decisions == {"pf-A": "ok", "pf-B": "ok"}
|
||||
assert store.warnings() == set()
|
||||
|
||||
|
||||
def test_paper_broker_returns_deterministic_balance():
|
||||
"""REQ-031 AC-3: paper stub provides deterministic balance."""
|
||||
from trex_core.adapters.broker.paper import PaperBroker
|
||||
from trex_core.application.broker_port import OrderRequest
|
||||
|
||||
broker = PaperBroker(fill_after_polls=0)
|
||||
ack = broker.submit_order(OrderRequest(slot_id="s", side="buy",
|
||||
symbol="005930", qty=10, limit_price=1000))
|
||||
broker.poll_fill(ack.broker_order_id, side="buy", symbol="005930") # fill
|
||||
balances = broker.get_balance()
|
||||
assert len(balances) == 1
|
||||
assert balances[0].symbol == "005930"
|
||||
assert balances[0].hldg_qty == 10
|
||||
|
||||
# zero-qty rows dropped (D-2 0-qty leftover semantics)
|
||||
ack2 = broker.submit_order(OrderRequest(slot_id="s2", side="buy",
|
||||
symbol="000660", qty=5, limit_price=500))
|
||||
broker.poll_fill(ack2.broker_order_id, side="buy", symbol="000660")
|
||||
ack3 = broker.submit_order(OrderRequest(slot_id="s3", side="sell",
|
||||
symbol="000660", qty=5, limit_price=500))
|
||||
broker.poll_fill(ack3.broker_order_id, side="sell", symbol="000660")
|
||||
syms = {b.symbol for b in broker.get_balance()}
|
||||
assert "000660" not in syms # net 0 → dropped
|
||||
assert "005930" in syms
|
||||
Reference in New Issue
Block a user