简体   繁体   中英

How to set up an error handler for Interactive brokers using IB API or ib_insync? Python

• My script keeps running into "Error 10182, reqID 1639:Failed to request live updates (disconnected)” which is representative of the Historical Data Farm being inactive. • This error pops up during what is generally considered after hours (I'm trading FX). • I've tried adding print(util.globalErrorEvent). It prints everything 10s or so but no error was detected before or after the 10182 error.
• I'm connected through Gateway and currently using a paper account to test. • After the error occurs if I look at my IB Gateway app – everything is green except Historical Data Farm. This line is yellow and the status is Inactive: cashhmds • If I then stop my program and restart it Gateway Historical Data Farm is switched to Green and everything starts working again (no Gateway restart needed).

My big question is how to flag that Historical Data Farm is inactive so that I can 'gracefully' disconnect and reconnect to reset the live updates? If I could register that the Historical Data Farm was inactive then I could implement the logic to restart the connection. • If Historical Data Farm == inactive or if error = 10182 then I could implement a restart of the connection.

Once I hit this error real-time data updates stop forever until I stop my python script and restart it (basically restarting the connection process). The program doesn't exit or error out and other logic will continue (checking time to register when to close the program for the week for instance).

What you are looking for is a reconciliation logic, which by definition differs from strategy to strategy. In a situation similar to what you mentioned, one of the approaches can be as follows:

  • On every iteration of the run, you should check whether you are connected to IB TWS or not using the app.isConnected() method. For example, if your trading frequency is 5 mins, you will check every 5 mins before your strategy logic executes.

  • The above point takes care of the connection between your client and IB TWS. Another check to implement is to see whether you are connected to the internet or not. In this case, you can ping to your broker's server (if they provide) or any public websites.

    • If you don't receive a response from the ping command, you set up a loop that keeps checking until your connection is restored.

    • You can run this check in a loop with some interval.

  • If you have a disconnection; upon reconnection:

  1. The algorithm should check for all the open positions in the account. Ideally, those positions should be present in the account. If they are not, you need to define what to do based on your strategy logic. For example, adjust positions in your algorithm.
  2. Fetch the historical data for whatever time the algorithm was disconnected. This is to ensure that your algorithm doesn't calculate incorrect technical or statistical indicators if any.
  3. Setup the streaming data again. TWS will do this by itself; you need to cancel your current subscription and subscribe again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM