Lean  $LEAN_TAG$
DataDictionary.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 
21 {
22  /// <summary>
23  /// Provides a base class for types holding base data instances keyed by symbol
24  /// </summary>
25  public class DataDictionary<T> : ExtendedDictionary<T>, IDictionary<Symbol, T>
26  {
27  // storage for the data
28  private readonly IDictionary<Symbol, T> _data = new Dictionary<Symbol, T>();
29 
30  /// <summary>
31  /// Initializes a new instance of the <see cref="QuantConnect.Data.Market.DataDictionary{T}"/> class.
32  /// </summary>
33  public DataDictionary()
34  {
35  }
36 
37  /// <summary>
38  /// Initializes a new instance of the <see cref="QuantConnect.Data.Market.DataDictionary{T}"/> class
39  /// using the specified <paramref name="data"/> as a data source
40  /// </summary>
41  /// <param name="data">The data source for this data dictionary</param>
42  /// <param name="keySelector">Delegate used to select a key from the value</param>
43  public DataDictionary(IEnumerable<T> data, Func<T, Symbol> keySelector)
44  {
45  foreach (var datum in data)
46  {
47  this[keySelector(datum)] = datum;
48  }
49  }
50 
51  /// <summary>
52  /// Initializes a new instance of the <see cref="QuantConnect.Data.Market.DataDictionary{T}"/> class.
53  /// </summary>
54  /// <param name="time">The time this data was emitted.</param>
55  public DataDictionary(DateTime time)
56  {
57 #pragma warning disable 618 // This assignment is left here until the Time property is removed.
58  Time = time;
59 #pragma warning restore 618
60  }
61 
62  /// <summary>
63  /// Gets or sets the time associated with this collection of data
64  /// </summary>
65  [Obsolete("The DataDictionary<T> Time property is now obsolete. All algorithms should use algorithm.Time instead.")]
66  public DateTime Time { get; set; }
67 
68  /// <summary>
69  /// Returns an enumerator that iterates through the collection.
70  /// </summary>
71  /// <returns>
72  /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
73  /// </returns>
74  /// <filterpriority>1</filterpriority>
75  public IEnumerator<KeyValuePair<Symbol, T>> GetEnumerator()
76  {
77  return _data.GetEnumerator();
78  }
79  /// <summary>
80  /// Returns an enumerator that iterates through a collection.
81  /// </summary>
82  /// <returns>
83  /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
84  /// </returns>
85  /// <filterpriority>2</filterpriority>
86  IEnumerator IEnumerable.GetEnumerator()
87  {
88  return ((IEnumerable)_data).GetEnumerator();
89  }
90 
91  /// <summary>
92  /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
93  /// </summary>
94  /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
95  public void Add(KeyValuePair<Symbol, T> item)
96  {
97  _data.Add(item);
98  }
99 
100  /// <summary>
101  /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
102  /// </summary>
103  /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
104  public override void Clear()
105  {
106  _data.Clear();
107  }
108 
109  /// <summary>
110  /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
111  /// </summary>
112  /// <returns>
113  /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
114  /// </returns>
115  /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
116  public bool Contains(KeyValuePair<Symbol, T> item)
117  {
118  return _data.Contains(item);
119  }
120 
121  /// <summary>
122  /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
123  /// </summary>
124  /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param><param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param><exception cref="T:System.ArgumentNullException"><paramref name="array"/> is null.</exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0.</exception><exception cref="T:System.ArgumentException">The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.</exception>
125  public void CopyTo(KeyValuePair<Symbol, T>[] array, int arrayIndex)
126  {
127  _data.CopyTo(array, arrayIndex);
128  }
129 
130  /// <summary>
131  /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
132  /// </summary>
133  /// <returns>
134  /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
135  /// </returns>
136  /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
137  public bool Remove(KeyValuePair<Symbol, T> item)
138  {
139  return _data.Remove(item);
140  }
141 
142  /// <summary>
143  /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
144  /// </summary>
145  /// <returns>
146  /// The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
147  /// </returns>
148  public int Count
149  {
150  get { return _data.Count; }
151  }
152 
153  /// <summary>
154  /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
155  /// </summary>
156  /// <returns>
157  /// true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.
158  /// </returns>
159  public override bool IsReadOnly
160  {
161  get { return _data.IsReadOnly; }
162  }
163 
164  /// <summary>
165  /// Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key.
166  /// </summary>
167  /// <returns>
168  /// true if the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the key; otherwise, false.
169  /// </returns>
170  /// <param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.</param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception>
171  public bool ContainsKey(Symbol key)
172  {
173  return _data.ContainsKey(key);
174  }
175 
176  /// <summary>
177  /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
178  /// </summary>
179  /// <param name="key">The object to use as the key of the element to add.</param><param name="value">The object to use as the value of the element to add.</param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception><exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.</exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.</exception>
180  public void Add(Symbol key, T value)
181  {
182  _data.Add(key, value);
183  }
184 
185  /// <summary>
186  /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
187  /// </summary>
188  /// <returns>
189  /// true if the element is successfully removed; otherwise, false. This method also returns false if <paramref name="key"/> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"/>.
190  /// </returns>
191  /// <param name="key">The key of the element to remove.</param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.</exception>
192  public override bool Remove(Symbol key)
193  {
194  return _data.Remove(key);
195  }
196 
197  /// <summary>
198  /// Gets the value associated with the specified key.
199  /// </summary>
200  /// <returns>
201  /// true if the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key; otherwise, false.
202  /// </returns>
203  /// <param name="key">The key whose value to get.</param><param name="value">When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the <paramref name="value"/> parameter. This parameter is passed uninitialized.</param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception>
204  public override bool TryGetValue(Symbol key, out T value)
205  {
206  return _data.TryGetValue(key, out value);
207  }
208 
209  /// <summary>
210  /// Gets or sets the element with the specified key.
211  /// </summary>
212  /// <returns>
213  /// The element with the specified key.
214  /// </returns>
215  /// <param name="symbol">The key of the element to get or set.</param>
216  /// <exception cref="T:System.ArgumentNullException"><paramref name="symbol"/> is null.</exception>
217  /// <exception cref="T:System.Collections.Generic.KeyNotFoundException">The property is retrieved and <paramref name="symbol"/> is not found.</exception>
218  /// <exception cref="T:System.NotSupportedException">The property is set and the <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.</exception>
219  public override T this[Symbol symbol]
220  {
221  get
222  {
223  T data;
224  if (TryGetValue(symbol, out data))
225  {
226  return data;
227  }
228  throw new KeyNotFoundException($"'{symbol}' wasn't found in the {GetType().GetBetterTypeName()} object, likely because there was no-data at this moment in time and it wasn't possible to fillforward historical data. Please check the data exists before accessing it with data.ContainsKey(\"{symbol}\")");
229  }
230  set
231  {
232  _data[symbol] = value;
233  }
234  }
235 
236  /// <summary>
237  /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
238  /// </summary>
239  /// <returns>
240  /// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
241  /// </returns>
242  public ICollection<Symbol> Keys
243  {
244  get { return _data.Keys; }
245  }
246 
247  /// <summary>
248  /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
249  /// </summary>
250  /// <returns>
251  /// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
252  /// </returns>
253  public ICollection<T> Values
254  {
255  get { return _data.Values; }
256  }
257 
258  /// <summary>
259  /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the Symbol objects of the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
260  /// </summary>
261  /// <returns>
262  /// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the Symbol objects of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
263  /// </returns>
264  protected override IEnumerable<Symbol> GetKeys => Keys;
265 
266  /// <summary>
267  /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
268  /// </summary>
269  /// <returns>
270  /// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
271  /// </returns>
272  protected override IEnumerable<T> GetValues => Values;
273 
274  /// <summary>
275  /// Gets the value associated with the specified key.
276  /// </summary>
277  /// <param name="key">The key whose value to get.</param>
278  /// <returns>
279  /// The value associated with the specified key, if the key is found; otherwise, the default value for the type of the <typeparamref name="T"/> parameter.
280  /// </returns>
281  public virtual T GetValue(Symbol key)
282  {
283  T value;
284  TryGetValue(key, out value);
285  return value;
286  }
287  }
288 
289  /// <summary>
290  /// Provides extension methods for the DataDictionary class
291  /// </summary>
292  public static class DataDictionaryExtensions
293  {
294  /// <summary>
295  /// Provides a convenience method for adding a base data instance to our data dictionary
296  /// </summary>
297  public static void Add<T>(this DataDictionary<T> dictionary, T data)
298  where T : BaseData
299  {
300  dictionary.Add(data.Symbol, data);
301  }
302  }
303 }