Can't connect to Interactive broker API with Python show error 502, 504

I can't connect to Interactive broker API with Python it shows errors 502, 504 all the time when run the code

I have checked in the global configuration all parameters it's correct. -Checked Enable Active and Socket Clients -Socket port and Client ID, it's the same between API application and TWS.

I cannot connect API since this week but before that, I can connect API.

When I run my APIs it shows the error as below all the time.

Error: -1 502 Couldn't connect to TWS. Confirm that "Enable ActiveX and Socket EClients" is enabled and connection port is the same as "Socket Port" on the TWS "Edit->Global Configuration...->API->Settings" menu. Live Trading ports:
TWS: 7496; IB Gateway: 4001. Simulated Trading ports for new installations of version 954.1 or newer: TWS: 7497; IB Gateway: 4002
Error: -1 504 Not connected

I have tried to run from my laptop but it's the same and also changed my service internet.

Here is my code:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum


class TestApp(EWrapper, EClient):
   def __init__(self):
   EClient.__init__(self, self)

 def error(self, reqId, errorCode, errorString):
   print("Error: ", reqId, " ", errorCode, " ", errorString)

 def contractDetails(self, reqId, contractDetails):
   print("contractDetails: ", reqId, " ", contractDetails)

def main():
  app = TestApp()
  app.connect(host="192.168.1.107",port=7498,clientId=0)

  contract = Contract()
  contract.symbol = "TNA"
  contract.secType = "STK"
  contract.exchange = "SMART"
  contract.currency = "USD"
  contract.primaryExchange = "ARCA"

  app.reqContractDetails(1,contract)

  app.run()
  app.disconnect()
if __name__ == "__main__":
   main()

How do i solve it?

Upvotes: 3

Views: 4605

Answers (1)

anergcorp
anergcorp

Reputation: 21

The problem is your MASTER ID in the global configuration. If that is set to None or 0 then it will give you Error 504: Not Connected. Try different master id for example 999. Then set it to your code. Global Configuration IBKR

Upvotes: 2

Related Questions