Interactive Brokers Native Python API

Guys need some help on IB Native API.



Am getting the error saying :



Error:  -1   504   Not connected



when using the paper trading account.


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


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="127.0.0.1",port=7497,clientId=1)

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

  app.reqContractDetails(1,contract)

  app.run()
  print (app.reqContractDetails(1,contract))
  app.disconnect()
if __name__ == "__main__":
   main()

 

Hello Sugat,



Kindly find the below feedback:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
impor time


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="127.0.0.1",port=7497,clientId=1)
  time.sleep(4)

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

  app.reqContractDetails(1,contract)

  app.run()
  print (app.reqContractDetails(1,contract))
  app.disconnect()
if __name__ == "__main__":
   main()

Whenever you start the app, you should make the app wait some seconds to connect it to the broker's server. When using some other functions, you'll need the same.

Please refer to the documentation.


Regards,
Alumni  Cell

Thank you.