Exemplo n.º 1
0
        private void sampleRawDataThenAddToValues(Dictionary <string, object> rawData, int dayNumber, int binSize_seconds)
        {
            var dfd = new OneDaysData();

            dfd.BinSize = binSize_seconds;

            foreach (var kvp in rawData)
            {
                var symbol  = kvp.Key;
                var dataRaw = (List <List <object> >)kvp.Value;

                // parse into something more manageable.
                var dates  = new List <DateTime>();
                var values = new List <double>();
                for (int i = 0; i < dataRaw.Count; ++i)
                {
                    DateTime dateVal;
                    if (dataRaw[i][0] is string)
                    {
                        var dateStr = (string)dataRaw[i][0];
                        dateVal = DateTime.Parse(dateStr, null, System.Globalization.DateTimeStyles.RoundtripKind);
                    }
                    else if (dataRaw[i][0] is DateTime)
                    {
                        dateVal = (DateTime)dataRaw[i][0];
                    }
                    else
                    {
                        continue;
                    }

                    double valu = double.NaN;
                    if (dataRaw[i][1] is double)
                    {
                        valu = (double)dataRaw[i][1];
                    }
                    else if (dataRaw[i][1] is long)
                    {
                        valu = (double)(long)dataRaw[i][1];
                    }
                    // else NaN;

                    dates.Add(dateVal);
                    values.Add(valu);
                }

                // interporlate in to equi-spaced bins.
                int currentRawIndex = 0;
                var sampled         = new List <double>();
                var dayStart        = this.calculateDayStart(dayNumber);
                for (int cTime = 0; cTime < 60 * 60 * 24; cTime += binSize_seconds)
                {
                    while (currentRawIndex + 1 < dates.Count &&
                           dates[currentRawIndex + 1] < dayStart + TimeSpan.FromSeconds(cTime))
                    {
                        currentRawIndex += 1;
                    }

                    if (dates[currentRawIndex] > dayStart + TimeSpan.FromSeconds(cTime))
                    {
                        sampled.Add(_values.ContainsKey(dayNumber - 1)
                            ? _values[dayNumber - 1].ValueAtTime(symbol, 60 * 60 * 24 - 1)
                            : double.NaN);
                    }
                    else if (dates[dates.Count - 1] + TimeSpan.FromSeconds(binSize_seconds) < dayStart + TimeSpan.FromSeconds(cTime))
                    {
                        sampled.Add(double.NaN);
                    }
                    else
                    {
                        sampled.Add(values[currentRawIndex]);
                    }
                }

                dfd.Values.Add(symbol, sampled.ToArray());
            }

            if (_values.ContainsKey(dayNumber))
            {
                Console.WriteLine("replaceing data for day #: " + dayNumber);
                _values[dayNumber] = dfd;
            }
            else
            {
                Console.WriteLine("adding data for day #: " + dayNumber);
                _values.Add(dayNumber, dfd);
            }
        }
Exemplo n.º 2
0
        private void sampleRawDataThenAddToValues(Dictionary<string, object> rawData, int dayNumber, int binSize_seconds)
        {
            var dfd = new OneDaysData();
            dfd.BinSize = binSize_seconds;

            foreach (var kvp in rawData)
            {
                var symbol = kvp.Key;
                var dataRaw = (List<List<object>>)kvp.Value;

                // parse into something more manageable.
                var dates = new List<DateTime>();
                var values = new List<double>();
                for (int i = 0; i < dataRaw.Count; ++i)
                {
                    DateTime dateVal;
                    if (dataRaw[i][0] is string)
                    {
                        var dateStr = (string) dataRaw[i][0];
                        dateVal = DateTime.Parse(dateStr, null, System.Globalization.DateTimeStyles.RoundtripKind);
                    }
                    else if (dataRaw[i][0] is DateTime)
                    {
                        dateVal = (DateTime) dataRaw[i][0];
                    }
                    else
                    {
                        continue;
                    }

                    double valu = double.NaN;
                    if (dataRaw[i][1] is double) {
                        valu = (double)dataRaw[i][1];
                    }
                    else if (dataRaw[i][1] is long) {
                        valu = (double)(long)dataRaw[i][1];
                    }
                    // else NaN;

                    dates.Add(dateVal);
                    values.Add(valu);
                }

                // interporlate in to equi-spaced bins.
                int currentRawIndex = 0;
                var sampled = new List<double>();
                var dayStart = this.calculateDayStart(dayNumber);
                for (int cTime = 0; cTime<60*60*24; cTime += binSize_seconds)
                {
                    while (   currentRawIndex + 1 < dates.Count
                           && dates[currentRawIndex + 1] < dayStart + TimeSpan.FromSeconds(cTime))
                    {
                        currentRawIndex += 1;
                    }

                    if (dates[currentRawIndex] > dayStart + TimeSpan.FromSeconds(cTime))
                    {
                        sampled.Add(_values.ContainsKey(dayNumber - 1)
                            ? _values[dayNumber - 1].ValueAtTime(symbol, 60*60*24 - 1)
                            : double.NaN);
                    }
                    else if (dates[dates.Count-1] + TimeSpan.FromSeconds(binSize_seconds) < dayStart + TimeSpan.FromSeconds(cTime))
                    {
                        sampled.Add(double.NaN);
                    }
                    else
                    {
                        sampled.Add(values[currentRawIndex]);
                    }
                }

                dfd.Values.Add(symbol, sampled.ToArray());
            }

            if (_values.ContainsKey(dayNumber))
            {
                Console.WriteLine("replaceing data for day #: " + dayNumber);
                _values[dayNumber] = dfd;
            }
            else
            {
                Console.WriteLine("adding data for day #: " + dayNumber);
                _values.Add(dayNumber, dfd);
            }
        }