erdewit / ib_insync

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

reqHistoricalData OPTION_IMPLIED_VOLATILITY Historical Market Data Service error message:No historical market data for #458

Closed fritol closed 2 years ago

fritol commented 2 years ago

after running this contract =Option(symbol="SPY",strike=strike,lastTradeDateOrContractMonth='20220325',right="C",exchange="CBOE",multiplier=100,currency="USD") bars = ib.reqHistoricalData(contract, endDateTime = '', durationStr = '2 M', barSizeSetting = '1 hour', whatToShow = 'OPTION_IMPLIED_VOLATILITY', useRTH = True, formatDate = 1) i get Error 162, reqId 3: Historical Market Data Service error message:No historical market data for SPY/OPT@IBVOL OptionImpliedVol 3600, contract: Option(symbol='SPY', lastTradeDateOrContractMonth='20220325', strike=425.0, right='C', multiplier=100, exchange='CBOE', currency='USD') when I use e.g. whatToShow = 'TRADES' the code retrieves the bars fine!

I also tried it with exchange="SMART" with the same error.

mattsta commented 2 years ago

The reqHistoricalData() call can only calculate IV on underlying stocks/ETFs/indexes according to the grid in the API documents.

For option IV, you need to run reqMktData() for an option contract with tick fields 104 and 106 for IV calcuations (also in ibkr docs), wait for the returned Ticker from reqMktData() to refresh (can take 1-20 seconds for the IBKR server to begin updating your ticker request), then on the Ticker you created, just read .impliedVolatility and/or .histVolatility.

Also because the IBKR API is weird, the server-side IV calculations don't work during non-market hours for all symbols. You can always calculate IV without the server though since IV is just a derived value based on: the current underlying price (needs a live quote), the option strike price (known value), the current option market value (needs a live quote), and time until expiration (known value).