ibpy:如何捕获从reqAccountSummary返回的数据

2024-01-03

我正在使用交互式代理的 ibapi,并且一般情况下我陷入了如何捕获返回数据的困境。例如,根据api docs https://interactivebrokers.github.io/tws-api/account_summary.html#acct_summary_req,当我请求 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()

您不能在主函数中执行此操作,因为app.run听取交易平台的回应。一旦您正确设置了所有回调,主函数将永远循环app.run.

您必须将代码直接放入accountSummary功能。这就是此类程序的工作方式,您将逻辑直接放入 回调函数。您随时可以分配self.myTotalCashValue = value使其可供班级的其他部分,甚至另一个线程使用。

-- OR --

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

add self._myTotalCashValue = value到账户总结,导入threading and 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 and main.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ibpy:如何捕获从reqAccountSummary返回的数据 的相关文章

随机推荐