/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using Python.Runtime; using QuantConnect.Data; using QuantConnect.Python; namespace QuantConnect.Indicators { /// /// To provide a base class for option greeks indicator /// public abstract class OptionGreeksIndicatorBase : OptionIndicatorBase { private ImpliedVolatility _iv; private bool _userProvidedIv; /// /// Gets the implied volatility of the option /// public ImpliedVolatility ImpliedVolatility { get { return _iv; } set { _iv = value; _userProvidedIv = true; } } /// /// Initializes a new instance of the OptionGreeksIndicatorBase class /// /// The name of this indicator /// The option to be tracked /// Risk-free rate model /// Dividend yield model /// The mirror option for parity calculation /// The option pricing model used to estimate the Greek /// The option pricing model used to estimate IV protected OptionGreeksIndicatorBase(string name, Symbol option, IRiskFreeInterestRateModel riskFreeRateModel, IDividendYieldModel dividendYieldModel, Symbol mirrorOption = null, OptionPricingModelType? optionModel = null, OptionPricingModelType? ivModel = null) : base(name, option, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, period: 1) { ivModel = GetOptionModel(ivModel, option.ID.OptionStyle); _iv = new ImpliedVolatility(name + "_IV", option, riskFreeRateModel, dividendYieldModel, mirrorOption, ivModel.Value); } /// /// Initializes a new instance of the OptionGreeksIndicatorBase class /// /// The name of this indicator /// The option to be tracked /// Risk-free rate model /// Dividend yield, as a constant /// The mirror option for parity calculation /// The option pricing model used to estimate the Greek /// The option pricing model used to estimate IV protected OptionGreeksIndicatorBase(string name, Symbol option, IRiskFreeInterestRateModel riskFreeRateModel, decimal dividendYield = 0.0m, Symbol mirrorOption = null, OptionPricingModelType? optionModel = null, OptionPricingModelType? ivModel = null) : this(name, option, riskFreeRateModel, new ConstantDividendYieldModel(dividendYield), mirrorOption, optionModel, ivModel) { } /// /// Initializes a new instance of the OptionGreeksIndicatorBase class /// /// The name of this indicator /// The option to be tracked /// Risk-free rate, as a constant /// Dividend yield, as a constant /// The mirror option for parity calculation /// The option pricing model used to estimate the Greek /// The option pricing model used to estimate IV protected OptionGreeksIndicatorBase(string name, Symbol option, decimal riskFreeRate = 0.05m, decimal dividendYield = 0.0m, Symbol mirrorOption = null, OptionPricingModelType? optionModel = null, OptionPricingModelType? ivModel = null) : this(name, option, new ConstantRiskFreeRateInterestRateModel(riskFreeRate), new ConstantDividendYieldModel(dividendYield), mirrorOption, optionModel, ivModel) { } /// /// Initializes a new instance of the OptionGreeksIndicatorBase class /// /// The name of this indicator /// The option to be tracked /// Risk-free rate model /// Dividend yield model /// The mirror option for parity calculation /// The option pricing model used to estimate the Greek /// The option pricing model used to estimate IV protected OptionGreeksIndicatorBase(string name, Symbol option, PyObject riskFreeRateModel, PyObject dividendYieldModel, Symbol mirrorOption = null, OptionPricingModelType? optionModel = null, OptionPricingModelType? ivModel = null) : this(name, option, RiskFreeInterestRateModelPythonWrapper.FromPyObject(riskFreeRateModel), DividendYieldModelPythonWrapper.FromPyObject(dividendYieldModel), mirrorOption, optionModel, ivModel) { } /// /// Initializes a new instance of the OptionGreeksIndicatorBase class /// /// The name of this indicator /// The option to be tracked /// Risk-free rate model /// Dividend yield, as a constant /// The mirror option for parity calculation /// The option pricing model used to estimate the Greek /// The option pricing model used to estimate IV protected OptionGreeksIndicatorBase(string name, Symbol option, PyObject riskFreeRateModel, decimal dividendYield = 0.0m, Symbol mirrorOption = null, OptionPricingModelType? optionModel = null, OptionPricingModelType? ivModel = null) : this(name, option, RiskFreeInterestRateModelPythonWrapper.FromPyObject(riskFreeRateModel), new ConstantDividendYieldModel(dividendYield), mirrorOption, optionModel, ivModel) { } /// /// Gets a flag indicating when this indicator is ready and fully initialized /// public override bool IsReady => ImpliedVolatility.IsReady; /// /// Computes the next value of the option greek indicator /// /// The input is returned unmodified. protected override decimal ComputeIndicator() { var time = Price.Current.EndTime; if (!_userProvidedIv) { ImpliedVolatility.Update(DataBySymbol[OptionSymbol].CurrentInput); ImpliedVolatility.Update(DataBySymbol[_underlyingSymbol].CurrentInput); if (UseMirrorContract) { ImpliedVolatility.Update(DataBySymbol[_oppositeOptionSymbol].CurrentInput); } } RiskFreeRate.Update(time, _riskFreeInterestRateModel.GetInterestRate(time)); DividendYield.Update(time, _dividendYieldModel.GetDividendYield(time, UnderlyingPrice.Current.Value)); var timeTillExpiry = Convert.ToDecimal(OptionGreekIndicatorsHelper.TimeTillExpiry(Expiry, time)); try { IndicatorValue = timeTillExpiry < 0 ? 0 : CalculateGreek(timeTillExpiry); } catch (OverflowException) { //Log.Error($"OptionGreeksIndicatorBase.Calculate: Decimal overflow detected. The previous greek value will be used."); } return IndicatorValue; } /// /// Calculate the greek of the option /// protected abstract decimal CalculateGreek(decimal timeTillExpiry); /// /// Resets this indicator and all sub-indicators /// public override void Reset() { ImpliedVolatility.Reset(); base.Reset(); } } }