简体   繁体   English

如何使用 IB API 或 ib_insync 为 Interactive 代理设置错误处理程序? Python

[英]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. • 我的脚本一直运行到“错误 10182,reqID 1639:无法请求实时更新(断开连接)” ,这表示历史数据农场处于非活动状态。 • 此错误在通常认为的下班后弹出(我正在交易FX). • 我尝试添加 print(util.globalErrorEvent)。它会在 10 秒左右打印所有内容,但在 10182 错误之前或之后都没有检测到错误。
• 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. • 如果我查看我的IB Gateway 应用程序,则在发生错误后– 除了历史数据农场之外,所有内容都是绿色的。 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).此行为黄色且状态为非活动:cashhmds • 如果我随后停止我的程序并重新启动它,网关历史数据农场将切换为绿色,并且一切都重新开始工作(无需网关重新启动)。

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. • 如果Historical Data Farm == inactive 或error = 10182,那么我可以重新启动连接。

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).一旦我遇到这个错误,实时数据更新就会永远停止,直到我停止我的 python 脚本并重新启动它(基本上重新启动连接过程)。 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.在运行的每次迭代中,您应该使用 app.isConnected() 方法检查您是否连接到 IB TWS。 For example, if your trading frequency is 5 mins, you will check every 5 mins before your strategy logic executes.例如,如果您的交易频率为 5 分钟,您将在策略逻辑执行前每 5 分钟检查一次。

  • The above point takes care of the connection between your client and IB TWS.上述要点负责您的客户和 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.在这种情况下,您可以 ping 到您的经纪人的服务器(如果他们提供)或任何公共网站。

    • If you don't receive a response from the ping command, you set up a loop that keeps checking until your connection is restored.如果您没有收到来自 ping 命令的响应,则您设置了一个循环,该循环会一直检查直到您的连接恢复。

    • 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; TWS 将自行完成; you need to cancel your current subscription and subscribe again.您需要取消当前订阅并重新订阅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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