Lean  $LEAN_TAG$
OptionIndicatorBase.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 QuantConnect.Data;
18 
20 {
21  /// <summary>
22  /// To provide a base class for option indicator
23  /// </summary>
24  public abstract class OptionIndicatorBase : IndicatorBase<IndicatorDataPoint>, IIndicatorWarmUpPeriodProvider
25  {
26  /// <summary>
27  /// Option's symbol object
28  /// </summary>
29  protected readonly Symbol _optionSymbol;
30 
31  /// <summary>
32  /// Mirror option symbol (by option right), for implied volatility
33  /// </summary>
34  protected Symbol _oppositeOptionSymbol { get; private set; }
35 
36  /// <summary>
37  /// Underlying security's symbol object
38  /// </summary>
40 
41  /// <summary>
42  /// Option pricing model used to calculate indicator
43  /// </summary>
45 
46  /// <summary>
47  /// Risk-free rate model
48  /// </summary>
50 
51  /// <summary>
52  /// Dividend yield model, for continuous dividend yield
53  /// </summary>
55 
56  /// <summary>
57  /// Gets the expiration time of the option
58  /// </summary>
59  public DateTime Expiry => _optionSymbol.ID.Date;
60 
61  /// <summary>
62  /// Gets the option right (call/put) of the option
63  /// </summary>
65 
66  /// <summary>
67  /// Gets the strike price of the option
68  /// </summary>
69  public decimal Strike => _optionSymbol.ID.StrikePrice;
70 
71  /// <summary>
72  /// Gets the option style (European/American) of the option
73  /// </summary>
75 
76  /// <summary>
77  /// Risk Free Rate
78  /// </summary>
79  public Identity RiskFreeRate { get; set; }
80 
81  /// <summary>
82  /// Dividend Yield
83  /// </summary>
84  public Identity DividendYield { get; set; }
85 
86  /// <summary>
87  /// Gets the option price level
88  /// </summary>
90 
91  /// <summary>
92  /// Gets the mirror option price level, for implied volatility
93  /// </summary>
94  public IndicatorBase<IndicatorDataPoint> OppositePrice { get; private set; }
95 
96  /// <summary>
97  /// Gets the underlying's price level
98  /// </summary>
100 
101  /// <summary>
102  /// Flag if mirror option is implemented for parity type calculation
103  /// </summary>
104  public bool UseMirrorContract => _oppositeOptionSymbol != null;
105 
106  /// <summary>
107  /// Initializes a new instance of the OptionIndicatorBase class
108  /// </summary>
109  /// <param name="name">The name of this indicator</param>
110  /// <param name="option">The option to be tracked</param>
111  /// <param name="riskFreeRateModel">Risk-free rate model</param>
112  /// <param name="dividendYieldModel">Dividend yield model</param>
113  /// <param name="mirrorOption">The mirror option for parity calculation</param>
114  /// <param name="period">The lookback period of volatility</param>
115  /// <param name="optionModel">The option pricing model used to estimate the Greek/IV</param>
116  protected OptionIndicatorBase(string name, Symbol option, IRiskFreeInterestRateModel riskFreeRateModel, IDividendYieldModel dividendYieldModel,
117  Symbol mirrorOption = null, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes, int period = 2)
118  : base(name)
119  {
120  var sid = option.ID;
121  if (!sid.SecurityType.IsOption())
122  {
123  throw new ArgumentException("OptionIndicatorBase only support SecurityType.Option.");
124  }
125 
126  _optionSymbol = option;
127  _riskFreeInterestRateModel = riskFreeRateModel;
128  _dividendYieldModel = dividendYieldModel;
129  _optionModel = optionModel;
130 
131  RiskFreeRate = new Identity(name + "_RiskFreeRate");
132  DividendYield = new Identity(name + "_DividendYield");
133  Price = new Identity(name + "_Close");
134  UnderlyingPrice = new Identity(name + "_UnderlyingClose");
135 
136  if (mirrorOption != null)
137  {
138  _oppositeOptionSymbol = mirrorOption;
139  OppositePrice = new Identity(Name + "_OppositeClose");
140  }
141 
142  WarmUpPeriod = period;
143  }
144 
145  /// <summary>
146  /// Required period, in data points, for the indicator to be ready and fully initialized.
147  /// </summary>
148  public int WarmUpPeriod { get; set; }
149 
150  /// <summary>
151  /// Resets this indicator and all sub-indicators
152  /// </summary>
153  public override void Reset()
154  {
155  RiskFreeRate.Reset();
156  DividendYield.Reset();
157  Price.Reset();
158  UnderlyingPrice.Reset();
159  base.Reset();
160 
161  if (UseMirrorContract)
162  {
163  OppositePrice.Reset();
164  }
165  }
166  }
167 }