/// <summary> /// Gets the data of the specified symbol and type. /// </summary> /// <remarks>Supports both C# and Python use cases</remarks> protected static dynamic GetImpl(Type type, Slice instance) { Lazy <object> dictionary; if (!instance._dataByType.TryGetValue(type, out dictionary)) { if (type == typeof(Tick)) { dictionary = new Lazy <object>(() => { var dataDictionaryCache = GenericDataDictionary.Get(type); var dic = Activator.CreateInstance(dataDictionaryCache.GenericType); foreach (var data in instance._data.Value.Values.SelectMany <dynamic, dynamic>(x => x.GetData()).Where(o => o != null && (Type)o.GetType() == type)) { dataDictionaryCache.MethodInfo.Invoke(dic, new[] { data.Symbol, data }); } return(dic); } ); } else if (type == typeof(TradeBar)) { dictionary = new Lazy <object>(() => new DataDictionary <TradeBar>( instance._data.Value.Values.Where(x => x.TradeBar != null).Select(x => x.TradeBar), x => x.Symbol)); } else if (type == typeof(QuoteBar)) { dictionary = new Lazy <object>(() => new DataDictionary <QuoteBar>( instance._data.Value.Values.Where(x => x.QuoteBar != null).Select(x => x.QuoteBar), x => x.Symbol)); } else { dictionary = new Lazy <object>(() => { var dataDictionaryCache = GenericDataDictionary.Get(type); var dic = Activator.CreateInstance(dataDictionaryCache.GenericType); foreach (var data in instance._data.Value.Values.Select(x => x.GetData()).Where(o => o != null && (Type)o.GetType() == type)) { dataDictionaryCache.MethodInfo.Invoke(dic, new[] { data.Symbol, data }); } return(dic); } ); } instance._dataByType[type] = dictionary; } return(dictionary.Value); }
/// <summary> /// Gets the data of the specified type. /// </summary> /// <remarks>Supports both C# and Python use cases</remarks> protected static dynamic GetImpl(Type type, Slice instance) { if (instance._dataByType == null) { // for performance we only really create this collection if someone used it instance._dataByType = new Dictionary <Type, object>(1); } object dictionary; if (!instance._dataByType.TryGetValue(type, out dictionary)) { var requestedOpenInterest = type == typeof(OpenInterest); if (type == typeof(Tick) || requestedOpenInterest) { var dataDictionaryCache = GenericDataDictionary.Get(type); dictionary = Activator.CreateInstance(dataDictionaryCache.GenericType); foreach (var data in instance.Ticks) { var symbol = data.Key; // preserving existing behavior we will return the last data point, users expect a 'DataDictionary<Tick> : IDictionary<Symbol, Tick>'. // openInterest is stored with the Ticks collection var lastDataPoint = data.Value.LastOrDefault(tick => requestedOpenInterest && tick.TickType == TickType.OpenInterest || !requestedOpenInterest && tick.TickType != TickType.OpenInterest); if (lastDataPoint == null) { continue; } dataDictionaryCache.MethodInfo.Invoke(dictionary, new object[] { symbol, lastDataPoint }); } } else if (type == typeof(TradeBar)) { dictionary = instance.Bars; } else if (type == typeof(QuoteBar)) { dictionary = instance.QuoteBars; } else if (type == typeof(Delisting)) { dictionary = instance.Delistings; } else if (type == typeof(Split)) { dictionary = instance.Splits; } else if (type == typeof(OptionChain)) { dictionary = instance.OptionChains; } else if (type == typeof(FuturesChain)) { dictionary = instance.FuturesChains; } else if (type == typeof(Dividend)) { dictionary = instance.Dividends; } else if (type == typeof(SymbolChangedEvent)) { dictionary = instance.SymbolChangedEvents; } else { var dataDictionaryCache = GenericDataDictionary.Get(type); dictionary = Activator.CreateInstance(dataDictionaryCache.GenericType); foreach (var data in instance._data.Value.Values.Select(x => x.Custom).Where(o => o != null && o.GetType() == type)) { dataDictionaryCache.MethodInfo.Invoke(dictionary, new object[] { data.Symbol, data }); } } instance._dataByType[type] = dictionary; } return(dictionary); }
/// <summary> /// Gets the data of the specified type. /// </summary> /// <remarks>Supports both C# and Python use cases</remarks> protected static dynamic GetImpl(Type type, Slice instance) { object dictionary; if (!instance._dataByType.TryGetValue(type, out dictionary)) { if (type == typeof(Tick)) { var dataDictionaryCache = GenericDataDictionary.Get(type); dictionary = Activator.CreateInstance(dataDictionaryCache.GenericType); foreach (var data in instance.Ticks) { var symbol = data.Key; var listOfTicks = data.Value; // preserving existing behavior we will return the last data point, users expect a 'DataDictionary<Tick> : IDictionary<Symbol, Tick>' var lastDataPoint = listOfTicks[listOfTicks.Count - 1]; dataDictionaryCache.MethodInfo.Invoke(dictionary, new object[] { symbol, lastDataPoint }); } } else if (type == typeof(TradeBar)) { dictionary = instance.Bars; } else if (type == typeof(QuoteBar)) { dictionary = instance.QuoteBars; } else if (type == typeof(Delisting)) { dictionary = instance.Delistings; } else if (type == typeof(Split)) { dictionary = instance.Splits; } else if (type == typeof(OptionChain)) { dictionary = instance.OptionChains; } else if (type == typeof(FuturesChain)) { dictionary = instance.FuturesChains; } else if (type == typeof(Dividend)) { dictionary = instance.Dividends; } else if (type == typeof(SymbolChangedEvent)) { dictionary = instance.SymbolChangedEvents; } else { var dataDictionaryCache = GenericDataDictionary.Get(type); dictionary = Activator.CreateInstance(dataDictionaryCache.GenericType); foreach (var data in instance._data.Value.Values.Select(x => x.Custom).Where(o => o != null && o.GetType() == type)) { dataDictionaryCache.MethodInfo.Invoke(dictionary, new object[] { data.Symbol, data }); } } instance._dataByType[type] = dictionary; } return(dictionary); }