我正在使用一个程序,该程序通过IB TWS API检索给定合同的历史数据。我面临的问题是,当我希望将"keepUpToDate"从False更改为True以便继续接收最新数据时,程序不会运行。
这是代码:
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()
我正试图将第38行的值从False更改为True。我还认为问题可能在第30行,因为文件中指出";如果为True,则不能指定和endDateTime">
https://interactivebrokers.github.io/tws-api/historical_bars.html
任何意见都将不胜感激。
如果要使用keepUpToDate,则不能声明结束时间。我不使用Python,所以null可能是None或";,我不确定语法是什么。
更改:
app.reqHistoricalData(1, contract, queryTime, "2 D", "15 mins", "TRADES", 0, 1, False, [])
至:
app.reqHistoricalData(1, contract, null, "2 D", "15 mins", "TRADES", 0, 1, True, [])
当您请求数据时,初始2 D
有一个结束。在historicalDataEnd
中执行app.disconnect()
在此处检查您的其他问题以获取代码https://stackoverflow.com/a/62800202/2855515