1

I can't connect to TWS. I've confirmed that "Enable ActiveX and Socket EClients" is enabled and connection port is the same as "Socket Port" on the TWS.

import ibapi
from ibapi.client import EClient
from ibapi.wrapper import EWrapper

class IBApi(EWrapper,EClient):
    def __init__(self):
        EClient.__init__(self, self)
        
class Bot:
    ib=None
    def __init__(self):
        ib=IBApi()
        ib.connect("127.0.0.1",7497,1)
        ib.run()
bot=Bot()

I tried changing the port number, but I was not able to also change it from Interactive Broker Client App.

Michael M.
  • 10,486
  • 9
  • 18
  • 34

1 Answers1

0

You can run the below to see if it is indeed connected

from ibapi.client import EClient
from ibapi.wrapper import EWrapper

class MyWrapper(EWrapper):
    def __init__(self):
        super().__init__()
        self.next_order_id = None

    def nextValidId(self, orderId: int):
        super().nextValidId(orderId)
        print(f"Received next valid order ID: {orderId}")
        self.next_order_id = orderId

app = EClient(MyWrapper())

# Connect to TWS/Gateway API
app.connect("127.0.0.1", 7497, clientId=0)

if app.isConnected():
    print("Connected to TWS/Gateway API!")
else:
    print("Not connected to TWS/Gateway API")

# Disconnect from TWS/Gateway API
app.disconnect()

Else you may try to create new environment with the modules installed then try to run ibapi in that new environment, it worked for me

Jun
  • 13
  • 4