Zipline live keeps status of some canceled/rejected orders as open

There are a couple situations when I've seen zipline identify inactive orders as open (i.e. order.status == ORDER_STATUS.OPEN):

  1. The order was rejected by IBKR when initially submitted. In this case, a blotter query outside of zipline (using download_order_statuses() will find the order with Status value of 'Inactive'.

  2. The order was a GTC stop order that was cancelled outside of zipline. In this case, the blotter Status will be 'Cancelled' (previously 'Pre-Submitted'), but zipline doesn't recognize it. Attempting to force cancel the order from within zipline with cancel_order() results in an 'order not found' error.

This is a problem for me, because I use get_open_orders() to check for existing orders that haven't filled yet before placing a new order on the same asset. While the blotter sees these orders as inactive, zipline does not.

Is there a way to force zipline to apply/map the correct statuses (ORDER_STATUS.REJECTED or ORDER_STATUS.CANCELLED)?

Deleting the context joblib doesn't work, because the order is marked as open again when zipline replays the transactions in the blotter at the start of each trading session.

I’m not sure it would be accurate to consider an order status of Inactive as cancelled, as it might become active again: TWS API v9.72+: Placing Orders

Zipline will consider an order cancelled if the status contains the word “cancelled” or “canceled” (case insensitive).

Seems like there's not a mapping of IBKR statuses that would be entirely accurate all the time, since the 'Inactive' category covers various situations. I have completely rejected orders in the blotter that will never change from the 'Inactive' status and will be forever read as open by zipline, unless I change the name of the strategy/order ref.

I've worked around this by querying the blotter directly from inside the strategy and filtering out the 'Inactives'. However, it would still be useful to have an option for zipline to map the blotter statuses differently. Not a high priority item, but maybe if you're fixing other things anyway. :slight_smile: