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

luk*_*awk 4 python interactive-brokers ibpy

盈透证券刚刚发布了他们 API 的 Python 版本。我正在尝试获取数据。

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

        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')
Run Code Online (Sandbox Code Playgroud)

我可以使用 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)
Run Code Online (Sandbox Code Playgroud)

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

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

编辑:

这是我认为的“回调”:

@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)
Run Code Online (Sandbox Code Playgroud)

这就是我感到困惑的地方。这似乎期望帐户的值(声明中的'value:str'),这正是我要求它产生的。我找不到我会说以下的话的地方:

myMonies = whateverTheHellGetsTheValue(reqID)
Run Code Online (Sandbox Code Playgroud)

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

bri*_*ian 8

我在这里回答了一个非常相似的问题。 /sf/answers/3000825691/

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

您调用 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()
Run Code Online (Sandbox Code Playgroud)