如何从Python中的IBs API接收数据? [英] How do I receive the data coming from IBs API in Python?

查看:289
本文介绍了如何从Python中的IBs API接收数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Interactive Brokers just released a python version of their API. I am trying to get data.

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

I am using the 'examples' in 'Program.py', and just trying to get account values. I just want to know what the account liquidation value is, and get that into python. This is the documentation. And this is the code to create and send the request:

        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中.如果我正确阅读了文档,则会看到以下内容:

I can use the IB Gateway, and see the request being sent, and the response coming back into IB Gateway. I cannot figure out how to get the response into Python. If I am reading the docs correctly, I see this:

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)

我该怎么办?似乎我调用此函数来获取值,但是此函数需要输入要返回的值!我想念什么!??!

What do I do with this? It seems like I call this function to get the values, but this function is requiring as an input the value I want returned! What am I missing!??!

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

Thanks for any help anyone can provide.

这是我认为的回调":

@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)

这就是我感到困惑的地方.这似乎期望该帐户有一个值(声明中的"value:str"),这正是我要它产生的值.我找不到类似以下内容的提示:

And this is where I am confused. This seems to expect a value for the account ('value: str' in the declaration), which is exactly what I am asking it to produce. I cannot find where I would say somehting like the following:

myMonies = whateverTheHellGetsTheValue(reqID)

因此,"myMonies"将保留该帐户的值,然后我可以继续愉快地工作.

So, 'myMonies' would then hold the account value, and I can continue on my merry way.

推荐答案

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

I answered a very similar question here. https://stackoverflow.com/a/42868938/2855515

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

Here is a program where I subclass the EWrapper and EClient in the same class and use that for everything, requests and receiving callbacks.

您调用EClient方法以请求数据,并且数据通过EWrapper方法反馈.这些是带有@iswrapper表示法的.

You call EClient methods to request data and it is fed back through the EWrapper methods. Those are the ones with the @iswrapper notation.

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中的IBs API接收数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆