Is it possible to check if IB is connected when AB starts?

Hello,

I use IB on two separate machines, and often switch between them. This means that when a session on one machine starts, the other session is automatically logged out. I've often been in the situation that I want to immediately execute a trade from AB, and it isn't executed because the IB instance running on that machine has been logged off. And I don't see the IB logge- off pop-up message because it is beneath other open windows.

Is there a ways to test using AFL if the IB session has been disconnected?
I tried the following, but it doesn't detect it.

testIbc = GetTradingInterface("IB");
if( IsNull( StaticVarGet("IBCStatusTested") ) )
{ 
  testIbc.Reconnect();
  StaticVarSet("IBCStatusTested", 1 );
  PopupWindow( "IBC Restarted", "*** IB CONNECTION ***", 0, -1, -1, 20, 50 ); 
}
testStatus = 0;
if( !(ibcStatus = testIbc.IsConnected()) )
  displayWarningMessage("IB not connected " + ibcStatus, colorBlack, colorRed);
else
  displayWarningMessage("IB OK "  + ibcStatus, colorBlack, ColorYellow);

It displays "IB not connected 0" if IB is not running.
It displays "IB OK 3" if IB is running.
It displays "IB OK 3" if IB is running, but has been disconnected.

Did you read the docs http://www.amibroker.com/at/ which say that isConnected returns number (not boolean) and what "3" actually means?

IsConnected ()

This function retrieves the connection status to TWS.

Return value:
It says:
0 - NO connection
1 - connection in progress
2 - means connection OK, no messages
3 - connection OK, but TWS generated some warning/error messages (see "Messages tab)

Code 3 means "connected BUT error messages are present" and you should check ERROR list to find out if you got log-off.

Only if you get 2 there are "no error messages" and you are free to go.

Hello @Tomasz,

Yes, I had read the AT documentation.
I experimented further, with the following code:

testStatus = testIbc.IsConnected();
if( testStatus == 0 )
  displayWarningMessage("IB not connected " + testStatus, colorBlack, colorRed);
else if( testStatus == 1 )
  displayWarningMessage("IB connection in progress " + testStatus, colorBlack, colorRed);
else if( testStatus == 2 )
  displayWarningMessage("IB connection error " + testStatus, colorBlack, colorRed);
else if( testStatus == 3 )
  displayWarningMessage("IB OK " + testStatus, colorBlack, colorYellow);

I did the following:

AB running, IB running: message "IB OK 3"
AB running, IB log out: message "IB OK 3"
AB running, IB stop: message "IB not connected 0"
AB running, IB started: message "IB connection error 2"
AB running, IB running: message "IB OK 3"
AB running, IB log out: message "IB OK 3"
AB stop, IB logged out: no message
AB start, IB logged out: message "IB OK 3"
AB running, IB log in: message "IB OK 3"
AB running, IB running: message "IB OK 3"

So the status 2 is never occurs when IB is logged out. It only occurs during IB startup.

I was able to fix it, but I had to modify the C++ implementation in IBC, in particular I had to modify the behaviour in response to the error messages.

When logged in:

1102: Connectivity between IB and TWS has been restored
2106: HMDS data farm connection is OK

When logged out:

1100: Connectivity between IB and TWS has been lost
2015: HMDS data farm connection is broken

Paul

For what it is worth: HMDS data farm connnection is irrelevant. HDMS stands for "Historical Data Market Service". IB controller does not use historical data at all. Also messages in >2000 range are "informational", not really errors. HDMS connection gets disconnected automatically after period of sitting idle which is normal when you are not using it.