/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using QuantConnect.Python; using System; using System.Collections; using System.Collections.Generic; namespace QuantConnect.Data.Market { /// /// Provides a base class for types holding base data instances keyed by symbol /// [PandasNonExpandable] public class DataDictionary : ExtendedDictionary, IDictionary { // storage for the data private readonly IDictionary _data = new Dictionary(); /// /// Initializes a new instance of the class. /// public DataDictionary() { } /// /// Initializes a new instance of the class /// using the specified as a data source /// /// The data source for this data dictionary /// Delegate used to select a key from the value public DataDictionary(IEnumerable data, Func keySelector) { foreach (var datum in data) { this[keySelector(datum)] = datum; } } /// /// Initializes a new instance of the class. /// /// The time this data was emitted. public DataDictionary(DateTime time) { #pragma warning disable 618 // This assignment is left here until the Time property is removed. Time = time; #pragma warning restore 618 } /// /// Gets or sets the time associated with this collection of data /// public DateTime Time { get; set; } /// /// Returns an enumerator that iterates through the collection. /// /// /// A that can be used to iterate through the collection. /// /// 1 public IEnumerator> GetEnumerator() { return _data.GetEnumerator(); } /// /// Returns an enumerator that iterates through a collection. /// /// /// An object that can be used to iterate through the collection. /// /// 2 IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)_data).GetEnumerator(); } /// /// Adds an item to the . /// /// The object to add to the .The is read-only. public void Add(KeyValuePair item) { _data.Add(item); } /// /// Removes all items from the . /// /// The is read-only. public override void Clear() { _data.Clear(); } /// /// Determines whether the contains a specific value. /// /// /// true if is found in the ; otherwise, false. /// /// The object to locate in the . public bool Contains(KeyValuePair item) { return _data.Contains(item); } /// /// Copies the elements of the to an , starting at a particular index. /// /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing.The zero-based index in at which copying begins. is null. is less than 0.The number of elements in the source is greater than the available space from to the end of the destination . public void CopyTo(KeyValuePair[] array, int arrayIndex) { _data.CopyTo(array, arrayIndex); } /// /// Removes the first occurrence of a specific object from the . /// /// /// true if was successfully removed from the ; otherwise, false. This method also returns false if is not found in the original . /// /// The object to remove from the .The is read-only. public bool Remove(KeyValuePair item) { return _data.Remove(item); } /// /// Gets the number of elements contained in the . /// /// /// The number of elements contained in the . /// public override int Count { get { return _data.Count; } } /// /// Gets a value indicating whether the is read-only. /// /// /// true if the is read-only; otherwise, false. /// public override bool IsReadOnly { get { return _data.IsReadOnly; } } /// /// Determines whether the contains an element with the specified key. /// /// /// true if the contains an element with the key; otherwise, false. /// /// The key to locate in the . is null. public override bool ContainsKey(Symbol key) { return _data.ContainsKey(key); } /// /// Gets all the items in the dictionary /// /// All the items in the dictionary public override IEnumerable> GetItems() => _data; /// /// Adds an element with the provided key and value to the . /// /// The object to use as the key of the element to add.The object to use as the value of the element to add. is null.An element with the same key already exists in the .The is read-only. public void Add(Symbol key, T value) { _data.Add(key, value); } /// /// Removes the element with the specified key from the . /// /// /// true if the element is successfully removed; otherwise, false. This method also returns false if was not found in the original . /// /// The key of the element to remove. is null.The is read-only. public override bool Remove(Symbol key) { return _data.Remove(key); } /// /// Gets the value associated with the specified key. /// /// /// true if the object that implements contains an element with the specified key; otherwise, false. /// /// The key whose value to get.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 parameter. This parameter is passed uninitialized. is null. public override bool TryGetValue(Symbol key, out T value) { return _data.TryGetValue(key, out value); } /// /// Gets or sets the element with the specified key. /// /// /// The element with the specified key. /// /// The key of the element to get or set. /// is null. /// The property is retrieved and is not found. /// The property is set and the is read-only. public override T this[Symbol symbol] { get { T data; if (TryGetValue(symbol, out data)) { return data; } CheckForImplicitlyCreatedSymbol(symbol); 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}\")"); } set { _data[symbol] = value; } } /// /// Gets an containing the keys of the . /// /// /// An containing the keys of the object that implements . /// public ICollection Keys { get { return _data.Keys; } } /// /// Gets an containing the values in the . /// /// /// An containing the values in the object that implements . /// public ICollection Values { get { return _data.Values; } } /// /// Gets an containing the Symbol objects of the . /// /// /// An containing the Symbol objects of the object that implements . /// protected override IEnumerable GetKeys => Keys; /// /// Gets an containing the values in the . /// /// /// An containing the values in the object that implements . /// protected override IEnumerable GetValues => Values; /// /// Gets the value associated with the specified key. /// /// The key whose value to get. /// /// The value associated with the specified key, if the key is found; otherwise, the default value for the type of the parameter. /// public virtual T GetValue(Symbol key) { T value; TryGetValue(key, out value); return value; } } /// /// Provides extension methods for the DataDictionary class /// public static class DataDictionaryExtensions { /// /// Provides a convenience method for adding a base data instance to our data dictionary /// public static void Add(this DataDictionary dictionary, T data) where T : BaseData { dictionary.Add(data.Symbol, data); } } }