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
///
public class DataDictionary : 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
///
[Obsolete("The DataDictionary Time property is now obsolete. All algorithms should use algorithm.Time instead.")]
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 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 int Count
{
get { return _data.Count; }
}
///
/// Gets a value indicating whether the is read-only.
///
///
/// true if the is read-only; otherwise, false.
///
public 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 bool ContainsKey(Symbol key)
{
return _data.ContainsKey(key);
}
///
/// 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 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 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 T this[Symbol symbol]
{
get
{
T data;
if (TryGetValue(symbol, out data))
{
return data;
}
throw new KeyNotFoundException(string.Format("'{0}' wasn't found in the {1} 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(\"{0}\")", symbol, GetType().GetBetterTypeName()));
}
set
{
_data[symbol] = 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 T this[string ticker]
{
get
{
Symbol symbol;
if (!SymbolCache.TryGetSymbol(ticker, out symbol))
{
throw new KeyNotFoundException(string.Format("'{0}' wasn't found in the {1} 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(\"{0}\")", ticker, GetType().GetBetterTypeName()));
}
return this[symbol];
}
set
{
Symbol symbol;
if (!SymbolCache.TryGetSymbol(ticker, out symbol))
{
throw new KeyNotFoundException(string.Format("'{0}' wasn't found in the {1} 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(\"{0}\")", ticker, GetType().GetBetterTypeName()));
}
this[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 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);
}
}
}