I am trying to get historical data of a specific stock from IBKR, but it seems like the testApp() function here is not being called or something. I'm able to connect to the TWS station, but somehow I'm not being fed back data, so was wondering if there is anything wrong with my Wrapper function?
Also, When I run the code, I get back this error message: "No Security Definition has been found for the request".
Any suggestions as to how I can go about fixing this?
class TestClient(EClient):
def __init__(self, wrapper):
EClient.__init__(self, wrapper)
class TestWrapper(wrapper.EWrapper):
def __init__(self):
wrapper.EWrapper.__init__(self)
class TestApp(TestWrapper, TestClient):
def __init__(self):
TestWrapper.__init__(self)
TestClient.__init__(self, wrapper=self)
def error(self, reqid, errorcode, errorstring):
print("Error: ", reqid, " ", errorcode, " ", errorstring)
def historicalData(self, reqid: int, bar: BarData):
print("HistoricalData. ReqId:", reqid, "BarData.", bar)
def historicalDataUpdate(self, reqid: int, bar: BarData):
print("HistoricalDataUpdate. 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 nextValidId(self, orderId: int):
super().nextValidId(orderId)
logging.debug("setting nextValidOrderId: %d", orderId)
self.nextValidOrderId = orderId
print("NextValidId:", orderId)
def contractDetails(self, reqId: int, contractDetails: ContractDetails):
super().contractDetails(reqId, contractDetails)
printinstance(contractDetails)
self.reqContractDetails(10004, ContractSamples.NewsFeedForQuery())
def contractDetailsEnd(self, reqId: int):
super().contractDetailsEnd(reqId)
print("ContractDetailsEnd. ReqId:", reqId)
def main():
app = TestApp()
app.connect("127.0.0.1", 7497, clientId=1)
print(app.isConnected())
time.sleep(1)
contract = Contract()
contract.secType = "STK"
contract.symbol = "AAPL"
contract.currency = "USD"
contract.exchange = "NASDAQ"
querytime = (datetime.datetime.today() - datetime.timedelta(days=180)).strftime("%Y%m%d %H:%M:%S")
app.reqHistoricalData(4001, contract, querytime, "1 M", "1 day", "MIDPOINT", 1, 1, False, [])
app.run()
if __name__ == "__main__":
main()
question from:
https://stackoverflow.com/questions/65832558/interactive-broker-no-security-definition-has-been-found-for-the-request