atreyuxtrading / atreyu-backtrader-api

Atreyu's integration of IB Native API with backtrader
BSD 2-Clause "Simplified" License
91 stars 58 forks source link

Reconnect on IB Gateway/TWS auto restart #4

Closed jvrodrisan closed 1 year ago

jvrodrisan commented 1 year ago

As we all known, TWS/IBG restarts everyday, now, on the latest versions the session token lasts for a week so you don't have to enter username/password. What I am struggling to make it work, on the restart, is the live data when the IB Gateway restarts.

Looking at the code, the msg=-504 is well recognized, however, there is no way to resuscribe to the live data feed. Tried many things but there is no way to take it back.

Thanks

jvrodrisan commented 1 year ago

There is needed to relaunch the thread that manages the connection to IB, once this is done, the feed works again.

tbilloud commented 1 year ago

This worked for me but with an additional correction: removing the 'if' condition when calling self.conn.connect() in the IBStore reconnect() function definition. I replaced:

                if self.conn.connect(self.p.host, self.p.port, self.clientId):
                    if not fromstart or resub:
                        self.startdatas()
                    return True  # connection successful

with

                self.conn.connect(self.p.host, self.p.port, self.clientId)
                self.apiThread = threading.Thread(target=self.conn.run, daemon=True)
                self.apiThread.start()
                if not fromstart or resub:
                    self.startdatas()
                return True  # connection successful