Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stop an ib_async script without causing IBGateway issues. #107

Open
jackroc97 opened this issue Jan 23, 2025 · 6 comments
Open

How to stop an ib_async script without causing IBGateway issues. #107

jackroc97 opened this issue Jan 23, 2025 · 6 comments

Comments

@jackroc97
Copy link

jackroc97 commented Jan 23, 2025

The problem that I am having goes something like this:

  1. Open IBGateway and authenticate
  2. Run my program which uses reqHistoricalData with keepUpToDate=True
  3. Program runs fine and data stream is kept up-to-date as expected
  4. Stop program using keyboard interrupt (ctrl+c)
  5. Run program again
  6. Wait for about 1 minute
  7. Receive error message: Error 162, reqId 4403: Historical Market Data Service error message:API historical data query cancelled: 4403, contract: Future(conId=603558932, symbol='ES', lastTradeDateOrContractMonth='20250321', multiplier='50', exchange='CME', currency='USD', localSymbol='ESH5', tradingClass='ES')
  8. Close IBGateway, then go back to step 1

How can I stop my program at-will without screwing up whatever is going on behind the scenes in the Gateway? Has anyone else had this issue before?

@gnzsnz
Copy link
Contributor

gnzsnz commented Jan 24, 2025

please check this notebook https://nbviewer.org/github/ib-api-reloaded/ib_async/blob/main/notebooks/basics.ipynb at the end it shows how to disconnect.

and the API documentation for disconnect

@mattsta
Copy link
Contributor

mattsta commented Jan 26, 2025

That is an odd situation because the gateway should be releasing all data requests/subscriptions/resources when your client disconnects from the gateway.

Feel free send a more complete example, but I don't see how the gateway would be the problem here.

Another potential cause: IBKR does enforce account-wide "pacing limitations" for trying to access too much market data too quickly, so if you start->stop->start->stop the same data feed many times (even across restarts), it's possible their upstream rate limiting is stopping new requests until the server-side service timeout resets your permissions again.

@jackroc97
Copy link
Author

Thanks all for the replies. I think this stems somehow from how I am stopping my program. For testing/debugging, I need a way to stop the program ad-hoc, and I have been using ctrl+c to interrupt and stop the program. To test if this was the factor which is causing these problems, I wrote a simple test script:

from ib_async import *

if __name__ == '__main__':
    try:
        ib = IB()
        ib.connect(port=7496)
    
        contract = Future(symbol="ES", lastTradeDateOrContractMonth="202503", exchange="CME")

        hist_data = ib.reqHistoricalData(
            contract, endDateTime="", durationStr="2 D", barSizeSetting="5 secs", 
            whatToShow="TRADES", useRTH=False) # keepUpToDate=True
    
        df = util.df(hist_data)
        print(df)
            
    finally:
        ib.disconnect()

As long as I don't interrupt the program with ctr+c, I can run this simple script over and over again. I believe this rules out the rate-limit hypothesis. Additionally, I don't think I am ever hitting the limits described in the docs.

The problem comes when I run this program but interrupt it before it comes to a stop. Re-running after interrupting the program causes the error I described in my initial post. The only way to reconnect is to close the Gateway, re-open it, and log back in.

I don't really know what to make of this... is there a "safe" way to stop the program while its running?

@August1328
Copy link

One of my scripts also uses reqHistoricalData with keepUpToDate=True.

To catch an user interrupt resp. CTRL + C, I always insert an except like this:

try:
    ...
    ib.connect()
   ...
except KeyboardInterrupt:
    ib.disconnect()
    print("Abborted")
    exit()

Maybe this helps... I have never had your problem.

@jackroc97
Copy link
Author

Thank you @August1328! Specifically catching the KeyboardInterrupt seems to have solved my problem. Maybe my understanding of how try... finally works is flawed, but I thought finally would execute no matter what.

@August1328
Copy link

Glad to hear.

I was just looking through the docs... Shall we add an example to the documentation, how to avoid issues if you use try? For example in the 'Code recipes' page? I am not familiar how to make changes to a repo, but I can give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants