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