Nest async withs
parent
a18a4b5a4c
commit
3506bbe05c
|
@ -321,97 +321,95 @@ async def start_order_mode(
|
||||||
async with open_ems(
|
async with open_ems(
|
||||||
brokername,
|
brokername,
|
||||||
symbol,
|
symbol,
|
||||||
) as (book, trades_stream):
|
) as (book, trades_stream), open_order_mode(
|
||||||
|
symbol,
|
||||||
|
chart,
|
||||||
|
book,
|
||||||
|
) as order_mode:
|
||||||
|
|
||||||
async with open_order_mode(
|
def get_index(time: float):
|
||||||
symbol,
|
|
||||||
chart,
|
|
||||||
book,
|
|
||||||
) as order_mode:
|
|
||||||
|
|
||||||
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
|
ohlc = chart._shm.array
|
||||||
# looks like we're gonna have to do some fixing..
|
indexes = ohlc['time'] >= time
|
||||||
|
|
||||||
ohlc = chart._shm.array
|
if any(indexes):
|
||||||
indexes = ohlc['time'] >= time
|
return ohlc['index'][indexes[-1]]
|
||||||
|
else:
|
||||||
|
return ohlc['index'][-1]
|
||||||
|
|
||||||
if any(indexes):
|
# Begin order-response streaming
|
||||||
return ohlc['index'][indexes[-1]]
|
|
||||||
else:
|
|
||||||
return ohlc['index'][-1]
|
|
||||||
|
|
||||||
# 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
|
fmsg = pformat(msg)
|
||||||
# about executions **from** the EMS actor
|
log.info(f'Received order msg:\n{fmsg}')
|
||||||
async for msg in trades_stream:
|
|
||||||
|
|
||||||
fmsg = pformat(msg)
|
resp = msg['resp']
|
||||||
log.info(f'Received order msg:\n{fmsg}')
|
|
||||||
|
|
||||||
resp = msg['resp']
|
if resp in (
|
||||||
|
'position',
|
||||||
|
):
|
||||||
|
# show line label once order is live
|
||||||
|
order_mode.on_position_update(msg)
|
||||||
|
continue
|
||||||
|
|
||||||
if resp in (
|
# delete the line from view
|
||||||
'position',
|
oid = msg['oid']
|
||||||
):
|
|
||||||
# show line label once order is live
|
|
||||||
order_mode.on_position_update(msg)
|
|
||||||
continue
|
|
||||||
|
|
||||||
# delete the line from view
|
# response to 'action' request (buy/sell)
|
||||||
oid = msg['oid']
|
if resp in (
|
||||||
|
'dark_submitted',
|
||||||
|
'broker_submitted'
|
||||||
|
):
|
||||||
|
|
||||||
# response to 'action' request (buy/sell)
|
# show line label once order is live
|
||||||
if resp in (
|
order_mode.on_submit(oid)
|
||||||
'dark_submitted',
|
|
||||||
'broker_submitted'
|
|
||||||
):
|
|
||||||
|
|
||||||
# show line label once order is live
|
# resp to 'cancel' request or error condition
|
||||||
order_mode.on_submit(oid)
|
# 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
|
elif resp in (
|
||||||
# for action request
|
'dark_executed'
|
||||||
elif resp in (
|
):
|
||||||
'broker_cancelled',
|
log.info(f'Dark order triggered for {fmsg}')
|
||||||
'broker_inactive',
|
|
||||||
'dark_cancelled'
|
|
||||||
):
|
|
||||||
# delete level line from view
|
|
||||||
order_mode.on_cancel(oid)
|
|
||||||
|
|
||||||
elif resp in (
|
# for alerts add a triangle and remove the
|
||||||
'dark_executed'
|
# level line
|
||||||
):
|
if msg['cmd']['action'] == 'alert':
|
||||||
log.info(f'Dark order triggered for {fmsg}')
|
|
||||||
|
|
||||||
# for alerts add a triangle and remove the
|
# should only be one "fill" for an alert
|
||||||
# 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
|
|
||||||
order_mode.on_fill(
|
order_mode.on_fill(
|
||||||
oid,
|
oid,
|
||||||
price=msg['price'],
|
price=msg['trigger_price'],
|
||||||
arrow_index=get_index(msg['broker_time']),
|
arrow_index=get_index(time.time())
|
||||||
pointing='up' if action == 'buy' else 'down',
|
|
||||||
)
|
)
|
||||||
|
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',
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue