From b01538f183c7de8d410a5dad3d0af4f9ab4311c5 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 8 Sep 2021 15:46:33 -0400 Subject: [PATCH] Support an account field in clearing system Each backend broker may support multiple (types) of accounts; this patch lets clients send order requests that pass through an `account` field in certain `emsd` <-> `brokerd` transactions. This allows each provider to read in and conduct logic based on what account value is passed via requests to the `trades_dialogue()` endpoint as well as tie together positioning updates with relevant account keys for display in UIs. This also adds relay support for a `Status` msg with a `'broker_errored'` status which for now will trigger the same logic as cancelled orders on the client side and thus will remove order lines submitted on a chart. --- piker/clearing/_ems.py | 11 +++++++++-- piker/ui/order_mode.py | 10 ++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/piker/clearing/_ems.py b/piker/clearing/_ems.py index f5eeff87..48c8cce3 100644 --- a/piker/clearing/_ems.py +++ b/piker/clearing/_ems.py @@ -201,6 +201,7 @@ async def clear_dark_triggers( msg = BrokerdOrder( action=cmd['action'], oid=oid, + account=cmd['account'], time_ns=time.time_ns(), # this **creates** new order request for the @@ -621,8 +622,11 @@ async def translate_and_relay_brokerd_events( # another stupid ib error to handle # if 10147 in message: cancel + resp = 'broker_errored' + broker_details = msg.dict() + # don't relay message to order requester client - continue + # continue elif name in ( 'status', @@ -741,6 +745,7 @@ async def process_client_order_cmds( oid=oid, reqid=reqid, time_ns=time.time_ns(), + account=live_entry.account, ) # NOTE: cancel response will be relayed back in messages @@ -814,6 +819,7 @@ async def process_client_order_cmds( action=action, price=trigger_price, size=size, + account=msg.account, ) # send request to backend @@ -1016,6 +1022,7 @@ async def _emsd_main( try: _router.clients.add(ems_client_order_stream) + # main entrypoint, run here until cancelled. await process_client_order_cmds( ems_client_order_stream, @@ -1035,7 +1042,7 @@ async def _emsd_main( dialogues = _router.dialogues - for oid, client_stream in dialogues.items(): + for oid, client_stream in dialogues.copy().items(): if client_stream == ems_client_order_stream: diff --git a/piker/ui/order_mode.py b/piker/ui/order_mode.py index 87332ac6..49a6cb99 100644 --- a/piker/ui/order_mode.py +++ b/piker/ui/order_mode.py @@ -103,7 +103,7 @@ class OrderMode: arrows: ArrowEditor multistatus: MultiStatus pp: PositionTracker - allocator: 'Allocator' # noqa + alloc: 'Allocator' # noqa pane: SettingsPane active: bool = False @@ -193,6 +193,7 @@ class OrderMode: order = self._staged_order = Order( action=action, price=price, + account=self.alloc.account_name(), size=0, symbol=symbol, brokers=symbol.brokers, @@ -538,6 +539,8 @@ async def open_order_mode( # load account names from ``brokers.toml`` accounts = bidict(config.load_accounts()) + # process pps back from broker, only present + # account names reported back from ``brokerd``. pp_account = None if pp_msg: @@ -598,7 +601,7 @@ async def open_order_mode( arrows, multistatus, pp_tracker, - allocator=alloc, + alloc=alloc, pane=order_pane, ) @@ -823,10 +826,13 @@ async def process_trades_and_update_ui( elif resp in ( 'broker_cancelled', 'broker_inactive', + 'broker_errored', 'dark_cancelled' ): # delete level line from view mode.on_cancel(oid) + broker_msg = msg['brokerd_msg'] + log.warning(f'Order {oid} failed with:\n{pformat(broker_msg)}') elif resp in ( 'dark_triggered'