How to get historical data from Interactive Brokers

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

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

Leave a Reply

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