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,58 +199,54 @@ 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}', '')
match cmd:
# alert: nothing to do but relay a status
# back to the requesting ems client
case {
'action': 'alert',
}:
resp = 'alert_triggered'
if action == 'alert':
# nothing to do but relay a status
# message back to the requesting ems client
resp = 'alert_triggered'
# 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
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
submit_price = price + abs_diff_away
live_req = BrokerdOrder(
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(
f'Dark order triggered for price {price}\n'
f'Submitting order @ price {submit_price}')
# 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] = live_req
msg = BrokerdOrder(
action=cmd['action'],
oid=oid,
account=cmd['account'],
time_ns=time.time_ns(),
case _:
raise ValueError(f'Invalid dark book entry: {cmd}')
# 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'],
)
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
# 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',