【问题标题】:Ibpy: how to capture data returned from reqAccountSummaryIbpy:如何捕获从 reqAccountSummary 返回的数据
【发布时间】:2017-11-07 06:57:50
【问题描述】:

我正在使用来自交互式代理的 ibapi,但我通常无法捕获返回的数据。例如,根据api docs,当我请求reqAccountSummary()时,该方法通过accountSummary()传递数据。但他们的例子只打印数据。我尝试过捕获数据或将其分配给变量,但他们的文档中没有显示如何执行此操作。我也谷歌搜索,只找到 register() 和 registerAll() 但这来自 ib.opt ,它不在最新的工作 ibapi 包中。

这是我的代码。你能告诉我如何修改 accountSummary() 来捕获数据吗?

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
class TestApp(EWrapper,EClient):
    def __init__(self):
        EClient.__init__(self,self)

    # request account data:
    def my_reqAccountSummary1(self, reqId:int, groupName:str, tags:str):
        self.reqAccountSummary(reqId, "All", "TotalCashValue")


    # The received data is passed to accountSummary()
    def accountSummary(self, reqId: int, account: str, tag: str, value: str, currency: str):
        super().accountSummary(reqId, account, tag, value, currency)
        print("Acct# Summary. ReqId>:", reqId, "Acct:", account, "Tag: ", tag, "Value:", value, "Currency:", currency)
        return value  #This is my attempt which doesn't work


def main():
    app = TestApp()
    app.connect("127.0.0.1",7497,clientId=0)

    app.my_reqAccountSummary1(8003, "All", "TotalCashValue")  #"This works, but the data is print to screen. I don't know how to assign the received TotalCashValue to a variable"

    # myTotalCashValue=app.my_reqAccountSummary1(8003, "All", "TotalCashValue")  #"My attempt doesn't work"
    # more code to stop trading if myTotalCashValue is low

    app.run()

if __name__=="__main__":
    main()

【问题讨论】:

    标签: python-3.x ibpy


    【解决方案1】:

    您不能在主函数中执行此操作,因为app.run 监听来自 TWS 的响应。一旦您正确设置了所有回调,主函数将在app.run 中永远循环。

    您必须将代码直接放入accountSummary 函数中。这就是这些程序的工作方式,您将逻辑直接放入 回调函数。您始终可以分配self.myTotalCashValue = value 以使其可用于您班级的其他部分,甚至可以用于其他线程。

    -- 或者--

    您在线程中运行 app.run 并等待返回值,例如

    将self._myTotalCashValue = value 添加到accountSummary,导入threading 和time,然后在main 中添加类似这样的内容:

    t = threading.Thread(target=app.run)
    t.daemon = True
    t.start()
    while not hasattr(app,"_myTotalCashValue"):
        time.sleep(1)
    print(app._myTotalCashValue)
    

    像往常一样使用线程,您必须小心app 和main 之间的共享内存。

    【讨论】:

      猜你喜欢
      • 2010-12-20
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 2015-04-21
      相关资源
      最近更新 更多