Exemple #1
0
        private double[][] GetPriceVectors(IEnumerable <Slice> slices)
        {
            var symbols   = Securities.Select(x => x.Symbol).ToArray();
            var timeZones = Securities.ToDictionary(x => x.Symbol, y => y.Exchange.TimeZone);

            // Special case: daily data and securities from different timezone
            var isDailyAndMultipleTimeZone = _resolution == Resolution.Daily && timeZones.Values.Distinct().Count() > 1;

            var bars = new List <BaseData>();

            if (isDailyAndMultipleTimeZone)
            {
                bars.AddRange(slices
                              .GroupBy(x => x.Time.Date)
                              .Where(x => x.Sum(k => k.Count) == symbols.Length)
                              .SelectMany(x => x.SelectMany(y => y.Values)));
            }
            else
            {
                bars.AddRange(slices
                              .Where(x => x.Count == symbols.Length)
                              .SelectMany(x => x.Values));
            }

            return(bars
                   .GroupBy(x => x.Symbol)
                   .Select(x =>
            {
                var array = x.Select(b => Math.Log((double)b.Price)).ToArray();
                if (array.Length > 1)
                {
                    for (var i = array.Length - 1; i > 0; i--)
                    {
                        array[i] = array[i] - array[i - 1];
                    }
                    array[0] = array[1];
                    return array;
                }
                else
                {
                    return new double[0];
                }
            }).ToArray());
        }