Deliver existing dialog (msgs) to every EMS client
Ideally every client that connects to the ems can know its state (immediately) meaning relay all the order dialogs that are currently active. This adds full (hacky WIP) support to receive those dialog (msgs) from the `open_ems()` startup values via the `.started()` msg from `_emsd_main()`. Further this adds support to the order mode chart-UI to display existing (live) orders on the chart during startup. Details include, - add a `OrderMode.load_unknown_dialog_from_msg()` for processing and displaying a ``BrokerdStatus`` (for now) msg from the EMS that was not previously created by the current ems client and registering and displaying it on the chart. - break out the ems msg processing into a new `order_mode.process_trade_msg()` func so that it can be called on the startup dialog-msg set as well as eventually used a more general low level auto-strat API (eg. when we get to displaying auto-strat and group trading automatically on an observing chart UI. - hackyness around msg-processing for the dialogs delivery since we're technically delivering `BrokerdStatus` msgs when the client-side processing technically expects `Status` msgs.. we'll rectify this soon!open_order_loading
parent
1cfa04927d
commit
2548aae73d
|
@ -224,11 +224,19 @@ async def open_ems(
|
||||||
fqsn=fqsn,
|
fqsn=fqsn,
|
||||||
exec_mode=mode,
|
exec_mode=mode,
|
||||||
|
|
||||||
) as (ctx, (positions, accounts)),
|
) as (
|
||||||
|
ctx,
|
||||||
|
(
|
||||||
|
positions,
|
||||||
|
accounts,
|
||||||
|
dialogs,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
|
||||||
# open 2-way trade command stream
|
# open 2-way trade command stream
|
||||||
ctx.open_stream() as trades_stream,
|
ctx.open_stream() as trades_stream,
|
||||||
):
|
):
|
||||||
|
# start sync code order msg delivery task
|
||||||
async with trio.open_nursery() as n:
|
async with trio.open_nursery() as n:
|
||||||
n.start_soon(
|
n.start_soon(
|
||||||
relay_order_cmds_from_sync_code,
|
relay_order_cmds_from_sync_code,
|
||||||
|
@ -236,4 +244,10 @@ async def open_ems(
|
||||||
trades_stream
|
trades_stream
|
||||||
)
|
)
|
||||||
|
|
||||||
yield book, trades_stream, positions, accounts
|
yield (
|
||||||
|
book,
|
||||||
|
trades_stream,
|
||||||
|
positions,
|
||||||
|
accounts,
|
||||||
|
dialogs,
|
||||||
|
)
|
||||||
|
|
|
@ -517,6 +517,48 @@ class OrderMode:
|
||||||
|
|
||||||
return ids
|
return ids
|
||||||
|
|
||||||
|
def load_unknown_dialog_from_msg(
|
||||||
|
self,
|
||||||
|
msg: dict,
|
||||||
|
|
||||||
|
) -> OrderDialog:
|
||||||
|
|
||||||
|
oid = str(msg['oid'])
|
||||||
|
size = msg['brokerd_msg']['size']
|
||||||
|
if size >= 0:
|
||||||
|
action = 'buy'
|
||||||
|
else:
|
||||||
|
action = 'sell'
|
||||||
|
|
||||||
|
acct = msg['brokerd_msg']['account']
|
||||||
|
price = msg['brokerd_msg']['price']
|
||||||
|
deats = msg['brokerd_msg']['broker_details']
|
||||||
|
fqsn = (
|
||||||
|
deats['fqsn'] + '.' + deats['name']
|
||||||
|
)
|
||||||
|
symbol = Symbol.from_fqsn(
|
||||||
|
fqsn=fqsn,
|
||||||
|
info={},
|
||||||
|
)
|
||||||
|
# map to order composite-type
|
||||||
|
order = Order(
|
||||||
|
action=action,
|
||||||
|
price=price,
|
||||||
|
account=acct,
|
||||||
|
size=size,
|
||||||
|
symbol=symbol,
|
||||||
|
brokers=symbol.brokers,
|
||||||
|
oid=oid,
|
||||||
|
exec_mode='live', # dark or live
|
||||||
|
)
|
||||||
|
|
||||||
|
dialog = self.submit_order(
|
||||||
|
send_msg=False,
|
||||||
|
order=order,
|
||||||
|
)
|
||||||
|
assert self.dialogs[oid] == dialog
|
||||||
|
return dialog
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def open_order_mode(
|
async def open_order_mode(
|
||||||
|
@ -554,6 +596,7 @@ async def open_order_mode(
|
||||||
trades_stream,
|
trades_stream,
|
||||||
position_msgs,
|
position_msgs,
|
||||||
brokerd_accounts,
|
brokerd_accounts,
|
||||||
|
ems_dialog_msgs,
|
||||||
),
|
),
|
||||||
trio.open_nursery() as tn,
|
trio.open_nursery() as tn,
|
||||||
|
|
||||||
|
@ -760,37 +803,59 @@ async def open_order_mode(
|
||||||
# to handle input since the ems connection is ready
|
# to handle input since the ems connection is ready
|
||||||
started.set()
|
started.set()
|
||||||
|
|
||||||
|
for oid, msg in ems_dialog_msgs.items():
|
||||||
|
|
||||||
|
# HACK ALERT: ensure a resp field is filled out since
|
||||||
|
# techincally the call below expects a ``Status``. TODO:
|
||||||
|
# parse into proper ``Status`` equivalents ems-side?
|
||||||
|
msg.setdefault('resp', msg['broker_details']['resp'])
|
||||||
|
msg.setdefault('oid', msg['broker_details']['oid'])
|
||||||
|
msg['brokerd_msg'] = msg
|
||||||
|
|
||||||
|
await process_trade_msg(
|
||||||
|
mode,
|
||||||
|
book,
|
||||||
|
msg,
|
||||||
|
)
|
||||||
|
|
||||||
tn.start_soon(
|
tn.start_soon(
|
||||||
process_trades_and_update_ui,
|
process_trades_and_update_ui,
|
||||||
tn,
|
|
||||||
feed,
|
|
||||||
mode,
|
|
||||||
trades_stream,
|
trades_stream,
|
||||||
|
mode,
|
||||||
book,
|
book,
|
||||||
)
|
)
|
||||||
|
|
||||||
yield mode
|
yield mode
|
||||||
|
|
||||||
|
|
||||||
async def process_trades_and_update_ui(
|
async def process_trades_and_update_ui(
|
||||||
|
|
||||||
n: trio.Nursery,
|
|
||||||
feed: Feed,
|
|
||||||
mode: OrderMode,
|
|
||||||
trades_stream: tractor.MsgStream,
|
trades_stream: tractor.MsgStream,
|
||||||
|
mode: OrderMode,
|
||||||
book: OrderBook,
|
book: OrderBook,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
get_index = mode.chart.get_index
|
|
||||||
global _pnl_tasks
|
|
||||||
|
|
||||||
# this is where we receive **back** messages
|
# this is where we receive **back** messages
|
||||||
# about executions **from** the EMS actor
|
# about executions **from** the EMS actor
|
||||||
async for msg in trades_stream:
|
async for msg in trades_stream:
|
||||||
|
await process_trade_msg(
|
||||||
|
mode,
|
||||||
|
book,
|
||||||
|
msg,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def process_trade_msg(
|
||||||
|
mode: OrderMode,
|
||||||
|
book: OrderBook,
|
||||||
|
msg: dict,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
get_index = mode.chart.get_index
|
||||||
fmsg = pformat(msg)
|
fmsg = pformat(msg)
|
||||||
log.info(f'Received order msg:\n{fmsg}')
|
log.info(f'Received order msg:\n{fmsg}')
|
||||||
|
|
||||||
name = msg['name']
|
name = msg['name']
|
||||||
if name in (
|
if name in (
|
||||||
'position',
|
'position',
|
||||||
|
@ -816,51 +881,18 @@ async def process_trades_and_update_ui(
|
||||||
|
|
||||||
# short circuit to next msg to avoid
|
# short circuit to next msg to avoid
|
||||||
# unnecessary msg content lookups
|
# unnecessary msg content lookups
|
||||||
continue
|
return
|
||||||
|
# continue
|
||||||
|
|
||||||
resp = msg['resp']
|
resp = msg['resp']
|
||||||
oid = str(msg['oid'])
|
oid = str(msg['oid'])
|
||||||
dialog = mode.dialogs.get(oid)
|
dialog = mode.dialogs.get(oid)
|
||||||
|
|
||||||
if dialog is None:
|
if dialog is None:
|
||||||
log.warning(f'received msg for untracked dialog:\n{fmsg}')
|
log.warning(
|
||||||
|
f'received msg for untracked dialog:\n{fmsg}'
|
||||||
size = msg['brokerd_msg']['size']
|
|
||||||
if size >= 0:
|
|
||||||
action = 'buy'
|
|
||||||
else:
|
|
||||||
action = 'sell'
|
|
||||||
|
|
||||||
acct = msg['brokerd_msg']['account']
|
|
||||||
price = msg['brokerd_msg']['price']
|
|
||||||
deats = msg['brokerd_msg']['broker_details']
|
|
||||||
fqsn = (
|
|
||||||
deats['fqsn'] + '.' + deats['name']
|
|
||||||
)
|
)
|
||||||
symbol = Symbol.from_fqsn(
|
dialog = mode.load_unknown_dialog_from_msg(msg)
|
||||||
fqsn=fqsn,
|
|
||||||
info={},
|
|
||||||
)
|
|
||||||
# map to order composite-type
|
|
||||||
order = Order(
|
|
||||||
action=action,
|
|
||||||
price=price,
|
|
||||||
account=acct,
|
|
||||||
size=size,
|
|
||||||
symbol=symbol,
|
|
||||||
brokers=symbol.brokers,
|
|
||||||
oid=oid,
|
|
||||||
exec_mode='live', # dark or live
|
|
||||||
)
|
|
||||||
|
|
||||||
dialog = mode.submit_order(
|
|
||||||
send_msg=False,
|
|
||||||
order=order,
|
|
||||||
)
|
|
||||||
|
|
||||||
# # TODO: enable pure tracking / mirroring of dialogs
|
|
||||||
# # is desired.
|
|
||||||
# continue
|
|
||||||
|
|
||||||
# record message to dialog tracking
|
# record message to dialog tracking
|
||||||
dialog.msgs[oid] = msg
|
dialog.msgs[oid] = msg
|
||||||
|
@ -870,7 +902,6 @@ async def process_trades_and_update_ui(
|
||||||
'dark_submitted',
|
'dark_submitted',
|
||||||
'broker_submitted'
|
'broker_submitted'
|
||||||
):
|
):
|
||||||
|
|
||||||
# show line label once order is live
|
# show line label once order is live
|
||||||
mode.on_submit(oid)
|
mode.on_submit(oid)
|
||||||
|
|
||||||
|
@ -930,11 +961,11 @@ async def process_trades_and_update_ui(
|
||||||
elif resp in (
|
elif resp in (
|
||||||
'broker_filled',
|
'broker_filled',
|
||||||
):
|
):
|
||||||
|
|
||||||
known_order = book._sent_orders.get(oid)
|
known_order = book._sent_orders.get(oid)
|
||||||
if not known_order:
|
if not known_order:
|
||||||
log.warning(f'order {oid} is unknown')
|
log.warning(f'order {oid} is unknown')
|
||||||
continue
|
return
|
||||||
|
# continue
|
||||||
|
|
||||||
action = known_order.action
|
action = known_order.action
|
||||||
details = msg['brokerd_msg']
|
details = msg['brokerd_msg']
|
||||||
|
|
Loading…
Reference in New Issue