TWS IB Gateway (version 972/974) Client keeps disconnecting

2.1k Views Asked by At

I am trying to connect with IB Api to download some historical data. I have noticed that my client connects to the API, but then disconnects automatically in a very small period (~a few seconds).

Here's the log in the server:

socket connection for client{10} has closed.
Connection terminated.

Here's my main code for starting the app:

class TestApp(TestWrapper, TestClient):
 def __init__(self):
    TestWrapper.__init__(self)
    TestClient.__init__(self, wrapper=self)
    self.connect(config.ib_hostname, config.ib_port, config.ib_session_id)
    self.session_id = int(config.ib_session_id)
    self.thread = Thread(target = self.run)
    self.thread.start()
    setattr(self, "_thread", self.thread)
    self.init_error()

 def reset_connection(self):
    pass

 def check_contract(self, name, exchange_name, security_type, currency):
    self.reset_connection()
    ibcontract = IBcontract()
    ibcontract.secType = security_type
    ibcontract.symbol = name
    ibcontract.exchange = exchange_name
    ibcontract.currency = currency
    return self.resolve_ib_contract(ibcontract)

def resolve_contract(self, security):
    self.reset_connection()
    ibcontract = IBcontract()
    ibcontract.secType = security.security_type()
    ibcontract.symbol=security.name()
    ibcontract.exchange=security.exchange()
    ibcontract.currency = security.currency()
    return self.resolve_ib_contract(ibcontract)

 def get_historical_data(self, security, duration, bar_size, what_to_show):
    self.reset_connection()
    resolved_ibcontract=self.resolve_contract(security)
    data = test_app.get_IB_historical_data(resolved_ibcontract.contract, duration, bar_size, what_to_show)
    return data



def create_app():
 test_app = TestApp()
 return test_app

Any suggestions on what could be the problem? I can show more error messages from the debug if needed.

2

There are 2 best solutions below

0
On BEST ANSWER

Installing the latest version of TWS API (v 9.76) solved the problem.

https://interactivebrokers.github.io/#

1
On

If you can connect without issue only by changing the client ID, typically that indicates that the previous connection was not properly closed and TWS thinks its still open. To disconnect an API client you should call the EClient.disconnect function explicity, overridden in your example as:

test_app.disconnect()

Though its not necessary to disconnect/reconnect after every task, and you can just leave the connection open for extended periods.

You may sometimes encounter problems if an API function, such as reqHistoricalData, is called immediately after connection. Its best to have a small pause after initiating a connection to wait for a callback such as nextValidID to ensure the connection is complete before proceeding.

http://interactivebrokers.github.io/tws-api/connection.html#connect

I'm not sure what the function init_error() is intended for in your example since it would always be called when a TestApp object is created (whether or not there is an error).