5

盈透证券刚刚发布了他们 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')

我可以使用 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)

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

myMonies = whateverTheHellGetsTheValue(reqID)

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

4

3 回答 3

10

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

这是一个程序,我将 and 子类化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()
于 2017-03-24T19:15:36.610 回答
2

对于像我这样的其他新手:

注意也;我试图:

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

或者暂停self.reqHistoricalData(). 正如上面提到的布赖恩;EClient发送请求并EWrapper接收回信息。

所以似乎试图掌握self.reqHistoricalData()不会让你得到任何东西(我得到None类型)

但是将请求添加到

    @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")
        self.reqHistoricalData(4102, ContractSamples.EurGbpFx(), queryTime,
                               "1 M", "1 day", "MIDPOINT", 1, 1, False, [])

        contract = Contract()
        contract.symbol = "AAPL"
        contract.secType = "STK"
        contract.currency = "USD"
        contract.exchange = "SMART"

        self.reqHistoricalData(4103, contract, queryTime,
                               "1 M", "1 day", "MIDPOINT", 1, 1, False, [])

足以让接收器 ( 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)
于 2019-12-26T09:00:02.803 回答
0

所以在你的情况下寻找正确的回调。例如,如果您请求一个选项(即testbed/contractOperations_req)。结果进入contractDetails(@iswrapper),您可以在其中指定您想要做什么......也许是print(contractDetails.summary.symbol)等。所以只需找到帐户信息的相应回调,然后打印/返回/等。它回到你的程序。

于 2017-03-22T19:44:18.113 回答