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,58 +199,54 @@ 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 {
'action': 'alert',
}:
resp = 'alert_triggered'
if action == 'alert': # executable order submission
# nothing to do but relay a status case {
# message back to the requesting ems client 'action': action,
resp = 'alert_triggered' '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
else: # executable order submission log.info(
f'Dark order triggered for price {price}\n'
f'Submitting order @ price {submit_price}')
# submit_price = price + price*percent_away live_req = BrokerdOrder(
submit_price = price + abs_diff_away action=action,
oid=oid,
account=account,
time_ns=time.time_ns(),
symbol=bfqsn,
price=submit_price,
size=size,
)
await brokerd_orders_stream.send(live_req)
log.info( # mark this entry as having sent an order
f'Dark order triggered for price {price}\n' # request. the entry will be replaced once the
f'Submitting order @ price {submit_price}') # target broker replies back with
# a ``BrokerdOrderAck`` msg including the
# allocated unique ``BrokerdOrderAck.reqid`` key
# generated by the broker's own systems.
book._ems_entries[oid] = live_req
msg = BrokerdOrder( case _:
action=cmd['action'], raise ValueError(f'Invalid dark book entry: {cmd}')
oid=oid,
account=cmd['account'],
time_ns=time.time_ns(),
# this **creates** new order request for the # fallthrough logic
# underlying broker so we set a "broker resp = Status(
# request id" (``reqid`` kwarg) to ``None`` oid=oid, # ems dialog id
# 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'],
)
await brokerd_orders_stream.send(msg)
# mark this entry as having sent an order
# request. the entry will be replaced once the
# target broker replies back with
# a ``BrokerdOrderAck`` msg including the
# allocated unique ``BrokerdOrderAck.reqid`` key
# generated by the broker's own systems.
book._ems_entries[oid] = msg
# our internal status value for client-side
# triggered "dark orders"
resp = 'dark_triggered'
msg = Status(
oid=oid, # ems order 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',