StopOrder and LimitOrders not going through using ib_insync

Discussion in 'App Development' started by bobrosski, Jul 26, 2023.

  1. bobrosski

    bobrosski

    Hi,

    First time posting on EliteTrader :)

    I was wondering if anyone might be able to help me resolve an issue I've been having with the ib_insync python library and the Interactive Brokers TWS Desktop Application. I've created a Stackoverflow post for my issue but figured maybe someone from the EliteTrader community might be able to help.

    Here is the link to my Stackoverflow post with my code: python - StopOrder and LimitOrders not going through using ib_insync - Stack Overflow

    To summarize, my example parent order of going long 25k GBP.USD always goes through, however, the StopOrder and LimitOrder usually never go through. I have tried increasing the delays and have been testing my code between 7pm EST - 10pm EST during weekdays so the market is live and volatility shouldn't be an issue. I noticed sometimes when I close my IB TWS Desktop Application, reopen it, and re-run my code all the orders go through (but not always).
     
  2. I took a brief look at the code you posted at StackOverflow. There are two things I don't like:
    (*) NEVER use a "sleep" command. The entire system is asynchronous, so you never know how fast you get a response after you have sent something to IB's servers, or how long it takes the exchange to execute your order. Instead of using "sleep" you should always monitor for responses which indicate a change in status and process those changes appropriately .
    (*) I'm not familiar with the ib_insync library, but wonder whether you got the correct order ID's for the stoploss and profit taking order. You first submit the stoploss order. Are you sure that the code gives you a new order ID for the profit taking order, even if the stoploss order has not been executed? In my view it would be safer to use the parent order (i.e. opening order), take that order ID and give the stoploss order an order ID of +1 and the profit taker an order ID of +2. In that case you are always assured that those order IDs are unique.