///Constructor for datetime-value arguements: public ChartPoint(DateTime time, decimal value) { x = Convert.ToInt64(Time.DateTimeToUnixTimeStamp(time.ToUniversalTime())); y = value; }
/// <summary> /// Samples the given series /// </summary> /// <param name="series">The series to be sampled</param> /// <param name="start">The date to start sampling, if before start of data then start of data will be used</param> /// <param name="stop">The date to stop sampling, if after stop of data, then stop of data will be used</param> /// <param name="truncateValues">True will truncate values to integers</param> /// <returns>The sampled series</returns> public Series Sample(Series series, DateTime start, DateTime stop, bool truncateValues = false) { var sampled = new Series(series.Name, series.SeriesType, series.Index, series.Unit); // chart point times are always in universal, so force it here as well double nextSample = Time.DateTimeToUnixTimeStamp(start.ToUniversalTime()); double unixStopDate = Time.DateTimeToUnixTimeStamp(stop.ToUniversalTime()); // we can't sample a single point and it doesn't make sense to sample scatter plots // in this case just copy the raw data if (series.Values.Count < 2 || series.SeriesType == SeriesType.Scatter) { // we can minimally verify we're within the start/stop interval foreach (var point in series.Values) { if (point.x >= nextSample && point.x <= unixStopDate) { var samplePoint = point; if (truncateValues) { // let's not modify the original samplePoint = new ChartPoint(samplePoint) { y = Math.Truncate(samplePoint.y) }; } sampled.Values.Add(samplePoint); } } return(sampled); } var enumerator = series.Values.GetEnumerator(); // initialize current/previous enumerator.MoveNext(); ChartPoint previous = enumerator.Current; enumerator.MoveNext(); ChartPoint current = enumerator.Current; // make sure we don't start sampling before the data begins if (nextSample < previous.x) { nextSample = previous.x; } // make sure to advance into the requestd time frame before sampling while (current.x < nextSample && enumerator.MoveNext()) { previous = current; current = enumerator.Current; } do { // advance our current/previous if (nextSample > current.x) { if (enumerator.MoveNext()) { previous = current; current = enumerator.Current; } else { break; } } // iterate until we pass where we want our next point while (nextSample <= current.x && nextSample <= unixStopDate) { var value = Interpolate(previous, current, (long)nextSample); var point = new ChartPoint { x = (long)nextSample, y = value }; if (truncateValues) { point.y = Math.Truncate(point.y); } sampled.Values.Add(point); nextSample += _seconds; } // if we've passed our stop then we're finished sampling if (nextSample > unixStopDate) { break; } }while (true); enumerator.DisposeSafely(); return(sampled); }
//Bitcoin Handler: public void OnData(Bitcoin data) { Debug(Time.ToLongTimeString() + " >> ALGO >> OnData(BTC) >> BTC: " + data.Close); }