155 lines
5.0 KiB
Python
155 lines
5.0 KiB
Python
|
|
'''
|
||
|
|
Offline broker-event routing through the EMS order lifecycle.
|
||
|
|
|
||
|
|
'''
|
||
|
|
from copy import copy
|
||
|
|
from decimal import Decimal
|
||
|
|
from types import SimpleNamespace
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
import trio
|
||
|
|
|
||
|
|
from piker.clearing._ems import (
|
||
|
|
DarkBook,
|
||
|
|
translate_and_relay_brokerd_events,
|
||
|
|
)
|
||
|
|
from piker.clearing._messages import Order, Status
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize('closing_status', ['canceled', 'closed'])
|
||
|
|
@pytest.mark.parametrize('late_error', [False, True])
|
||
|
|
@pytest.mark.parametrize('reopen', [None, 'pending', 'open'])
|
||
|
|
def test_closed_order_retains_fill_routing(
|
||
|
|
closing_status: str,
|
||
|
|
late_error: bool,
|
||
|
|
reopen: str|None,
|
||
|
|
) -> None:
|
||
|
|
'''
|
||
|
|
Route late fills using the original ``Order`` after closure.
|
||
|
|
|
||
|
|
A ``'canceled'`` status removed ``DarkBook._active``'s request,
|
||
|
|
then a broker error recreated ``Error(req=None)``. A subsequent
|
||
|
|
fill raised ``AttributeError``; without the error, that fill was
|
||
|
|
silently lost. Replay ``'canceled'`` and ``'closed'`` with and
|
||
|
|
without the intervening error and a pending/open broker update.
|
||
|
|
|
||
|
|
- Drive the actual EMS event loop with a finite ``trio`` memory
|
||
|
|
stream, capturing broadcasts before each ``Status`` changes.
|
||
|
|
- Assert the FQME route, original ``Order`` identity, and fill
|
||
|
|
payload: a symbol-less fill needs ``Order.symbol`` to reach
|
||
|
|
the client subscribed to that market endpoint.
|
||
|
|
- Keep canceled/closed dialogs out of ``DarkBook._active`` unless
|
||
|
|
a subsequent pending/open status reopens them.
|
||
|
|
- Send a second order's fill to prove processing continues.
|
||
|
|
|
||
|
|
'''
|
||
|
|
async def main() -> None:
|
||
|
|
book = DarkBook(broker='ib')
|
||
|
|
req = Order(
|
||
|
|
action='buy',
|
||
|
|
exec_mode='live',
|
||
|
|
oid='order-233',
|
||
|
|
symbol='qqq.nasdaq.ib',
|
||
|
|
account='ib.algopaper',
|
||
|
|
price=Decimal('687.98'),
|
||
|
|
size=1.0,
|
||
|
|
brokers=['ib'],
|
||
|
|
)
|
||
|
|
next_req = copy(req)
|
||
|
|
next_req.oid = 'next-order'
|
||
|
|
for order in (req, next_req):
|
||
|
|
book._active[order.oid] = Status(
|
||
|
|
time_ns=1,
|
||
|
|
oid=order.oid,
|
||
|
|
resp='pending',
|
||
|
|
req=order,
|
||
|
|
)
|
||
|
|
|
||
|
|
events: list[dict] = [
|
||
|
|
{'name': 'ack', 'oid': req.oid, 'reqid': '233'},
|
||
|
|
{
|
||
|
|
'name': 'ack',
|
||
|
|
'oid': next_req.oid,
|
||
|
|
'reqid': '234',
|
||
|
|
},
|
||
|
|
{
|
||
|
|
'name': 'status',
|
||
|
|
'reqid': '233',
|
||
|
|
'time_ns': 2,
|
||
|
|
'status': closing_status,
|
||
|
|
'broker_details': {'name': 'ib'},
|
||
|
|
},
|
||
|
|
]
|
||
|
|
if late_error:
|
||
|
|
events.append({
|
||
|
|
'name': 'error',
|
||
|
|
'oid': None,
|
||
|
|
'reqid': '233',
|
||
|
|
'reason': 'Order TIF was set to DAY',
|
||
|
|
'broker_details': {'name': 'ib', 'flow': {}},
|
||
|
|
})
|
||
|
|
if reopen is not None:
|
||
|
|
events.append({
|
||
|
|
'name': 'status',
|
||
|
|
'reqid': '233',
|
||
|
|
'time_ns': 3,
|
||
|
|
'status': reopen,
|
||
|
|
'broker_details': {'name': 'ib'},
|
||
|
|
})
|
||
|
|
for reqid in ('233', '234'):
|
||
|
|
events.append({
|
||
|
|
'name': 'fill',
|
||
|
|
'time_ns': 4,
|
||
|
|
'broker_time': 1793402932.0,
|
||
|
|
'reqid': reqid,
|
||
|
|
'size': 1.0,
|
||
|
|
'price': 687.78,
|
||
|
|
'action': 'buy',
|
||
|
|
'broker_details': {},
|
||
|
|
})
|
||
|
|
|
||
|
|
sent: list[tuple[str, str, Order, object]] = []
|
||
|
|
|
||
|
|
async def capture(fqme: str, status: Status) -> None:
|
||
|
|
if status.resp == 'error':
|
||
|
|
assert req.oid not in book._active
|
||
|
|
sent.append((
|
||
|
|
fqme, status.resp, status.req, status.brokerd_msg,
|
||
|
|
))
|
||
|
|
|
||
|
|
tx, rx = trio.open_memory_channel(len(events))
|
||
|
|
router = SimpleNamespace(
|
||
|
|
get_dark_book=lambda _: book,
|
||
|
|
relays={'ib': SimpleNamespace(brokerd_stream=rx)},
|
||
|
|
client_broadcast=capture,
|
||
|
|
)
|
||
|
|
async with tx, rx:
|
||
|
|
for event in events:
|
||
|
|
tx.send_nowait(event)
|
||
|
|
await tx.aclose()
|
||
|
|
await translate_and_relay_brokerd_events(
|
||
|
|
'ib', rx, router,
|
||
|
|
)
|
||
|
|
|
||
|
|
expected: list[str] = [closing_status]
|
||
|
|
if late_error:
|
||
|
|
expected.append('error')
|
||
|
|
if reopen is not None:
|
||
|
|
expected.append(reopen)
|
||
|
|
expected.extend(['fill', 'fill'])
|
||
|
|
assert [resp for _, resp, _, _ in sent] == expected
|
||
|
|
assert all(fqme == req.symbol for fqme, _, _, _ in sent)
|
||
|
|
assert all(order is req for _, _, order, _ in sent[:-1])
|
||
|
|
assert sent[-1][2] is next_req
|
||
|
|
assert sent[-2][3].reqid == '233'
|
||
|
|
assert sent[-2][3].price == 687.78
|
||
|
|
assert sent[-2][3].size == 1.0
|
||
|
|
if reopen is None:
|
||
|
|
assert req.oid not in book._active
|
||
|
|
assert book._closed[req.oid].req is req
|
||
|
|
else:
|
||
|
|
assert req.oid not in book._closed
|
||
|
|
assert book._active[req.oid].req is req
|
||
|
|
|
||
|
|
trio.run(main)
|