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

2024-05-16 18:11:37 发布

您现在位置:Python中文网/ 问答频道 /正文

交互式代理刚刚发布了他们的API的python版本。我在试着获取数据。在

我用的是程序.py,并尝试获取帐户值。我只想知道账户清算值是多少,并将其输入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。如果我正确阅读文档,我会看到:

^{pr2}$

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

谢谢任何人能提供的帮助。在

编辑:

我想这是“回拨”:

@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),这正是我要求它产生的结果。我想说的是我找不到的东西:

myMonies = whateverTheHellGetsTheValue(reqID)

所以,'我的钱'将持有帐户价值,我可以继续我的快乐之路。在


Tags: 函数app网关valuetag帐户accountcurrency
3条回答

对于像我这样的新手:

请注意,我正试图:

    print(self.reqHistoricalData(4102, ContractSamples.EurGbpFx(), queryTime,
                               "1 M", "1 day", "MIDPOINT", 1, 1, False, []))

或者等待self.reqHistoricalData()的返回。 如上所述,EClient发送请求,EWrapper接收返回的信息。在

因此,似乎试图获得self.reqHistoricalData()的句柄不会得到任何东西(我得到None类型)

但是将请求添加到

^{pr2}$

足够让接收器(EWrapper)打印到控制台

更新日期:2019-01-05: EWrapper需要知道如何处理接收到的消息。 为了允许EWrapper为您提供一个句柄,例如打印到控制台;代码的编写者必须在代码中指定"# here is where you start using api"之外的代码中指定装饰语句

例如: 如果代码包含此代码段:

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

        queryTime = (datetime.datetime.today() - datetime.timedelta(days=5)).strftime("%Y%m%d %H:%M:%S")        
        self.reqHistoricalData(4102, ContractSamples.EurGbpFx(), queryTime,
                        "1 M", "1 day", "MIDPOINT", 1, 1, False, [])

    @iswrapper
    def historicalData(self, reqId:int, bar: BarData):
        print("HistoricalData. ReqId:", reqId, "BarData.", bar)

然后我们会得到一个打印到控制台。 如果忽略包装器装饰器方法,那么就没有打印到控制台。 如在本规范中:

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

        queryTime = (datetime.datetime.today() - datetime.timedelta(days=5)).strftime("%Y%m%d %H:%M:%S")        
        self.reqHistoricalData(4102, ContractSamples.EurGbpFx(), queryTime,
                        "1 M", "1 day", "MIDPOINT", 1, 1, False, [])

    #@iswrapper
    #def historicalData(self, reqId:int, bar: BarData):
    #    print("HistoricalData. ReqId:", reqId, "BarData.", bar)

因此,在您的情况下,寻找正确的回调。例如,如果您请求一个选项(即测试床/合同操作请求)。结果进入contractDetails(@iswrapper)中,您可以指定要执行的操作。。。也许打印(合同详细信息.summary.symbol)等。所以只需找到相应的帐户信息回调,然后打印/返回等,将其返回到程序中。在

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

这是一个程序,在这个程序中,我在同一个类中对EWrapper和{}进行子类化,并将其用于所有事务、请求和接收回调。在

调用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()

相关问题 更多 >