Handling TWS connection loss to IB servers


Dear All,

Recently I have created the following test:

  • I had an order placed for which IB controller correctly reported TRUE when ibc.IsOrderPending(orderID) was called
  • I have then disabled my WiFi which caused TWS to loose connection to IB servers.
  • However, IB controller was still connected to TWS, which caused the call to ibc.IsConnected() to return TRUE
  • I then called again ibc.IsOrderPending(orderID) for the same order, but this time the result was FALSE.

My questions are:

  • Is there a way to detect when TWS is not connected to IB servers?
  • When TWS is not connected to IB servers, what will ibc.PlaceOrder(…) return?


TWS/IB API does not provide any methods of detecting disconnection EXCEPT error messages:

You can get last error using ibc.GetLastError as documented in http://www.amibroker.com/at/


Hmm, as the name says, GetLastError returns the LAST error. Now, since my code is NOT invoked (called back) in real time fashion, but executed at regular intervals as specified by the repeat interval set in the analysis window, I could have multiple errors coming from IB within two runs of my code. If the error that signals the disconnect is not the last one, I will miss that, isn’t it?


You won’t get OTHER errors once you get “disconnect” error because you are … disconnected.


You get lots of junk about market data / historical data farms - codes 2103 to 2106.


No it is not junk. These should also be interpreted as disconnect.


Fair point, but I just noticed in the past that orders can still be placed while these errors are reported, as “only” the market data servers are affected and IB is still able to route orders.