fix: reconcile ambiguous KIS order submissions
This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
|
||||
In-memory adapters; deterministic paper double drives the full async path.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from trex_core.adapters.broker.paper import PaperBroker
|
||||
from trex_core.adapters.persistence.in_memory_intent_store import InMemoryIntentStore
|
||||
from trex_core.application.broker_port import FillStatus, OrderAck
|
||||
from trex_core.application.execution import ExecutionService
|
||||
from trex_core.application.intents import BuyIntent, IntentService, SellIntent
|
||||
from trex_core.domain.slot import SlotService, SlotState
|
||||
@@ -19,9 +21,12 @@ def _wire(broker, max_active_slots=None):
|
||||
repo = InMemorySlotRepository()
|
||||
svc = SlotService(repo, instance_key=INST)
|
||||
intents = IntentService(
|
||||
svc, repo, InMemoryIntentStore(),
|
||||
svc,
|
||||
repo,
|
||||
InMemoryIntentStore(),
|
||||
resolve_symbol=SYMBOLS.get,
|
||||
instance_key=INST, max_active_slots=max_active_slots,
|
||||
instance_key=INST,
|
||||
max_active_slots=max_active_slots,
|
||||
)
|
||||
execution = ExecutionService(svc, repo, broker, instance_key=INST)
|
||||
repo.reconcile_universe(INST, SYMBOLS) # permanent asset slots
|
||||
@@ -29,9 +34,7 @@ def _wire(broker, max_active_slots=None):
|
||||
|
||||
|
||||
def _buy(intents, iid="b1", pid="PF-1", qty=10):
|
||||
return intents.submit_buy(
|
||||
BuyIntent(intent_id=iid, portfolio_id=pid, qty=qty, entry_price=1000)
|
||||
)
|
||||
return intents.submit_buy(BuyIntent(intent_id=iid, portfolio_id=pid, qty=qty, entry_price=1000))
|
||||
|
||||
|
||||
def test_buy_lifecycle_to_holding():
|
||||
@@ -48,13 +51,93 @@ def test_buy_dispatch_is_idempotent_no_double_order():
|
||||
broker = PaperBroker(fill_after_polls=5) # stays accepted (not filled) across ticks
|
||||
repo, svc, intents, execution = _wire(broker)
|
||||
sid = _buy(intents).slot_id
|
||||
execution.run_once() # dispatch (1 order) + poll(accepted)
|
||||
execution.run_once() # dispatch (1 order) + poll(accepted)
|
||||
bid1 = repo.fetch_slot(sid).broker_order_id
|
||||
execution.run_once() # must NOT re-dispatch
|
||||
execution.run_once() # must NOT re-dispatch
|
||||
bid2 = repo.fetch_slot(sid).broker_order_id
|
||||
assert bid1 == bid2 and len(broker._orders) == 1 # exactly one broker order
|
||||
|
||||
|
||||
def test_unknown_submit_outcome_stays_dispatched_without_retry():
|
||||
class UnknownOutcomeBroker(PaperBroker):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.submit_count = 0
|
||||
self.cancel_count = 0
|
||||
|
||||
def submit_order(self, req):
|
||||
self.submit_count += 1
|
||||
return OrderAck(
|
||||
accepted=True,
|
||||
broker_order_id=f"submission-unknown:{req.slot_id}",
|
||||
reason="submit_outcome_unknown:ReadTimeout",
|
||||
)
|
||||
|
||||
def poll_fill(self, broker_order_id, *, side, symbol):
|
||||
return FillStatus(state="unknown")
|
||||
|
||||
def cancel_order(self, broker_order_id, *, side, symbol, qty, venue="KRX"):
|
||||
self.cancel_count += 1
|
||||
return False
|
||||
|
||||
broker = UnknownOutcomeBroker()
|
||||
repo, svc, intents, execution = _wire(broker)
|
||||
sid = _buy(intents).slot_id
|
||||
|
||||
first = execution.run_once()
|
||||
second = execution.run_once()
|
||||
|
||||
slot = repo.fetch_slot(sid)
|
||||
assert first.dispatched == 1
|
||||
assert second.dispatched == 0
|
||||
assert broker.submit_count == 1
|
||||
assert broker.cancel_count == 0
|
||||
assert slot.status is SlotState.PENDING_BUY
|
||||
assert slot.broker_order_id == f"submission-unknown:{sid}"
|
||||
|
||||
|
||||
def test_unknown_submit_resolution_is_persisted_before_restart_reconcile():
|
||||
class ResolvedOutcomeBroker(PaperBroker):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.submit_count = 0
|
||||
self.poll_count = 0
|
||||
|
||||
def submit_order(self, req):
|
||||
self.submit_count += 1
|
||||
return OrderAck(
|
||||
accepted=True,
|
||||
broker_order_id=f"submission-unknown:{req.slot_id}",
|
||||
reason="submit_outcome_unknown:ReadTimeout",
|
||||
)
|
||||
|
||||
def poll_fill(self, broker_order_id, *, side, symbol):
|
||||
self.poll_count += 1
|
||||
if self.poll_count == 1:
|
||||
return FillStatus(
|
||||
state="accepted",
|
||||
raw={"resolved_broker_order_id": "91258:0012345678"},
|
||||
)
|
||||
return FillStatus(
|
||||
state="filled",
|
||||
filled_qty=10,
|
||||
avg_price=1000,
|
||||
execution_id="resolved-fill-1",
|
||||
)
|
||||
|
||||
broker = ResolvedOutcomeBroker()
|
||||
repo, svc, intents, execution = _wire(broker)
|
||||
sid = _buy(intents).slot_id
|
||||
|
||||
execution.run_once()
|
||||
assert repo.fetch_slot(sid).broker_order_id == "91258:0012345678"
|
||||
|
||||
execution.reconcile()
|
||||
slot = repo.fetch_slot(sid)
|
||||
assert slot.status is SlotState.HOLDING
|
||||
assert broker.submit_count == 1
|
||||
|
||||
|
||||
def test_buy_reject_rolls_back_to_inactive():
|
||||
repo, svc, intents, execution = _wire(PaperBroker(reject_symbols={"005931"}))
|
||||
sid = _buy(intents).slot_id
|
||||
@@ -156,8 +239,7 @@ def test_sell_skipped_when_broker_flat():
|
||||
sid = _buy(intents).slot_id
|
||||
execution.run_once() # holding (broker balance 10)
|
||||
broker._orders.clear() # simulate divergence: broker is actually flat
|
||||
svc.transit_slot(sid, SlotState.HOLDING, SlotState.PENDING_SELL,
|
||||
reason="r", actor="t")
|
||||
svc.transit_slot(sid, SlotState.HOLDING, SlotState.PENDING_SELL, reason="r", actor="t")
|
||||
r = execution.run_once()
|
||||
assert r.sell_skipped_no_balance == 1
|
||||
assert r.dispatched == 0
|
||||
@@ -172,8 +254,7 @@ def test_skipped_sell_does_not_block_other_dispatches():
|
||||
sid = _buy(intents, iid="b1", pid="PF-1").slot_id
|
||||
execution.run_once() # PF-1 holding
|
||||
broker._orders.clear() # broker flat for PF-1's symbol
|
||||
svc.transit_slot(sid, SlotState.HOLDING, SlotState.PENDING_SELL,
|
||||
reason="r", actor="t")
|
||||
svc.transit_slot(sid, SlotState.HOLDING, SlotState.PENDING_SELL, reason="r", actor="t")
|
||||
d2 = _buy(intents, iid="b2", pid="PF-2") # a buy behind the stuck sell
|
||||
r = execution.run_once()
|
||||
assert r.sell_skipped_no_balance == 1
|
||||
@@ -185,10 +266,10 @@ def test_reconcile_after_crash_completes_fill_without_redispatch():
|
||||
broker = PaperBroker(fill_after_polls=1)
|
||||
repo, svc, intents, execution = _wire(broker)
|
||||
sid = _buy(intents).slot_id
|
||||
execution.run_once() # dispatch + poll1 (accepted, not filled) — simulates crash here
|
||||
execution.run_once() # dispatch + poll1 (accepted, not filled) — simulates crash here
|
||||
assert repo.fetch_slot(sid).status is SlotState.PENDING_BUY
|
||||
orders_before = len(broker._orders)
|
||||
execution.reconcile() # restart: re-poll -> fills, no new order
|
||||
execution.reconcile() # restart: re-poll -> fills, no new order
|
||||
slot = repo.fetch_slot(sid)
|
||||
assert slot.status is SlotState.HOLDING
|
||||
assert len(broker._orders) == orders_before # reconcile did not submit a new order
|
||||
@@ -197,7 +278,8 @@ def test_reconcile_after_crash_completes_fill_without_redispatch():
|
||||
def test_capacity_full_blocks_further_buys():
|
||||
"""dcy-020/REQ-059: at M active slots further buys reject capacity_full."""
|
||||
repo, svc, intents, execution = _wire(
|
||||
PaperBroker(fill_after_polls=0), max_active_slots=2,
|
||||
PaperBroker(fill_after_polls=0),
|
||||
max_active_slots=2,
|
||||
)
|
||||
assert _buy(intents, iid="b0", pid="PF-0").status == "admitted"
|
||||
assert _buy(intents, iid="b1", pid="PF-1").status == "admitted"
|
||||
|
||||
Reference in New Issue
Block a user