17 using System.Collections.Generic;
19 using MathNet.Numerics.Statistics;
33 private bool _needsUpdate;
34 private decimal _volatility;
35 private DateTime _lastUpdate = DateTime.MinValue;
36 private decimal _lastPrice;
38 private TimeSpan _periodSpan;
39 private readonly
object _sync =
new object();
51 if (_window.
Count < 2)
59 var std = _window.StandardDeviation().SafeDecimalCast();
60 _volatility = std * (decimal)Math.Sqrt(252.0);
89 TimeSpan? updateFrequency =
null
94 throw new ArgumentOutOfRangeException(nameof(periods),
"'periods' must be greater than or equal to 2.");
98 _resolution = resolution;
99 _periodSpan = updateFrequency ?? resolution?.ToTimeSpan() ?? TimeSpan.FromDays(1);
121 TimeSpan? updateFrequency =
null
122 ) : this(PeriodsInResolution(resolution), resolution, updateFrequency)
134 var timeSinceLastUpdate = data.
EndTime - _lastUpdate;
135 if (timeSinceLastUpdate >= _periodSpan && data.
Price > 0)
139 if (_lastPrice > 0.0m)
142 _window.
Add((
double)(data.
Price / _lastPrice) - 1.0);
147 _lastPrice = data.
Price;
174 _needsUpdate =
false;
176 _lastUpdate = DateTime.MinValue;
181 private static int PeriodsInResolution(
Resolution resolution)