INNER CODE UNIT · Python
connect_to_ibkr
ldt9/PyOptionTrader · models/futures/short_strangle.py:67
def connect_to_ibkr(self):
# Connect to IB
max_attempts = 60
current_reconnect = 0
delaySecs = 60
while not self.ib.isConnected():
try:
self.ib.connect("127.0.0.1", port=7497, clientId=101, timeout=5)
if self.ib.isConnected():
print(f'{self.get_timestamp()} Connected to IBKR')
current_reconnect = 0
break
except Exception as err:
print(f"{self.get_timestamp()} Connection exception: ", err)
if current_reconnect < max_attempts:
current_reconnect += 1
print(f'{self.get_timestamp()} Connect failed')
print(f'{self.get_timestamp()} Retrying in {delaySecs} seconds, attempt {current_reconnect} of max {max_attempts}')