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