17 using System.Collections;
18 using System.Collections.Generic;
38 public static IEnumerable<TradeBars>
TradeBars(
this IEnumerable<Slice> slices)
40 return slices.Where(x => x.Bars.Count > 0).Select(x => x.Bars);
48 public static IEnumerable<Ticks>
Ticks(
this IEnumerable<Slice> slices)
50 return slices.Where(x => x.Ticks.Count > 0).Select(x => x.Ticks);
58 public static IEnumerable<DataDictionary<BaseDataCollection>>
GetUniverseData(
this IEnumerable<Slice> slices)
60 return slices.SelectMany(x => x.AllData).Select(x =>
74 public static IEnumerable<dynamic>
Get(
this IEnumerable<Slice> slices, Type type,
Symbol symbol =
null)
76 var result = slices.Select(x => x.Get(type));
83 return result.Where(x => x.ContainsKey(symbol)).Select(x => x[symbol]);
93 public static IEnumerable<TradeBar>
Get(
this IEnumerable<Slice> slices,
Symbol symbol)
95 return slices.TradeBars().Where(x => x.ContainsKey(symbol)).Select(x => x[symbol]);
109 return dataDictionaries.Where(x => x.ContainsKey(symbol)).Select(x => x[symbol]);
122 Func<T, decimal> selector;
123 if (typeof (
DynamicData).IsAssignableFrom(typeof (T)))
128 return (decimal) dyn.GetProperty(field);
131 else if (typeof (T) == typeof (List<Tick>))
136 selector = ticks => dataSelector(((List<Tick>) (
object) ticks).Last());
143 foreach (var dataDictionary
in dataDictionaries)
146 if (dataDictionary.TryGetValue(symbol, out item))
148 yield
return selector(item);
159 public static IEnumerable<DataDictionary<T>>
Get<T>(
this IEnumerable<Slice> slices)
162 return slices.Select(x => x.Get<T>()).Where(x => x.Count > 0);
172 public static IEnumerable<T>
Get<T>(
this IEnumerable<Slice> slices,
Symbol symbol)
175 return slices.Select(x => x.Get<T>()).Where(x => x.ContainsKey(symbol)).Select(x => x[symbol]);
186 public static IEnumerable<decimal>
Get(
this IEnumerable<Slice> slices,
Symbol symbol, Func<BaseData, decimal> field)
188 foreach (var slice
in slices)
191 if (slice.TryGetValue(symbol, out item))
193 if (item is List<Tick>) yield
return field(item.Last());
194 else yield
return field(item);
212 if (typeData.ContainsKey(symbol))
214 data = typeData[symbol];
232 var typeData = slice.
Get(type);
233 if (typeData.ContainsKey(symbol))
235 data = typeData[symbol];
249 return decimals.Select(x => (
double) x).ToArray();
258 public static void PushThroughConsolidators(
this IEnumerable<Slice> slices, Dictionary<Symbol, IDataConsolidator> consolidatorsBySymbol)
263 consolidatorsBySymbol.TryGetValue(symbol, out consolidator);
276 slices.PushThrough(data => consolidatorsProvider(data?.
Symbol)?.Update(data));
286 public static void PushThrough(
this IEnumerable<Slice> slices, Action<BaseData> handler, Type dataType =
null)
288 if (dataType !=
null)
290 Func<Slice, IEnumerable<BaseData>> dataSelector =
default;
293 dataSelector = slice => slice.QuoteBars.Values;
295 else if (dataType == typeof(
Tick))
297 dataSelector = slice => slice.Ticks.Values.Select(x => x.Last());
299 else if (dataType == typeof(
TradeBar))
301 dataSelector = slice => slice.Bars.Values;
305 dataSelector = slice => slice.Get(dataType).Values;
308 foreach (var slice
in slices)
310 foreach (
BaseData baseData
in dataSelector(slice))
318 foreach (var slice
in slices)
320 foreach (var symbol
in slice.Keys)
323 if (!slice.TryGetValue(symbol, out value))
328 var list = value as IList;
329 var data = (
BaseData)(list !=
null ? list[list.Count - 1] : value);