How to get historical data from Interactive Brokers

Work in progress… details to follow but this some sample code that works

    def historicalDataOperations_req(self):
        contract = Contract()
        asset_info = quotes_library.get_asset_info()
        for val in asset_info:
            self.assetId = val['assetId']
            tickerId = val['assetId']
            contract = Contract()
            contract.symbol = val['base_symbol']
            contract.secType = "FUT"
   = val['exchange']
            contract.currency = "USD"
            contract.lastTradeDateOrContractMonth = val['lastTradeDateOrContractMonth']
            self.reqHistoricalData(tickerId, contract, "", "1 D", "1 min", "TRADES", 0, 1, True,[])
    def historicalDataUpdate(self, reqId: int, bar: BarData):
        # print("HistoricalDataUpdate. ReqId:", reqId, "BarData.", bar)
    # ! [historicaldataend]
    def historicalDataEnd(self, reqId: int, start: str, end: str):
        super().historicalDataEnd(reqId, start, end)
        print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
    # ! [historicaldataend]

Leave a Reply

Your email address will not be published. Required fields are marked *