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..
open_order_loading
Tyler Goodlet 2022-08-17 13:05:02 -04:00
parent 06845e5504
commit 5861839783
1 changed files with 29 additions and 28 deletions

View File

@ -186,7 +186,7 @@ async def handle_order_requests(
) )
) )
if action == 'cancel': elif action == 'cancel':
msg = BrokerdCancel(**request_msg) msg = BrokerdCancel(**request_msg)
client.submit_cancel(reqid=int(msg.reqid)) 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.. # TODO: we probably want to generalize this into a "ledgers" api..
ledgers: dict[str, dict] = {} ledgers: dict[str, dict] = {}
tables: dict[str, PpTable] = {} tables: dict[str, PpTable] = {}
order_msgs: list[Status] = []
with ( with (
ExitStack() as lstack, ExitStack() as lstack,
): ):
@ -486,11 +487,8 @@ async def trades_dialogue(
for account, proxy in proxies.items(): for account, proxy in proxies.items():
client = aioclients[account] client = aioclients[account]
trades: list[Trade] = client.ib.openTrades() trades: list[Trade] = client.ib.openTrades()
order_msgs = []
for trade in trades: for trade in trades:
order = trade.order order = trade.order
quant = trade.order.totalQuantity quant = trade.order.totalQuantity
action = order.action.lower() action = order.action.lower()
@ -840,34 +838,37 @@ async def deliver_trade_events(
trade: Trade = item trade: Trade = item
status: OrderStatus = trade.orderStatus status: OrderStatus = trade.orderStatus
ib_status_key = status.status.lower() ib_status_key = status.status.lower()
acctid = accounts_def.inverse[trade.order.account]
# double check there is no error when # TODO: try out cancelling inactive orders after delay:
# cancelling.. gawwwd # https://github.com/erdewit/ib_insync/issues/363
if ib_status_key == 'cancelled': # acctid = accounts_def.inverse[trade.order.account]
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': # # 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
async def sched_cancel(): # elif ib_status_key == 'inactive':
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) # 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 = ( status_key = (
_statuses.get(ib_status_key) _statuses.get(ib_status_key.lower())
or ib_status_key.lower() or ib_status_key.lower()
) )
@ -880,7 +881,7 @@ async def deliver_trade_events(
# execdict = asdict(execu) # execdict = asdict(execu)
# execdict.pop('acctNumber') # execdict.pop('acctNumber')
msg = BrokerdFill( fill_msg = BrokerdFill(
# should match the value returned from # should match the value returned from
# `.submit_limit()` # `.submit_limit()`
reqid=execu.orderId, reqid=execu.orderId,
@ -892,7 +893,7 @@ async def deliver_trade_events(
# XXX: required by order mode currently # XXX: required by order mode currently
broker_time=execu.time, broker_time=execu.time,
) )
await ems_stream.send(msg) await ems_stream.send(fill_msg)
if remaining == 0: if remaining == 0:
# emit a closed status on filled statuses where # emit a closed status on filled statuses where