/* * 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 QuantConnect.Data; using QuantConnect.Data.Market; using QuantConnect.Securities.Option; using System; namespace QuantConnect.Indicators { /// /// Helper class that holds and updates the greeks indicators /// public class GreeksIndicators { private readonly static IRiskFreeInterestRateModel _interestRateProvider = new InterestRateProvider(); private readonly static IDividendYieldModel _constantDividendYieldModel = new ConstantDividendYieldModel(0); private readonly Symbol _optionSymbol; private readonly Symbol _mirrorOptionSymbol; /// /// Gets the implied volatility indicator /// public ImpliedVolatility ImpliedVolatility { get; } /// /// Gets the delta indicator /// public Delta Delta { get; } /// /// Gets the gamma indicator /// public Gamma Gamma { get; } /// /// Gets the vega indicator /// public Vega Vega { get; } /// /// Gets the daily theta indicator /// public Theta ThetaPerDay { get; } /// /// Gets the rho indicator /// public Rho Rho { get; } /// /// Gets the interest rate used in the calculations /// public decimal InterestRate => Delta.RiskFreeRate; /// /// Gets the dividend yield used in the calculations /// public decimal DividendYield => Delta.DividendYield; /// /// Gets the current greeks values /// public Greeks Greeks { get { return new Greeks(Delta, Gamma, Vega, Greeks.GetSafeTheta(ThetaPerDay.Current.Value), Rho, 0m); } } /// /// Whether the mirror option is set and will be used in the calculations. /// public bool UseMirrorOption => _mirrorOptionSymbol != null; /// /// Gets the current result of the greeks indicators, including the implied volatility, theoretical price and greeks values /// public OptionPriceModelResult CurrentResult => new OptionPriceModelResult(ImpliedVolatility.TheoreticalPrice, ImpliedVolatility, Greeks); /// /// Gets the dividend yield model to be used in the calculations for the specified option symbol. /// public static IDividendYieldModel GetDividendYieldModel(Symbol optionSymbol) { return optionSymbol.SecurityType != SecurityType.IndexOption ? DividendYieldProvider.CreateForOption(optionSymbol) : _constantDividendYieldModel; } /// /// Creates a new instance of the class /// public GreeksIndicators(Symbol optionSymbol, Symbol mirrorOptionSymbol, OptionPricingModelType? optionModel = null, OptionPricingModelType? ivModel = null, IDividendYieldModel dividendYieldModel = null, IRiskFreeInterestRateModel riskFreeInterestRateModel = null) { _optionSymbol = optionSymbol; _mirrorOptionSymbol = mirrorOptionSymbol; dividendYieldModel ??= GetDividendYieldModel(optionSymbol); riskFreeInterestRateModel ??= _interestRateProvider; ImpliedVolatility = new ImpliedVolatility(_optionSymbol, riskFreeInterestRateModel, dividendYieldModel, _mirrorOptionSymbol, ivModel); Delta = new Delta(_optionSymbol, riskFreeInterestRateModel, dividendYieldModel, _mirrorOptionSymbol, optionModel, ivModel); Gamma = new Gamma(_optionSymbol, riskFreeInterestRateModel, dividendYieldModel, _mirrorOptionSymbol, optionModel, ivModel); Vega = new Vega(_optionSymbol, riskFreeInterestRateModel, dividendYieldModel, _mirrorOptionSymbol, optionModel, ivModel); ThetaPerDay = new Theta(_optionSymbol, riskFreeInterestRateModel, dividendYieldModel, _mirrorOptionSymbol, optionModel, ivModel); Rho = new Rho(_optionSymbol, riskFreeInterestRateModel, dividendYieldModel, _mirrorOptionSymbol, optionModel, ivModel); Delta.ImpliedVolatility = ImpliedVolatility; Gamma.ImpliedVolatility = ImpliedVolatility; Vega.ImpliedVolatility = ImpliedVolatility; ThetaPerDay.ImpliedVolatility = ImpliedVolatility; Rho.ImpliedVolatility = ImpliedVolatility; } /// /// Feeds the specified data into the indicators /// public void Update(IBaseData data) { ImpliedVolatility.Update(data); Delta.Update(data); Gamma.Update(data); Vega.Update(data); ThetaPerDay.Update(data); Rho.Update(data); } /// /// Resets the indicators to their default state /// public void Reset() { ImpliedVolatility.Reset(); Delta.Reset(); Gamma.Reset(); Vega.Reset(); ThetaPerDay.Reset(); Rho.Reset(); } } }