I am trying to construct time series historical options greeks on SPX in the research environment. I have followed the instructions here but get errors indicating that the indicator_history method is failing.
This is my research notebook code:
# QuantBook Analysis Tool
# For more information see [https://www.quantconnect.com/docs/v2/our-platform/research/getting-started]
start = datetime(2024,2,9)
qb = QuantBook()
qb.set_start_date(start)
spx = qb.add_index("SPX", Resolution.Minute)
underlying_symbol = spx.symbol
# Get option chain
chain = qb.option_chain(
Symbol.create_canonical_option(spx.symbol, Market.USA, "?SPXW"), flatten=True
).data_frame
# Filter for 7-day expiry and -0.05 delta PUT option
expiry_date = qb.time + timedelta(7)
chain = chain[
(chain.expiry == pd.Timestamp(expiry_date)) &
(chain.right == OptionRight.PUT) &
(chain.delta > -0.06) & (chain.delta < -0.04)
]
# Select the contract closest to -0.05 delta
target_contract = chain.loc[[(chain['delta'] - (-0.05)).abs().idxmin()]]
contract_symbol = target_contract.index[-1]
option_contract = qb.add_index_option_contract(contract_symbol, fill_forward=False)
# Greeks and IV History
# Create Mirror Symbol
mirror_contract_symbol = Symbol.create_option(
option_contract.underlying.symbol, contract_symbol.id.market, option_contract.style,
OptionRight.Call if option_contract.right == OptionRight.PUT else OptionRight.PUT,
option_contract.strike_price, option_contract.expiry
)
# Set up risk-free interest rate, etc.
risk_free_rate_model = qb.risk_free_interest_rate_model
dividend_yield_model = DividendYieldProvider(underlying_symbol)
option_model = OptionPricingModelType.FORWARD_TREE
# Define method to return greeks and IV
def greeks_and_iv(contracts, period, risk_free_rate_model, dividend_yield_model, option_model):
# Get the call and put contract.
call, put = sorted(contracts, key=lambda s: s.id.option_right)
def get_values(indicator_class, contract, mirror_contract):
return qb.indicator_history(
indicator_class(contract, risk_free_rate_model, dividend_yield_model, mirror_contract, option_model),
[contract, mirror_contract, contract.underlying],
period
).data_frame.current
return pd.DataFrame({
'iv_call': get_values(ImpliedVolatility, call, put),
'iv_put': get_values(ImpliedVolatility, put, call),
'delta_call': get_values(Delta, call, put),
'delta_put': get_values(Delta, put, call),
'gamma_call': get_values(Gamma, call, put),
'gamma_put': get_values(Gamma, put, call),
'rho_call': get_values(Rho, call, put),
'rho_put': get_values(Rho, put, call),
'vega_call': get_values(Vega, call, put),
'vega_put': get_values(Vega, put, call),
'theta_call': get_values(Theta, call, put),
'theta_put': get_values(Theta, put, call),
})
# call the method
greeks_and_iv([contract_symbol, mirror_contract_symbol], 15, risk_free_rate_model, dividend_yield_model, option_model)
Which produces the following backtrace:
{
"name": "NotImplementedException",
"message": "Indicator.value must be implemented. Please implement this missing method in IV(SPX YG1PIISAP2XA|SPX 31,SPX 240216P04830000,QuantConnect.Data.InterestRateProvider,QuantConnect.Data.DividendYieldProvider,ForwardTree)
at QuantConnect.Indicators.PythonIndicator.SetIndicator(PyObject indicator) in /LeanCloud/CI.Builder/bin/Release/src/QuantConnect/LeanEnterprise/Indicators/PythonIndicator.cs:line 85
at QuantConnect.Indicators.PythonIndicator..ctor(PyObject indicator) in /LeanCloud/CI.Builder/bin/Release/src/QuantConnect/LeanEnterprise/Indicators/PythonIndicator.cs:line 59
at QuantConnect.Algorithm.QCAlgorithm.WrapPythonIndicator(PyObject pyObject, PythonIndicator convertedPythonIndicator) in /LeanCloud/CI.Builder/bin/Release/src/QuantConnect/LeanEnterprise/Algorithm/QCAlgorithm.Python.cs:line 1818
at QuantConnect.Algorithm.QCAlgorithm.IndicatorHistory(PyObject indicator, PyObject symbol, Int32 period, Nullable`1 resolution, PyObject selector) in /LeanCloud/CI.Builder/bin/Release/src/QuantConnect/LeanEnterprise/Algorithm/QCAlgorithm.Python.cs:line 1612
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithManyArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)",
"stack": "---------------------------------------------------------------------------
NotImplementedException Traceback (most recent call last)
Cell In[21], line 69
53 return pd.DataFrame({
54 'iv_call': get_values(ImpliedVolatility, call, put),
55 'iv_put': get_values(ImpliedVolatility, put, call),
(...)
65 'theta_put': get_values(Theta, put, call),
66 })
68 # call the method
---> 69 greeks_and_iv([contract_symbol, mirror_contract_symbol], 15, risk_free_rate_model, dividend_yield_model, option_model)
Cell In[21], line 54, in greeks_and_iv(contracts, period, risk_free_rate_model, dividend_yield_model, option_model)
46 def get_values(indicator_class, contract, mirror_contract):
47 return qb.indicator_history(
48 indicator_class(contract, risk_free_rate_model, dividend_yield_model, mirror_contract, option_model),
49 [contract, mirror_contract, contract.underlying],
50 period
51 ).data_frame.current
53 return pd.DataFrame({
---> 54 'iv_call': get_values(ImpliedVolatility, call, put),
55 'iv_put': get_values(ImpliedVolatility, put, call),
56 'delta_call': get_values(Delta, call, put),
57 'delta_put': get_values(Delta, put, call),
58 'gamma_call': get_values(Gamma, call, put),
59 'gamma_put': get_values(Gamma, put, call),
60 'rho_call': get_values(Rho, call, put),
61 'rho_put': get_values(Rho, put, call),
62 'vega_call': get_values(Vega, call, put),
63 'vega_put': get_values(Vega, put, call),
64 'theta_call': get_values(Theta, call, put),
65 'theta_put': get_values(Theta, put, call),
66 })
Cell In[21], line 47, in greeks_and_iv.<locals>.get_values(indicator_class, contract, mirror_contract)
46 def get_values(indicator_class, contract, mirror_contract):
---> 47 return qb.indicator_history(
48 indicator_class(contract, risk_free_rate_model, dividend_yield_model, mirror_contract, option_model),
49 [contract, mirror_contract, contract.underlying],
50 period
51 ).data_frame.current
NotImplementedException: Indicator.value must be implemented. Please implement this missing method in IV(SPX YG1PIISAP2XA|SPX 31,SPX 240216P04830000,QuantConnect.Data.InterestRateProvider,QuantConnect.Data.DividendYieldProvider,ForwardTree)
at QuantConnect.Indicators.PythonIndicator.SetIndicator(PyObject indicator) in /LeanCloud/CI.Builder/bin/Release/src/QuantConnect/LeanEnterprise/Indicators/PythonIndicator.cs:line 85
at QuantConnect.Indicators.PythonIndicator..ctor(PyObject indicator) in /LeanCloud/CI.Builder/bin/Release/src/QuantConnect/LeanEnterprise/Indicators/PythonIndicator.cs:line 59
at QuantConnect.Algorithm.QCAlgorithm.WrapPythonIndicator(PyObject pyObject, PythonIndicator convertedPythonIndicator) in /LeanCloud/CI.Builder/bin/Release/src/QuantConnect/LeanEnterprise/Algorithm/QCAlgorithm.Python.cs:line 1818
at QuantConnect.Algorithm.QCAlgorithm.IndicatorHistory(PyObject indicator, PyObject symbol, Int32 period, Nullable`1 resolution, PyObject selector) in /LeanCloud/CI.Builder/bin/Release/src/QuantConnect/LeanEnterprise/Algorithm/QCAlgorithm.Python.cs:line 1612
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithManyArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)"
}
Mia Alissi
Focus on ensuring that
computed_value
is appropriately calculated in thecompute_indicator
method for your specific use case.Sources: Custom Indicators - QuantConnect.com, Custom Indicators - QuantConnect.com, Greeks and IV Implementation, Indicators - QuantConnect.com, Option Indicators Regression Algorithm
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.
Louis Szeto
Hi FantasticMan
It is a bug related to an upgrade of the Option Greeks Indicators. You might follow the fixing progress of this issue. Right now, please workaround with either:
Best
Louis
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.
FantasticMan
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!