erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.8k stars 743 forks source link

unable to retrieve open orders made using the same client id or the master client id #416

Closed yarimiz closed 2 years ago

yarimiz commented 2 years ago

This is the situation I'm at:

After connecting, the client doesn't show open orders made via the API, using the same client id. I've tried using the master client id, client id = 0 any specific client IDs for all the runs.

  1. Manually calling ib.reqOpenOrders() show nothing, while in reality, I have several open orders (not transmitted) in TWS. Some of the orders were created via API and some manually via GUI
  2. Calling ib.reqAllOpenOrders() retrieves only the manual orders made from GUI, but not those who have been made via API, using the same client id when connecting.

This is how I'm connecting. client id 999 is configured as master in TWS. ib.connect("127.0.0.1", config["port"], clientId=999)

I tried connecting with 0, 999 and just 1

Update: If I'm calling openTrades() from within the same session that I made the orders in, it will retrieve the new orders. But, as soon as I restart my app (using with the exact same conditions), calling openTrades() will not retrieve orders made from the previous session.

The flow:

  1. Launch my app
  2. Send some MarketOrders
  3. call ib.openTrades() -> All new traders are retrieved
  4. Restart my app
  5. call ib.openTrades() -> No orders are retrieved.

At step 5, I also called reqAllOpenOrders, AllOpenOrders, OpenOrders. All returns empty list.

Needless to say, all orders are yet to be filled. They are all not transmitted yet.

Some more info: I'm using only one client My TWS is connected to a single paper account, without sub-accounts

yarimiz commented 2 years ago

I've figured out what happens, so for newcomers facing the same issue, I'm leaving this comment here and closing the issue, as this is not ib_insync bug.

Apparently, if orders are not transmitted (sent with transmit=False) they won't be picked up by the API. This is not an issue of ib_insync, but an intentional (but not documented, AFAIK) behavior of IB API.

In addition, if during your session you've transmitted an untransmitted order, this won't be picked up automatically by the API (as it wasn't received during the sync operation during the connection). In this case, you'll have to run reqOpenTrades() to reinitiate the sync.

Nonetheless, please be sure you're using either the same clientId which created the orders or configure a master client id to retrieve orders made by any client.

disaster123 commented 1 year ago

Just leave it here - this is the real solution even you won't wanna use the same client id or a master id: https://groups.io/g/insync/message/4997