Lean  $LEAN_TAG$
OptionGreekIndicatorBase.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using Python.Runtime;
18 using QuantConnect.Data;
19 using QuantConnect.Python;
20 
22 {
23  /// <summary>
24  /// To provide a base class for option greeks indicator
25  /// </summary>
27  {
28  private ImpliedVolatility _iv;
29  private bool _userProvidedIv;
30 
31  /// <summary>
32  /// Gets the implied volatility of the option
33  /// </summary>
35  {
36  get
37  {
38  return _iv;
39  }
40  set
41  {
42  _iv = value;
43  _userProvidedIv = true;
44  }
45  }
46 
47  /// <summary>
48  /// Initializes a new instance of the OptionGreeksIndicatorBase class
49  /// </summary>
50  /// <param name="name">The name of this indicator</param>
51  /// <param name="option">The option to be tracked</param>
52  /// <param name="riskFreeRateModel">Risk-free rate model</param>
53  /// <param name="dividendYieldModel">Dividend yield model</param>
54  /// <param name="mirrorOption">The mirror option for parity calculation</param>
55  /// <param name="optionModel">The option pricing model used to estimate the Greek</param>
56  /// <param name="ivModel">The option pricing model used to estimate IV</param>
57  protected OptionGreeksIndicatorBase(string name, Symbol option, IRiskFreeInterestRateModel riskFreeRateModel, IDividendYieldModel dividendYieldModel,
58  Symbol mirrorOption = null, OptionPricingModelType? optionModel = null, OptionPricingModelType? ivModel = null)
59  : base(name, option, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, period: 1)
60  {
61  ivModel = GetOptionModel(ivModel, option.ID.OptionStyle);
62  _iv = new ImpliedVolatility(name + "_IV", option, riskFreeRateModel, dividendYieldModel, mirrorOption, ivModel.Value);
63  }
64 
65  /// <summary>
66  /// Initializes a new instance of the OptionGreeksIndicatorBase class
67  /// </summary>
68  /// <param name="name">The name of this indicator</param>
69  /// <param name="option">The option to be tracked</param>
70  /// <param name="riskFreeRateModel">Risk-free rate model</param>
71  /// <param name="dividendYield">Dividend yield, as a constant</param>
72  /// <param name="mirrorOption">The mirror option for parity calculation</param>
73  /// <param name="optionModel">The option pricing model used to estimate the Greek</param>
74  /// <param name="ivModel">The option pricing model used to estimate IV</param>
75  protected OptionGreeksIndicatorBase(string name, Symbol option, IRiskFreeInterestRateModel riskFreeRateModel, decimal dividendYield = 0.0m,
76  Symbol mirrorOption = null, OptionPricingModelType? optionModel = null, OptionPricingModelType? ivModel = null)
77  : this(name, option, riskFreeRateModel, new ConstantDividendYieldModel(dividendYield), mirrorOption, optionModel, ivModel)
78  {
79  }
80 
81  /// <summary>
82  /// Initializes a new instance of the OptionGreeksIndicatorBase class
83  /// </summary>
84  /// <param name="name">The name of this indicator</param>
85  /// <param name="option">The option to be tracked</param>
86  /// <param name="riskFreeRate">Risk-free rate, as a constant</param>
87  /// <param name="dividendYield">Dividend yield, as a constant</param>
88  /// <param name="mirrorOption">The mirror option for parity calculation</param>
89  /// <param name="optionModel">The option pricing model used to estimate the Greek</param>
90  /// <param name="ivModel">The option pricing model used to estimate IV</param>
91  protected OptionGreeksIndicatorBase(string name, Symbol option, decimal riskFreeRate = 0.05m, decimal dividendYield = 0.0m, Symbol mirrorOption = null,
92  OptionPricingModelType? optionModel = null, OptionPricingModelType? ivModel = null)
93  : this(name, option, new ConstantRiskFreeRateInterestRateModel(riskFreeRate), new ConstantDividendYieldModel(dividendYield),
94  mirrorOption, optionModel, ivModel)
95  {
96  }
97 
98  /// <summary>
99  /// Initializes a new instance of the OptionGreeksIndicatorBase class
100  /// </summary>
101  /// <param name="name">The name of this indicator</param>
102  /// <param name="option">The option to be tracked</param>
103  /// <param name="riskFreeRateModel">Risk-free rate model</param>
104  /// <param name="dividendYieldModel">Dividend yield model</param>
105  /// <param name="mirrorOption">The mirror option for parity calculation</param>
106  /// <param name="optionModel">The option pricing model used to estimate the Greek</param>
107  /// <param name="ivModel">The option pricing model used to estimate IV</param>
108  protected OptionGreeksIndicatorBase(string name, Symbol option, PyObject riskFreeRateModel, PyObject dividendYieldModel, Symbol mirrorOption = null,
109  OptionPricingModelType? optionModel = null, OptionPricingModelType? ivModel = null)
110  : this(name, option, RiskFreeInterestRateModelPythonWrapper.FromPyObject(riskFreeRateModel),
111  DividendYieldModelPythonWrapper.FromPyObject(dividendYieldModel), mirrorOption, optionModel, ivModel)
112  {
113  }
114 
115  /// <summary>
116  /// Initializes a new instance of the OptionGreeksIndicatorBase class
117  /// </summary>
118  /// <param name="name">The name of this indicator</param>
119  /// <param name="option">The option to be tracked</param>
120  /// <param name="riskFreeRateModel">Risk-free rate model</param>
121  /// <param name="dividendYield">Dividend yield, as a constant</param>
122  /// <param name="mirrorOption">The mirror option for parity calculation</param>
123  /// <param name="optionModel">The option pricing model used to estimate the Greek</param>
124  /// <param name="ivModel">The option pricing model used to estimate IV</param>
125  protected OptionGreeksIndicatorBase(string name, Symbol option, PyObject riskFreeRateModel, decimal dividendYield = 0.0m, Symbol mirrorOption = null,
126  OptionPricingModelType? optionModel = null, OptionPricingModelType? ivModel = null)
127  : this(name, option, RiskFreeInterestRateModelPythonWrapper.FromPyObject(riskFreeRateModel),
128  new ConstantDividendYieldModel(dividendYield), mirrorOption, optionModel, ivModel)
129  {
130  }
131 
132  /// <summary>
133  /// Gets a flag indicating when this indicator is ready and fully initialized
134  /// </summary>
135  public override bool IsReady => ImpliedVolatility.IsReady;
136 
137  /// <summary>
138  /// Computes the next value of the option greek indicator
139  /// </summary>
140  /// <returns>The input is returned unmodified.</returns>
141  protected override decimal ComputeIndicator()
142  {
143  var time = Price.Current.EndTime;
144 
145  if (!_userProvidedIv)
146  {
147  ImpliedVolatility.Update(DataBySymbol[OptionSymbol].CurrentInput);
148  ImpliedVolatility.Update(DataBySymbol[_underlyingSymbol].CurrentInput);
149  if (UseMirrorContract)
150  {
152  }
153  }
154 
156  DividendYield.Update(time, _dividendYieldModel.GetDividendYield(time, UnderlyingPrice.Current.Value));
157 
158  var timeTillExpiry = Convert.ToDecimal(OptionGreekIndicatorsHelper.TimeTillExpiry(Expiry, time));
159  try
160  {
161  IndicatorValue = timeTillExpiry < 0 ? 0 : CalculateGreek(timeTillExpiry);
162  }
163  catch (OverflowException)
164  {
165  //Log.Error($"OptionGreeksIndicatorBase.Calculate: Decimal overflow detected. The previous greek value will be used.");
166  }
167 
168  return IndicatorValue;
169  }
170 
171  /// <summary>
172  /// Calculate the greek of the option
173  /// </summary>
174  protected abstract decimal CalculateGreek(decimal timeTillExpiry);
175 
176  /// <summary>
177  /// Resets this indicator and all sub-indicators
178  /// </summary>
179  public override void Reset()
180  {
182  base.Reset();
183  }
184  }
185 }