diff --git a/piker/ui/order_mode.py b/piker/ui/order_mode.py index 27059894..8b1d1a14 100644 --- a/piker/ui/order_mode.py +++ b/piker/ui/order_mode.py @@ -321,97 +321,95 @@ async def start_order_mode( async with open_ems( brokername, symbol, - ) as (book, trades_stream): + ) as (book, trades_stream), open_order_mode( + symbol, + chart, + book, + ) as order_mode: - async with open_order_mode( - symbol, - chart, - book, - ) as order_mode: + def get_index(time: float): - def get_index(time: float): + # XXX: not sure why the time is so off here + # looks like we're gonna have to do some fixing.. - # XXX: not sure why the time is so off here - # looks like we're gonna have to do some fixing.. + ohlc = chart._shm.array + indexes = ohlc['time'] >= time - ohlc = chart._shm.array - indexes = ohlc['time'] >= time + if any(indexes): + return ohlc['index'][indexes[-1]] + else: + return ohlc['index'][-1] - if any(indexes): - return ohlc['index'][indexes[-1]] - else: - return ohlc['index'][-1] + # Begin order-response streaming - # Begin order-response streaming + # this is where we receive **back** messages + # about executions **from** the EMS actor + async for msg in trades_stream: - # this is where we receive **back** messages - # about executions **from** the EMS actor - async for msg in trades_stream: + fmsg = pformat(msg) + log.info(f'Received order msg:\n{fmsg}') - fmsg = pformat(msg) - log.info(f'Received order msg:\n{fmsg}') + resp = msg['resp'] - resp = msg['resp'] + if resp in ( + 'position', + ): + # show line label once order is live + order_mode.on_position_update(msg) + continue - if resp in ( - 'position', - ): - # show line label once order is live - order_mode.on_position_update(msg) - continue + # delete the line from view + oid = msg['oid'] - # delete the line from view - oid = msg['oid'] + # response to 'action' request (buy/sell) + if resp in ( + 'dark_submitted', + 'broker_submitted' + ): - # response to 'action' request (buy/sell) - if resp in ( - 'dark_submitted', - 'broker_submitted' - ): + # show line label once order is live + order_mode.on_submit(oid) - # show line label once order is live - order_mode.on_submit(oid) + # resp to 'cancel' request or error condition + # for action request + elif resp in ( + 'broker_cancelled', + 'broker_inactive', + 'dark_cancelled' + ): + # delete level line from view + order_mode.on_cancel(oid) - # resp to 'cancel' request or error condition - # for action request - elif resp in ( - 'broker_cancelled', - 'broker_inactive', - 'dark_cancelled' - ): - # delete level line from view - order_mode.on_cancel(oid) + elif resp in ( + 'dark_executed' + ): + log.info(f'Dark order triggered for {fmsg}') - elif resp in ( - 'dark_executed' - ): - log.info(f'Dark order triggered for {fmsg}') + # for alerts add a triangle and remove the + # level line + if msg['cmd']['action'] == 'alert': - # for alerts add a triangle and remove the - # level line - if msg['cmd']['action'] == 'alert': - - # should only be one "fill" for an alert - order_mode.on_fill( - oid, - price=msg['trigger_price'], - arrow_index=get_index(time.time()) - ) - await order_mode.on_exec(oid, msg) - - # response to completed 'action' request for buy/sell - elif resp in ( - 'broker_executed', - ): - await order_mode.on_exec(oid, msg) - - # each clearing tick is responded individually - elif resp in ('broker_filled',): - action = msg['action'] - # TODO: some kinda progress system + # should only be one "fill" for an alert order_mode.on_fill( oid, - price=msg['price'], - arrow_index=get_index(msg['broker_time']), - pointing='up' if action == 'buy' else 'down', + price=msg['trigger_price'], + arrow_index=get_index(time.time()) ) + await order_mode.on_exec(oid, msg) + + # response to completed 'action' request for buy/sell + elif resp in ( + 'broker_executed', + ): + await order_mode.on_exec(oid, msg) + + # each clearing tick is responded individually + elif resp in ('broker_filled',): + action = msg['action'] + # TODO: some kinda progress system + order_mode.on_fill( + oid, + price=msg['price'], + arrow_index=get_index(msg['broker_time']), + pointing='up' if action == 'buy' else 'down', + )