erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.84k stars 770 forks source link

CFD Historical Data Problem #679

Closed skalaydzhiyski closed 10 months ago

skalaydzhiyski commented 10 months ago

Discussed in https://github.com/erdewit/ib_insync/discussions/672

Originally posted by **skalaydzhiyski** December 18, 2023 Hi guys, I am pretty new to using ib_insync, but I find it a lot better than using the official IB API so here goes my problem. I am trying to retrieve historical data for a CFD contract (or get current market price) , but for some reason nothing happens. When requesting historical data it either times out or it returns that the contract was not found. Please see examples below. Historical Data example: ```python def get_historical_bars(contract, end_date, duration, interval): res = client.reqHistoricalData( contract, endDateTime=make_date(end_date), durationStr=duration, barSizeSetting=interval, whatToShow='TRADES', useRTH=True, ) res = util.df(res) res["date"] = pd.to_datetime(res["date"]) res = res.set_index("date") return res contract = Contract(secType='CFD', symbol='NVDA', exchange='SMART') client.qualifyContracts(contract) print(contract) res = get_historical_bars(contract, end_date='2023-02-05', duration='1 Y', interval='1 day') print(res) ``` output -> ``` reqHistoricalData: Timeout for Contract(secType='CFD', conId=120549668, symbol='NVDA', exchange='SMART', currency='USD', localSymbol='NVDAn', tradingClass='NVDA') Error 366, reqId 45: No historical data query found for ticker id:45, contract: Contract(secType='CFD', conId=120549668, symbol='NVDA', exchange='SMART', currency='USD', localSymbol='NVDAn', tradingClass='NVDA') reqHistoricalData: Timeout for Contract(secType='CFD', conId=120549668, symbol='NVDA', exchange='SMART', currency='USD', localSymbol='NVDAn', tradingClass='NVDA') Error 366, reqId 47: No historical data query found for ticker id:47, contract: Contract(secType='CFD', conId=120549668, symbol='NVDA', exchange='SMART', currency='USD', localSymbol='NVDAn', tradingClass='NVDA') ``` MktData example: ```python contract = Contract(secType='CFD', symbol='NVDA', exchange='SMART') client.qualifyContracts(contract) ticker = client.reqMktData(contract) print(ticker) print(ticker.marketPrice()) ``` output -> ``` Ticker(contract=Contract(secType='CFD', conId=120549668, symbol='NVDA', exchange='SMART', currency='USD', localSymbol='NVDAn', tradingClass='NVDA')) nan ``` I have tried NOT qualifying the contract, using MIDPOINT, using different dates, removing some of othe arguments in Contract, but to no effect. Any help here is welcome! I just need to see an example of how to retrieve data for the contract, I couldn't find anything useful online.
erdewit commented 10 months ago

If I try it it gives "No historical data query found for ticker id...", so there might be no historical data for stock CFDs.