Lean  $LEAN_TAG$
OptionContract.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 
19 using System;
20 
22 {
23  /// <summary>
24  /// Defines a single option contract at a specific expiration and strike price
25  /// </summary>
26  public class OptionContract
27  {
28  private Lazy<OptionPriceModelResult> _optionPriceModelResult = new(() => OptionPriceModelResult.None);
29 
30  /// <summary>
31  /// Gets the option contract's symbol
32  /// </summary>
33  public Symbol Symbol
34  {
35  get; private set;
36  }
37 
38  /// <summary>
39  /// Gets the underlying security's symbol
40  /// </summary>
42  {
43  get; private set;
44  }
45 
46  /// <summary>
47  /// Gets the strike price
48  /// </summary>
49  public decimal Strike => Symbol.ID.StrikePrice;
50 
51  /// <summary>
52  /// Gets the strike price multiplied by the strike multiplier
53  /// </summary>
54  public decimal ScaledStrike
55  {
56  get;
57  private set;
58  }
59 
60  /// <summary>
61  /// Gets the expiration date
62  /// </summary>
63  public DateTime Expiry => Symbol.ID.Date;
64 
65  /// <summary>
66  /// Gets the right being purchased (call [right to buy] or put [right to sell])
67  /// </summary>
69 
70  /// <summary>
71  /// Gets the option style
72  /// </summary>
74 
75  /// <summary>
76  /// Gets the theoretical price of this option contract as computed by the <see cref="IOptionPriceModel"/>
77  /// </summary>
78  public decimal TheoreticalPrice => _optionPriceModelResult.Value.TheoreticalPrice;
79 
80  /// <summary>
81  /// Gets the implied volatility of the option contract as computed by the <see cref="IOptionPriceModel"/>
82  /// </summary>
83  public decimal ImpliedVolatility => _optionPriceModelResult.Value.ImpliedVolatility;
84 
85  /// <summary>
86  /// Gets the greeks for this contract
87  /// </summary>
88  public Greeks Greeks => _optionPriceModelResult.Value.Greeks;
89 
90  /// <summary>
91  /// Gets the local date time this contract's data was last updated
92  /// </summary>
93  public DateTime Time
94  {
95  get; set;
96  }
97 
98  /// <summary>
99  /// Gets the open interest
100  /// </summary>
101  public decimal OpenInterest
102  {
103  get; set;
104  }
105 
106  /// <summary>
107  /// Gets the last price this contract traded at
108  /// </summary>
109  public decimal LastPrice
110  {
111  get; set;
112  }
113 
114  /// <summary>
115  /// Gets the last volume this contract traded at
116  /// </summary>
117  public long Volume
118  {
119  get; set;
120  }
121 
122  /// <summary>
123  /// Gets the current bid price
124  /// </summary>
125  public decimal BidPrice
126  {
127  get; set;
128  }
129 
130  /// <summary>
131  /// Get the current bid size
132  /// </summary>
133  public long BidSize
134  {
135  get; set;
136  }
137 
138  /// <summary>
139  /// Gets the ask price
140  /// </summary>
141  public decimal AskPrice
142  {
143  get; set;
144  }
145 
146  /// <summary>
147  /// Gets the current ask size
148  /// </summary>
149  public long AskSize
150  {
151  get; set;
152  }
153 
154  /// <summary>
155  /// Gets the last price the underlying security traded at
156  /// </summary>
157  public decimal UnderlyingLastPrice
158  {
159  get; set;
160  }
161 
162  /// <summary>
163  /// Initializes a new instance of the <see cref="OptionContract"/> class
164  /// </summary>
165  /// <param name="security">The option contract security</param>
166  /// <param name="underlyingSymbol">The symbol of the underlying security</param>
167  public OptionContract(ISecurityPrice security, Symbol underlyingSymbol)
168  {
169  Symbol = security.Symbol;
170  UnderlyingSymbol = underlyingSymbol;
172  }
173 
174  /// <summary>
175  /// Sets the option price model evaluator function to be used for this contract
176  /// </summary>
177  /// <param name="optionPriceModelEvaluator">Function delegate used to evaluate the option price model</param>
178  internal void SetOptionPriceModel(Func<OptionPriceModelResult> optionPriceModelEvaluator)
179  {
180  _optionPriceModelResult = new Lazy<OptionPriceModelResult>(optionPriceModelEvaluator);
181  }
182 
183  /// <summary>
184  /// Returns a string that represents the current object.
185  /// </summary>
186  /// <returns>
187  /// A string that represents the current object.
188  /// </returns>
189  public override string ToString() => Symbol.Value;
190 
191  /// <summary>
192  /// Creates a <see cref="OptionContract"/>
193  /// </summary>
194  /// <param name="baseData"></param>
195  /// <param name="security">provides price properties for a <see cref="Security"/></param>
196  /// <param name="underlyingLastPrice">last price the underlying security traded at</param>
197  /// <returns>Option contract</returns>
198  public static OptionContract Create(BaseData baseData, ISecurityPrice security, decimal underlyingLastPrice)
199  => Create(baseData.Symbol.Underlying, baseData.EndTime, security, underlyingLastPrice);
200 
201  /// <summary>
202  /// Creates a <see cref="OptionContract"/>
203  /// </summary>
204  /// <param name="underlyingSymbol">The symbol of the underlying security</param>
205  /// <param name="endTime">local date time this contract's data was last updated</param>
206  /// <param name="security">provides price properties for a <see cref="Security"/></param>
207  /// <param name="underlyingLastPrice">last price the underlying security traded at</param>
208  /// <returns>Option contract</returns>
209  public static OptionContract Create(Symbol underlyingSymbol, DateTime endTime, ISecurityPrice security, decimal underlyingLastPrice)
210  {
211  return new OptionContract(security, underlyingSymbol)
212  {
213  Time = endTime,
214  LastPrice = security.Close,
215  Volume = (long)security.Volume,
216  BidPrice = security.BidPrice,
217  BidSize = (long)security.BidSize,
218  AskPrice = security.AskPrice,
219  AskSize = (long)security.AskSize,
220  OpenInterest = security.OpenInterest,
221  UnderlyingLastPrice = underlyingLastPrice
222  };
223  }
224  }
225 }