Handling disconnects when using the IB API

Discussion in 'Retail Brokers' started by CarlErikson, Nov 18, 2002.

  1. I've been trying to automate a trading algorithm using the IB API. It has a list of stocks and criteria for those stocks to determine entry. Once entered, it uses a trailing stop.

    I have developed this program to the point where if my connection to IB never went down, I could run my program in the morning, go out for the day, and come back at the close and see how it performed.

    Unfortunately, I frequently get disconnected from IB during the day - these disconnects are brief and the TWS automatically reconnects a few seconds later. When this happens, the TWS API does not give me enough information to gracefully recover from the disconnect. For example, I'll have an order identifier for a particular order and once the disconnect/reconnect occurs, that particular order identifier is no longer valid - i.e., I can't cancel an order that I had access to before. I don't believe that I'm given enough information after the disconnect to reassign these order ids (although if someone has been successful doing so - please tell me your secret).

    I was just curious if other people have run into this problem. If so, how do you overcome it? How can you detect when a disconnect occurs? Note that I believe my internet connection is stable and that these disconnects originate on IB's end (although I could be wrong). The reason I think my connection is solid is because I've never had disconnect problems like this with any other program I've run.

    My question has two parts:

    1. How can you automatically detect when the TWS disconnects?

    2. How can you recover from this disconnect (assuming the TWS reconnects immediately)?

    Thanks for any help,
  2. TGregg


    I've not had this problem - TWS stand alone rarely disconnects on my systems.

    One kludge way to tell if you've been disconnected is to get quotes on a high volume instrument (like the Qs or the NQs) and a timer. Then add logic that checks to see if the program has not received any quotes in the last x seconds.

    As I recall, the few times TWS has disconnected on me, it reconnected automagically to my app when it reconnected automagically to their server. Quotes started coming in.

    Never had an order from my app while IB disconnected. But, the quotes come back with the same ID as before. At least, on my computers they do :D .
  3. Yes. the quote ids appear to stay the same. The order ids change. The problem is that my disconnect/reconnects happen so quickly that I'm not sure I can detect them just by looking for the absence of quotes.

  4. in my experience

    public void connectionClosed()

    gets called when disconnected.

    When reconnected


    could be called and during callback

    public void openOrder(int orderId, Contract contract, Order order)

    and orderId seems to be the same as the one you set it during

    clientSocket.placeOrder(orderId, contract, order)
  5. Simon-IB


    Hi Carl,

    1. How can you automatically detect when the TWS disconnects?
    >The void EClientSocket::wtf:nClose( int i) function is called when the TWS terminates the connection

    2. How can you recover from this disconnect (assuming the TWS reconnects immediately)?
    >When the API connection is dropped the order's entered via the API client are no longer associated with the client (i.e as if they had been directly entered into TWS). To re-associate the orders with the API after a disconnect, call reqOpenOrders. After this call the open orders can be managed by the client e.g. cancel or modify them.
  6. Thanks trader42 and Simon-IB.

    After reading your suggestions and the user's guide, it all makes sense. Unfortunately (or fortunately depending on your point of view), I have all my stuff written in Perl (I ported the C++ IB socket library into Perl). As far as I can tell, there is no callback for when a socket closes, nor is there a way to tell if the socket is closed by doing some sort of polling operation. I'll figure it out eventually, but essentially that is why I've been having so many problems.

    Since you have been so helpful, I have another question. Why is it that after a disconnect, I still receive ticker messages? Isn't the socket closed at that point? I mean, don't I need to reconnect? Or does the TWS automatically re-open the socket connection when it reconnects? How else would I still be receiving ticker messages (after the disconnect) if this were not the case?

  7. Simon-IB


    Dont know how perl handles socket connections but this is the scenario when using the C++ socket based interface:

    When you loose the TWS connections the EClientSocket.eDisconnect() method is called. This method destroys the socket so there is no way to communicate between API and TWS.