gpt4 book ai didi

python - 我如何在 Python 中接收来自 IBs API 的数据?

转载 作者:行者123 更新时间:2023-11-28 22:29:34 24 4
gpt4 key购买 nike

Interactive Brokers 刚刚发布了其 API 的 Python 版本。我正在尝试获取数据。

我正在使用“Program.py”中的“示例”,只是想获取帐户值。我只想知道帐户清算值(value)是多少,并将其输入 python。这是 documentation.这是创建和发送请求的代码:

        app = TestApp()
app.connect("127.0.0.1", 4001, clientId=0)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(),
app.twsConnectionTime()))
app.reqAccountSummary(9004, 'All', '$LEDGER')

我可以使用 IB 网关,并看到正在发送的请求,以及返回到 IB 网关的响应。我不知道如何将响应放入 Python。如果我正确阅读文档,我会看到:

Receiving

Summarised information is delivered via IBApi.EWrapper.accountSummary and IBApi.EWrapper.accountSummaryEnd

1 class TestWrapper(wrapper.EWrapper):
...
1 def accountSummary(self, reqId: int, account: str, tag: str, value: str,
2 currency: str):
3 super().accountSummary(reqId, account, tag, value, currency)
4 print("Acct Summary. ReqId:", reqId, "Acct:", account,
5 "Tag: ", tag, "Value:", value, "Currency:", currency)
6
...
1 def accountSummaryEnd(self, reqId: int):
2 super().accountSummaryEnd(reqId)
3 print("AccountSummaryEnd. Req Id: ", reqId)

我该怎么办?好像我调用这个函数来获取值,但这个函数需要我想要返回的值作为输入!我错过了什么!??!

感谢任何人可以提供的帮助。

编辑:

我认为这是“回调”:

@iswrapper
# ! [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)

这就是我感到困惑的地方。这似乎期望帐户有一个值(声明中的“值:str”),这正是我要求它产生的值。我找不到像下面这样说的地方:

myMonies = whateverTheHellGetsTheValue(reqID)

因此,“myMonies”将保留帐户值(value),我可以继续我的快乐之路。

最佳答案

我在这里回答了一个非常相似的问题。 https://stackoverflow.com/a/42868938/2855515

这是一个程序,我在同一个类中将 EWrapperEClient 子类化,并将其用于所有请求和接收回调。

您调用 EClient 方法请求数据,并通过 EWrapper 方法反馈。这些是带有 @iswrapper 符号的那些。

from ibapi import wrapper
from ibapi.client import EClient
from ibapi.utils import iswrapper #just for decorator
from ibapi.common import *

class TestApp(wrapper.EWrapper, EClient):
def __init__(self):
wrapper.EWrapper.__init__(self)
EClient.__init__(self, wrapper=self)

@iswrapper
def nextValidId(self, orderId:int):
print("setting nextValidOrderId: %d", orderId)
self.nextValidOrderId = orderId
# here is where you start using api
self.reqAccountSummary(9002, "All", "$LEDGER")

@iswrapper
def error(self, reqId:TickerId, errorCode:int, errorString:str):
print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)

@iswrapper
def accountSummary(self, reqId:int, account:str, tag:str, value:str, currency:str):
print("Acct Summary. ReqId:" , reqId , "Acct:", account,
"Tag: ", tag, "Value:", value, "Currency:", currency)

@iswrapper
def accountSummaryEnd(self, reqId:int):
print("AccountSummaryEnd. Req Id: ", reqId)
# now we can disconnect
self.disconnect()

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

if __name__ == "__main__":
main()

关于python - 我如何在 Python 中接收来自 IBs API 的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42939191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com