0

I am working on IB Gateway and want to get the historical data. As i have completed the steps on IB Gateway software to enable the API.

I am using python notebook for this. For now i am running this code and i am able to import the given library but rest of the code giving me this error. Important thing is connection is established as I have mention client id 1. then it is created and can be seen on IB Gateway application.

My code is here.

from ib_insync import *
#util.startLoop() # uncomment this line when in a notebook

ib = IB()
ib.connect('127.0.0.1', 5021, clientId=1)

bars = ib.reqHistoricalData(
       contract=Stock('TSLA', 'SMART', 'USD'),
       endDateTime='',
       durationStr='30 D',
       barSizeSetting='1 hour',
       whatToShow='TRADES',
       useRTH=True)

print(bars)

Here is the error.

Peer closed connection
clientId 1 already in use?
API connection failed: CancelledError()

As i am using notebook if i uncomment the second line (util.startLoop()) it adds one more error about timeout..

Need help to get this done.

Big Thanks

  • What is the actual error from IB if there is any? Are you sure you are subscribed to data and are not running parallel accounts? If you run a production account with live data and a demo account for testing with only one data subscription, you will not get data on the latter. – misantroop May 30 '19 at 17:29
  • 1
    You can only connect with a unique clientId. Change it in case you're already connected. There have been cases where the disconnect doesn't work or takes a long time so just change the clientId. I don't use ib_insync but there is a user group by the author here https://groups.io/g/insync – brian Jun 02 '19 at 13:54

1 Answers1

0

Assign a different clientID to this connecion:

ib.connect('127.0.0.1', 5021, clientId=2)

Apparently you already have another connection with clientId=1.

aboger
  • 2,214
  • 6
  • 33
  • 47
t1user
  • 48
  • 3