I have a program that will place trades in either the paper or live account. I run both platforms simultaneously on the same machine. It crashes when I try get my account value after switching from one to the other because I get the first result over again. I have a support ticket in with IB, but they keep suggesting it's my code. Can anyone here create the problem to help me sort this out?
With two instances of TWS open, one paper and one live, here's the simplest code I've run to show the problem:
from ib_async import *
def ensure_connection(ib: IB, host: str, desired_port: int, client_id: int):
"""Ensure IB is connected to the desired port. Reconnect if necessary."""
# Check if IB is connected and if the port matches
if ib.isConnected() and ib.client.port == desired_port:
print(f"Already connected on the desired port: {desired_port}")
return
# If connected but on the wrong port, disconnect
if ib.isConnected():
print(f"Connected on wrong port {ib.client.port}, disconnecting...")
ib.disconnect()
ib.sleep(1)
# Reconnect on the correct port
print(f"Connecting to port {desired_port}...")
ib.connect(host, desired_port, client_id)
print(f"Connected on port: {ib.client.port}")
ib = IB()
IBPort = 7496
ensure_connection(ib, "127.0.0.1", IBPort, 33)
print(ib.isConnected())
print(ib.client.port)
SummaryAccountValue = ib.accountSummary()
print(SummaryAccountValue)
IBPort = 7497
ensure_connection(ib, "127.0.0.1", IBPort, 33)
print(ib.isConnected())
print(ib.client.port)
SummaryAccountValue = ib.accountSummary()
print(SummaryAccountValue)
ib.disconnect()
print(ib.isConnected())
print(ib.client.port)
IBPort = 7497
ensure_connection(ib, "127.0.0.1", IBPort, 33)
print(ib.isConnected())
print(ib.client.port)
SummaryAccountValue = ib.accountSummary()
print(SummaryAccountValue)
IBPort = 7496
ensure_connection(ib, "127.0.0.1", IBPort, 33)
print(ib.isConnected())
print(ib.client.port)
SummaryAccountValue = ib.accountSummary()
print(SummaryAccountValue)
ib.disconnect()
'''
ib.fills also seems to have problems. For the first connection, it will show correct information, but on the second connection, it will show the first plus the second.
Is there a problem somewhere in the ib_async library or is this an IB issue? Can anyone reproduce this or suggest how I can fix it?
Glenn