Hi QuantConnect Community,
I am trying to plot the implied volatility. I got this error when I pass an option contract to the IV function, I get this error: Exception: Please register to receive data for symbol 'SPY YJOKFAJ2WRD2|SPY R735QTJ8XC9X' using the AddSecurity() function. at QuantConnect.Algorithm.QCAlgorithm.GetSubscription(Symbol symbol, Nullable`1 tickType) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/LeanEnterprise/Algorithm/QCAlgorithm.Indicators.cs:line 2607 at QuantConnect.Algorithm.QCAlgorithm.CreateIndicatorName(Symbol symbol, String type, Nullable`1 resolution) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/LeanEnterprise/Algorithm/QCAlgorithm.Indicators.cs:line 2532.
The code is as follows:
current_date = qb.Time
option_chain = qb.OptionChainProvider.GetOptionContractList(spy_option.Symbol, current_date)
history = qb.History(spy.Symbol, 5, Resolution.Daily)
current_price = history['close'].iloc[-1]
# Define your filters
min_strike = current_price * 0.97 # -3% of current equity price
max_strike = current_price * 1.03 # +3% of current equity price
min_expiry = current_date + timedelta(20)
max_expiry = current_date + timedelta(40)
# Filter the option contracts
filtered_contracts = [contract for contract in option_chain if min_strike <= contract.ID.StrikePrice <= max_strike and min_expiry <= contract.ID.Date <= max_expiry]
# Display filtered contracts
for contract in filtered_contracts:
underlying_history = qb.History(contract.Underlying, 30, Resolution.Daily)
implied_volatility = qb.iv(contract)
print(f"Symbol: {contract}, Strike: {contract.ID.StrikePrice}, Expiry: {contract.ID.Date}, impliedvolatility = {implied_volatility}")
Mia Alissi
The error message you're seeing is indicating that you need to register the option contract with the
AddSecurity()
function before you can use it. This is a necessary step because it tells the algorithm to subscribe to data for that particular security.In your case, you can add the following line of code before you calculate the implied volatility:
So your code would look something like this:
This should resolve the error you're seeing. If you continue to have issues, please let us know!
For more information, you can refer to the following knowledge sources:
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Kashi Raman
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!