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,
Carl
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,
Carl
.
nClose( int i) function is called when the TWS terminates the connection