Lean  $LEAN_TAG$
OptionChain.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 System.Collections;
18 using System.Collections.Generic;
19 using System.Linq;
21 using QuantConnect.Util;
22 
24 {
25  /// <summary>
26  /// Represents an entire chain of option contracts for a single underying security.
27  /// This type is <see cref="IEnumerable{OptionContract}"/>
28  /// </summary>
29  public class OptionChain : BaseData, IEnumerable<OptionContract>
30  {
31  private readonly Dictionary<Type, Dictionary<Symbol, List<BaseData>>> _auxiliaryData = new Dictionary<Type, Dictionary<Symbol, List<BaseData>>>();
32 
33  /// <summary>
34  /// Gets the most recent trade information for the underlying. This may
35  /// be a <see cref="Tick"/> or a <see cref="TradeBar"/>
36  /// </summary>
37  public BaseData Underlying
38  {
39  get; internal set;
40  }
41 
42  /// <summary>
43  /// Gets all ticks for every option contract in this chain, keyed by option symbol
44  /// </summary>
45  public Ticks Ticks
46  {
47  get; private set;
48  }
49 
50  /// <summary>
51  /// Gets all trade bars for every option contract in this chain, keyed by option symbol
52  /// </summary>
53  public TradeBars TradeBars
54  {
55  get; private set;
56  }
57 
58  /// <summary>
59  /// Gets all quote bars for every option contract in this chain, keyed by option symbol
60  /// </summary>
61  public QuoteBars QuoteBars
62  {
63  get; private set;
64  }
65 
66  /// <summary>
67  /// Gets all contracts in the chain, keyed by option symbol
68  /// </summary>
70  {
71  get; private set;
72  }
73 
74  /// <summary>
75  /// Gets the set of symbols that passed the <see cref="Option.ContractFilter"/>
76  /// </summary>
77  public HashSet<Symbol> FilteredContracts
78  {
79  get; private set;
80  }
81 
82  /// <summary>
83  /// Initializes a new default instance of the <see cref="OptionChain"/> class
84  /// </summary>
85  private OptionChain()
86  {
87  DataType = MarketDataType.OptionChain;
88  }
89 
90  /// <summary>
91  /// Initializes a new instance of the <see cref="OptionChain"/> class
92  /// </summary>
93  /// <param name="canonicalOptionSymbol">The symbol for this chain.</param>
94  /// <param name="time">The time of this chain</param>
95  public OptionChain(Symbol canonicalOptionSymbol, DateTime time)
96  {
97  Time = time;
98  Symbol = canonicalOptionSymbol;
99  DataType = MarketDataType.OptionChain;
100  Ticks = new Ticks(time);
101  TradeBars = new TradeBars(time);
102  QuoteBars = new QuoteBars(time);
103  Contracts = new OptionContracts(time);
104  FilteredContracts = new HashSet<Symbol>();
105  Underlying = new QuoteBar();
106  }
107 
108  /// <summary>
109  /// Initializes a new instance of the <see cref="OptionChain"/> class
110  /// </summary>
111  /// <param name="canonicalOptionSymbol">The symbol for this chain.</param>
112  /// <param name="time">The time of this chain</param>
113  /// <param name="underlying">The most recent underlying trade data</param>
114  /// <param name="trades">All trade data for the entire option chain</param>
115  /// <param name="quotes">All quote data for the entire option chain</param>
116  /// <param name="contracts">All contracts for this option chain</param>
117  /// <param name="filteredContracts">The filtered list of contracts for this option chain</param>
118  public OptionChain(Symbol canonicalOptionSymbol, DateTime time, BaseData underlying, IEnumerable<BaseData> trades, IEnumerable<BaseData> quotes, IEnumerable<OptionContract> contracts, IEnumerable<Symbol> filteredContracts)
119  {
120  Time = time;
121  Underlying = underlying;
122  Symbol = canonicalOptionSymbol;
123  DataType = MarketDataType.OptionChain;
124  FilteredContracts = filteredContracts.ToHashSet();
125 
126  Ticks = new Ticks(time);
127  TradeBars = new TradeBars(time);
128  QuoteBars = new QuoteBars(time);
129  Contracts = new OptionContracts(time);
130 
131  foreach (var trade in trades)
132  {
133  var tick = trade as Tick;
134  if (tick != null)
135  {
136  List<Tick> ticks;
137  if (!Ticks.TryGetValue(tick.Symbol, out ticks))
138  {
139  ticks = new List<Tick>();
140  Ticks[tick.Symbol] = ticks;
141  }
142  ticks.Add(tick);
143  continue;
144  }
145  var bar = trade as TradeBar;
146  if (bar != null)
147  {
148  TradeBars[trade.Symbol] = bar;
149  }
150  }
151 
152  foreach (var quote in quotes)
153  {
154  var tick = quote as Tick;
155  if (tick != null)
156  {
157  List<Tick> ticks;
158  if (!Ticks.TryGetValue(tick.Symbol, out ticks))
159  {
160  ticks = new List<Tick>();
161  Ticks[tick.Symbol] = ticks;
162  }
163  ticks.Add(tick);
164  continue;
165  }
166  var bar = quote as QuoteBar;
167  if (bar != null)
168  {
169  QuoteBars[quote.Symbol] = bar;
170  }
171  }
172 
173  foreach (var contract in contracts)
174  {
175  Contracts[contract.Symbol] = contract;
176  }
177  }
178 
179  /// <summary>
180  /// Gets the auxiliary data with the specified type and symbol
181  /// </summary>
182  /// <typeparam name="T">The type of auxiliary data</typeparam>
183  /// <param name="symbol">The symbol of the auxiliary data</param>
184  /// <returns>The last auxiliary data with the specified type and symbol</returns>
185  public T GetAux<T>(Symbol symbol)
186  {
187  List<BaseData> list;
188  Dictionary<Symbol, List<BaseData>> dictionary;
189  if (!_auxiliaryData.TryGetValue(typeof(T), out dictionary) || !dictionary.TryGetValue(symbol, out list))
190  {
191  return default(T);
192  }
193  return list.OfType<T>().LastOrDefault();
194  }
195 
196  /// <summary>
197  /// Gets all auxiliary data of the specified type as a dictionary keyed by symbol
198  /// </summary>
199  /// <typeparam name="T">The type of auxiliary data</typeparam>
200  /// <returns>A dictionary containing all auxiliary data of the specified type</returns>
202  {
203  Dictionary<Symbol, List<BaseData>> d;
204  if (!_auxiliaryData.TryGetValue(typeof(T), out d))
205  {
206  return new DataDictionary<T>();
207  }
208  var dictionary = new DataDictionary<T>();
209  foreach (var kvp in d)
210  {
211  var item = kvp.Value.OfType<T>().LastOrDefault();
212  if (item != null)
213  {
214  dictionary.Add(kvp.Key, item);
215  }
216  }
217  return dictionary;
218  }
219 
220  /// <summary>
221  /// Gets all auxiliary data of the specified type as a dictionary keyed by symbol
222  /// </summary>
223  /// <typeparam name="T">The type of auxiliary data</typeparam>
224  /// <returns>A dictionary containing all auxiliary data of the specified type</returns>
225  public Dictionary<Symbol, List<BaseData>> GetAuxList<T>()
226  {
227  Dictionary<Symbol, List<BaseData>> dictionary;
228  if (!_auxiliaryData.TryGetValue(typeof(T), out dictionary))
229  {
230  return new Dictionary<Symbol, List<BaseData>>();
231  }
232  return dictionary;
233  }
234 
235  /// <summary>
236  /// Gets a list of auxiliary data with the specified type and symbol
237  /// </summary>
238  /// <typeparam name="T">The type of auxiliary data</typeparam>
239  /// <param name="symbol">The symbol of the auxiliary data</param>
240  /// <returns>The list of auxiliary data with the specified type and symbol</returns>
241  public List<T> GetAuxList<T>(Symbol symbol)
242  {
243  List<BaseData> list;
244  Dictionary<Symbol, List<BaseData>> dictionary;
245  if (!_auxiliaryData.TryGetValue(typeof(T), out dictionary) || !dictionary.TryGetValue(symbol, out list))
246  {
247  return new List<T>();
248  }
249  return list.OfType<T>().ToList();
250  }
251 
252  /// <summary>
253  /// Returns an enumerator that iterates through the collection.
254  /// </summary>
255  /// <returns>
256  /// An enumerator that can be used to iterate through the collection.
257  /// </returns>
258  public IEnumerator<OptionContract> GetEnumerator()
259  {
260  return Contracts.Values.GetEnumerator();
261  }
262 
263  /// <summary>
264  /// Returns an enumerator that iterates through a collection.
265  /// </summary>
266  /// <returns>
267  /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
268  /// </returns>
269  IEnumerator IEnumerable.GetEnumerator()
270  {
271  return GetEnumerator();
272  }
273 
274  /// <summary>
275  /// Return a new instance clone of this object, used in fill forward
276  /// </summary>
277  /// <returns>A clone of the current object</returns>
278  public override BaseData Clone()
279  {
280  return new OptionChain
281  {
283  Ticks = Ticks,
288  Symbol = Symbol,
289  Time = Time,
290  DataType = DataType,
291  Value = Value
292  };
293  }
294 
295  /// <summary>
296  /// Adds the specified auxiliary data to this option chain
297  /// </summary>
298  /// <param name="baseData">The auxiliary data to be added</param>
299  internal void AddAuxData(BaseData baseData)
300  {
301  var type = baseData.GetType();
302  Dictionary<Symbol, List<BaseData>> dictionary;
303  if (!_auxiliaryData.TryGetValue(type, out dictionary))
304  {
305  dictionary = new Dictionary<Symbol, List<BaseData>>();
306  _auxiliaryData[type] = dictionary;
307  }
308 
309  List<BaseData> list;
310  if (!dictionary.TryGetValue(baseData.Symbol, out list))
311  {
312  list = new List<BaseData>();
313  dictionary[baseData.Symbol] = list;
314  }
315  list.Add(baseData);
316  }
317  }
318 }