我正在使用交互式代理的 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()