1

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

2
  • 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. Commented May 30, 2019 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 groups.io/g/insync Commented Jun 2, 2019 at 13:54

1 Answer 1

2

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.

dcio
2,3126 gold badges34 silver badges51 bronze badges
answered Sep 10, 2019 at 10:36
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this worked for me, helped build the connection.
This worked for me too. Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.