简体   繁体   中英

IB TWS API - reqHistoricalData - keepUpToDate

I am working with a program that retrieves historical data for a given contract through IB TWS API. The issue that I am facing is that when I wish to change 'keepUpToDate' from False to True so that I keep receiving up-to-date data however, the program doesn't run.

Here is the code:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.common import BarData
import datetime


class MyWrapper(EWrapper):

    def nextValidId(self, orderId:int):
        print("Setting nextValidOrderId: %d", orderId)
        self.nextValidOrderId = orderId
        self.start()

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

    def historicalDataEnd(self, reqId: int, start: str, end: str):
        print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
        app.disconnect()
        print("Finished")

    def historicalDataUpdate(self, reqId: int, bar: BarData):
        print("HistoricalDataUpdate. ReqId:", reqId, "BarData.", bar)

    def error(self, reqId, errorCode, errorString):
        print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)

    def start(self):
        queryTime = (datetime.datetime.today() - datetime.timedelta(days=0)).strftime("%Y%m%d %H:%M:%S")

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

        app.reqHistoricalData(1, contract, queryTime, "2 D", "15 mins", "TRADES", 0, 1, False, [])

app = EClient(MyWrapper())
app.connect("127.0.0.1", 7496, clientId=123)
app.run()

I am trying to change the value on line 38 from False to True. I also believe that the problem may be in line 30, because the documentation states "If True, and endDateTime cannot be specified."

https://interactivebrokers.github.io/tws-api/historical_bars.html

Any input would be appreciated.

When you request data the initial 2 D has an end. In the historicalDataEnd you do app.disconnect()

Check your other question here for code https://stackoverflow.com/a/62800202/2855515

You can't state an end time if you want to use keepUpToDate. I don't use Python so null might be something else like None or "", I'm not sure what the syntax would be.

Change:

app.reqHistoricalData(1, contract, queryTime, "2 D", "15 mins", "TRADES", 0, 1, False, [])

to:

app.reqHistoricalData(1, contract, null, "2 D", "15 mins", "TRADES", 0, 1, True, [])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM