Search code examples
pythonclient-servertwsib-api

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


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.


Solution

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

    https://interactivebrokers.github.io/#