Pick a good time for TWS/IBKR restart based upon the trading schedule for the instruments you are working with. For example some time between 16:00 and 17:00 US/Central for Chicago traded futures (say 16:30) or after 20:00 US/Eastern for NYC traded stocks (say 20:30). Keep an eye on the IBKR daily reset/maintenance window for your region and stay away from that.
Start your client a little before trading starts for the instruments you are interested in.
At client start, make calls to for all instruments your client works with. That way, you get fresh Contract objects that you can use for all requests that take a Contract parameter and you know the actual trade schedule for that day (and the next few days). That takes care of any last minute exchange hour changes or holiday hours.
Now you know the time when trading ends and you can stop your client shortly after hat time (but before your scheduled TWS/IBGW restart). Your client can save enough state so that it can start again before the next session.
Any error 502 received before markets close is unscheduled should probably trigger some kind of alert and possible needs some kind of manual interaction. There are ways to automate TWS/IBGW and client restart (see IBC for example) should this really happen, but you may need to provide at least 2FA login authorization (that can be with IBKR Key on a mobile device).
On Tue, Nov 21, 2023 at 08:11 PM, <makeitrain512@...> wrote:
Hey there Jurgen, thanks a lot for your input, it has really helped me see this problem in a different way.?
What's the best way I could approach a scheduled full disconnect right before TWS restarts and the make a fresh new connection after TWS has restarted ? I imagine this is what most people do, instead of having to deal with a reconnection because of all the issues that arise from it.