For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Is it possible to retrieve all the open/active orders from Interactive Broker?



  • All my strategies will place bracket order, i.e., an order to get into a position, a limit order to get out of the position and take profit, and a stop order to get out of the position and limit loss. I also allow my strategy to generate signals to exit positions, and when that happens, the limit order and stop order need to be canceled otherwise they will enter an unwanted position.

    My solution now is I keep a reference to all the orders I placed, so when I need to cancel any of them, I just use the reference number. This works when my program and IB are both running, but one issue arose during my testing, that is if I connect my program to IB when there are open limit/stop loss orders, I won't be able to reference them, therefore won't be able to cancel them when needed, and later on they will be executed and give me unwanted positions.

    My thought is if I can retrieve all the open/active orders from IB, then I will be in full control. From IB API's documentation: https://interactivebrokers.github.io/tws-api/open_orders.html

    I see they support this functionality through API, also with backtrader broker I see there is a method called get_orders_open() which gives me exactly what I want, however, I have difficulty finding the corresponding method with ibbroker.

    Would anyone please help me solve this issue?

    Thanks a lot.


  • administrators

    It's not implemented in the IBBroker, because if those orders are added to the system and executed, there would be no reference to the original bracket/position to which they correspond (position which may be even gone as you mention with your exit order) and would break all calculations.



  • Got it, Thanks for your reply.

    So the calculations will also be incorrect if there is already a position before Cerebro start running, right?



  • @backtrader Thanks for your reply.

    If I just want to have this capability to monitor all the open orders in IB, and don't care about messing up with the calculations, would you please give me some guides on how I can implement it? I've been trying to read and understand the relevant source codes, but have some difficulty in connecting everything together and figure out exactly what to do.

    Thanks a lot.


  • administrators

    @albertzgd said in Is it possible to retrieve all the open/active orders from Interactive Broker?:

    Got it, Thanks for your reply.

    So the calculations will also be incorrect if there is already a position before Cerebro start running, right?

    Yes. The platform assumes that you trade from start to end and not from a given starting position. You could also open a position with backtrader and close it manually. backtrader wouldn't understand that you have done so.

    @albertzgd said in Is it possible to retrieve all the open/active orders from Interactive Broker?:

    If I just want to have this capability to monitor all the open orders in IB, and don't care about messing up with the calculations, would you please give me some guides on how I can implement it?

    Your best bet is to directly use the IB Python API.