IBController sometimes loses sync despite being connected

Once in a while IBController seems to be losing sync with TWS despite being connected -- that it, it is not updating the portfolio / positions correctly. This is happened at least twice in the last 4 months for me. The result is that some of my orders are not being triggered because it is not reading the correct current position size. The issue is resolved upon closing IBController and when the next time the auto-repeat is triggered all of those orders come flooding in at once.

My super janky solution is to run the Windows task scheduler to automatically close BrokerIB.exe every so many minutes. Is there a more elegant solution to this, or perhaps something I'm doing wrong?

Thanks in advance.

So after testing the task scheduler killer, I also needed to add a filter to not run the system when IBController is being closed because it might cause an error.

Here is the code for the batch file to have the Windows task scheduler kill IBController:

@echo off
tasklist | find /I "BrokerIB.exe"
if errorlevel 0 (
    taskkill /IM BrokerIB.exe /F
)

Here is an example of how to filter this out of the autotrading code:

BarTime = Now(4) % 500;
DoNotRun = BarTime > 445 AND BarTime < 453 ;

if (!DoNotRun) {

ibc = GetTradingInterface("IB");

// blah blah blah auto-trading code here code here

}

It's janky but it seems to be working so far. Will update if any further issues.

edit: I should probably mention that I have tried the ibc.Reconnect() call but it just hangs and wouldn't finish.

1 Like

Reconnect may appear to “hang “ when TWS displays message box asking you to allow API connection. Make sure you have correct settings and allow API connections without manual dialog box confirmation

1 Like

Wouldnt it then be much easier to use ibc.Reconnect() than to use alle these workarounds. I have the same problem as dude. I lose sync nearly once a day, but ibc.Reconnect() doesnt have any effect. Also there is no open TWS message box.

I have seen TWS fail to sync, but got it working again by clicking into different windows, especially the Trades window. It seems that selecting different tabs in the Trades window does have an effect on the data that is available in the API.

Generally TWS API is very coupled with GUI and account refreshes via API are exactly the same as used in TWS GUI so they are slow because TWS refreshes it's account page slowly (like one per second)

1 Like

Thanks for your response. Every time that it would hang, there was no message box or confirmation. I've checked the API settings in TWS and don't see anything that would've caused it. However closing TWS did break the "hang" so it certainly does seem like it was waiting for something from TWS.

Funny enough, I just retried ibc.Reconnect() right now and it seems to work fine for the first time despite failing just a few days ago (and every time before that). So I have no clue what the problem was before, and will have to try to replicate it.

Unfortunately I am not always by the computer and need the system to run autonomously without manual intervention. I realize that is not the scope/intention of the plugin but I'm trying to get there.

If you're running a system, it's better to use IB Gateway rather than TWS. Based on my experience, disconnections and other issues are far less frequent. In any case, supervision is always necessary. Good luck.

2 Likes

After some fiddling, it looks like ibc.Reconnect() hangs only if it is called on multiple symbols. For example if running an exploration on a watchlist. The solution was to use it on a batch scheduler that only runs on a single symbol.

1 Like