softwarespartan / IB4m

Interactive Brokers API for Matlab
GNU General Public License v2.0
62 stars 21 forks source link

Re-Connect to TWS after connection-loss due to msg-rate violation #38

Open Benvaulter opened 6 years ago

Benvaulter commented 6 years ago

Hi there,

I am experiencing an issue with the TWS when attempting to re-connect after the connection to TWS has been lost. I have written a very simple function to check for the connection status and re-connect in case the connection is not available:

% retrieve connection status
    status = session.eClientSocket.isConnected();

    % if is not connected, re-establish connection
    if status == 0

        % re-connect
        session.eClientSocket.eConnect('127.0.0.1',7496,0);

        % display update
        disp([datestr(now),': attempted to re-establish connection']);

        % pause briefly
        pause(1);

    else

        % display update
        disp([datestr(now),': connection is up and running']);
    end

This code seems to work just fine under normal circumstances. However, when the connection to TWS is lost due to the violation of messages sent from the client to the server in one second (limit is 50 msgs per second - I do have multiple checks in place to prevent this, but it still seems to happen some times) - when trying to reconnect I get the following error:

Couldn't connect to TWS. Confirm that "Enable ActiveX and Socket Clients" is enabled and connection port is the same as "Socket Port" on the TWS "Edit->Global Configuration...->API->Settings" menu. Live Trading ports: TWS: 7496; IB Gateway: 4001. Simulated Trading ports for new installations of version 954.1 or newer: TWS: 7497; IB Gateway: 4002

It seems when I lose the connection due to the violation, some settings in TWS change so that I cannot re-connect in the normal way. Once I restarted TWS, everything works normal again... Any idea on how to fix this behaviour?

Many thanks!

Benvaulter commented 6 years ago

I just found out that once the connection is terminated due to the violation described above, TWS won't allow new connections from the same clientID. I will test now if using a different clientID will work

softwarespartan commented 6 years ago

Try ‘clear all’ and then re init the session. This is a little bug. Will fix soon. Thanks for pinging me about it!

-abel

Sent from my iPhone

On Sep 11, 2018, at 2:26 AM, Benvaulter notifications@github.com wrote:

I just found out that once the connection is terminated due to the violation described above, TWS won't allow new connections from the same clientID. I will test now if using a different clientID will work

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

Benvaulter commented 6 years ago

Okay will try - all though it seems this is actually behavior of the TWS (I chatted with someone from the API Support Team) so not sure whether this helps. Will try - thanks!