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– misantroop2019年05月30日 17:29:23 +00:00Commented May 30, 2019 at 17:29
-
1You 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 groups.io/g/insyncbrian– brian2019年06月02日 13:54:58 +00:00Commented Jun 2, 2019 at 13:54
1 Answer 1
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.