Matchify the dark trigger loop

the_ems_flattening
Tyler Goodlet 2022-08-04 15:42:31 -04:00
parent 2309e7ab05
commit ae001c3dd7
1 changed files with 46 additions and 51 deletions

View File

@ -174,10 +174,9 @@ async def clear_dark_triggers(
)
):
price = tick.get('price')
ttype = tick['type']
# update to keep new cmds informed
book.lasts[sym] = price
ttype = tick['type']
for oid, (
pred,
@ -200,43 +199,39 @@ async def clear_dark_triggers(
# majority of iterations will be non-matches
continue
action: str = cmd['action']
symbol: str = cmd['symbol']
bfqsn: str = symbol.replace(f'.{broker}', '')
if action == 'alert':
# nothing to do but relay a status
# message back to the requesting ems client
match cmd:
# alert: nothing to do but relay a status
# back to the requesting ems client
case {
'action': 'alert',
}:
resp = 'alert_triggered'
else: # executable order submission
# submit_price = price + price*percent_away
# executable order submission
case {
'action': action,
'symbol': symbol,
'account': account,
'size': size,
}:
bfqsn: str = symbol.replace(f'.{broker}', '')
submit_price = price + abs_diff_away
resp = 'dark_triggered' # hidden on client-side
log.info(
f'Dark order triggered for price {price}\n'
f'Submitting order @ price {submit_price}')
msg = BrokerdOrder(
action=cmd['action'],
live_req = BrokerdOrder(
action=action,
oid=oid,
account=cmd['account'],
account=account,
time_ns=time.time_ns(),
# this **creates** new order request for the
# underlying broker so we set a "broker
# request id" (``reqid`` kwarg) to ``None``
# so that the broker client knows that we
# aren't trying to modify an existing
# order-request and instead create a new one.
reqid=None,
symbol=bfqsn,
price=submit_price,
size=cmd['size'],
size=size,
)
await brokerd_orders_stream.send(msg)
await brokerd_orders_stream.send(live_req)
# mark this entry as having sent an order
# request. the entry will be replaced once the
@ -244,14 +239,14 @@ async def clear_dark_triggers(
# a ``BrokerdOrderAck`` msg including the
# allocated unique ``BrokerdOrderAck.reqid`` key
# generated by the broker's own systems.
book._ems_entries[oid] = msg
book._ems_entries[oid] = live_req
# our internal status value for client-side
# triggered "dark orders"
resp = 'dark_triggered'
case _:
raise ValueError(f'Invalid dark book entry: {cmd}')
msg = Status(
oid=oid, # ems order id
# fallthrough logic
resp = Status(
oid=oid, # ems dialog id
time_ns=time.time_ns(),
resp=resp,
trigger_price=price,
@ -266,13 +261,14 @@ async def clear_dark_triggers(
f'pred for {oid} was already removed!?'
)
# send response to client-side
try:
await ems_client_order_stream.send(msg)
await ems_client_order_stream.send(resp)
except (
trio.ClosedResourceError,
):
log.warning(
f'client {ems_client_order_stream} stream is broke'
f'{ems_client_order_stream} stream broke?'
)
break
@ -684,7 +680,6 @@ async def translate_and_relay_brokerd_events(
# about. In most default situations, with composed orders
# (ex. brackets), most brokers seem to use a oca policy.
# BrokerdStatus
case {
'name': 'status',