From 5861839783c26804ac59a7c34fc13454cd5a6f5f Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 17 Aug 2022 13:05:02 -0400 Subject: [PATCH] Fix multi-account order loading.. We were overwriting the existing loaded orders list in the per client loop (lul) so move the def above all that. Comment out the "try-to-cancel-inactive-orders-via-task-after-timeout" stuff pertaining to https://github.com/erdewit/ib_insync/issues/363 for now since we don't have a mechanism in place to cancel the re-cancel task once the order is cancelled - plus who knows if this is even the best way to do it.. --- piker/brokers/ib/broker.py | 63 +++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/piker/brokers/ib/broker.py b/piker/brokers/ib/broker.py index 9679203de..f9dd91ea9 100644 --- a/piker/brokers/ib/broker.py +++ b/piker/brokers/ib/broker.py @@ -186,7 +186,7 @@ async def handle_order_requests( ) ) - if action == 'cancel': + elif action == 'cancel': msg = BrokerdCancel(**request_msg) client.submit_cancel(reqid=int(msg.reqid)) @@ -465,6 +465,7 @@ async def trades_dialogue( # TODO: we probably want to generalize this into a "ledgers" api.. ledgers: dict[str, dict] = {} tables: dict[str, PpTable] = {} + order_msgs: list[Status] = [] with ( ExitStack() as lstack, ): @@ -486,11 +487,8 @@ async def trades_dialogue( for account, proxy in proxies.items(): client = aioclients[account] - trades: list[Trade] = client.ib.openTrades() - order_msgs = [] for trade in trades: - order = trade.order quant = trade.order.totalQuantity action = order.action.lower() @@ -840,34 +838,37 @@ async def deliver_trade_events( trade: Trade = item status: OrderStatus = trade.orderStatus ib_status_key = status.status.lower() - acctid = accounts_def.inverse[trade.order.account] - - # double check there is no error when - # cancelling.. gawwwd - if ib_status_key == 'cancelled': - last_log = trade.log[-1] - if ( - last_log.message - and 'Error' not in last_log.message - ): - ib_status_key = trade.log[-2].status - elif ib_status_key == 'inactive': - - async def sched_cancel(): - log.warning( - 'OH GAWD an inactive order..scheduling a cancel\n' - f'{pformat(item)}' - ) - proxy = proxies[acctid] - await proxy.submit_cancel(reqid=trade.order.orderId) - await trio.sleep(1) - nurse.start_soon(sched_cancel) - - nurse.start_soon(sched_cancel) + # TODO: try out cancelling inactive orders after delay: + # https://github.com/erdewit/ib_insync/issues/363 + # acctid = accounts_def.inverse[trade.order.account] + + # # double check there is no error when + # # cancelling.. gawwwd + # if ib_status_key == 'cancelled': + # last_log = trade.log[-1] + # if ( + # last_log.message + # and 'Error' not in last_log.message + # ): + # ib_status_key = trade.log[-2].status + + # elif ib_status_key == 'inactive': + + # async def sched_cancel(): + # log.warning( + # 'OH GAWD an inactive order.scheduling a cancel\n' + # f'{pformat(item)}' + # ) + # proxy = proxies[acctid] + # await proxy.submit_cancel(reqid=trade.order.orderId) + # await trio.sleep(1) + # nurse.start_soon(sched_cancel) + + # nurse.start_soon(sched_cancel) status_key = ( - _statuses.get(ib_status_key) + _statuses.get(ib_status_key.lower()) or ib_status_key.lower() ) @@ -880,7 +881,7 @@ async def sched_cancel(): # execdict = asdict(execu) # execdict.pop('acctNumber') - msg = BrokerdFill( + fill_msg = BrokerdFill( # should match the value returned from # `.submit_limit()` reqid=execu.orderId, @@ -892,7 +893,7 @@ async def sched_cancel(): # XXX: required by order mode currently broker_time=execu.time, ) - await ems_stream.send(msg) + await ems_stream.send(fill_msg) if remaining == 0: # emit a closed status on filled statuses where