18 using MathNet.Numerics.Statistics;
21 using System.Collections.Generic;
34 private bool _needsUpdate;
35 private decimal _volatility;
36 private DateTime _lastUpdate;
37 private readonly TimeSpan _periodSpan;
38 private readonly
object _sync =
new object();
50 if (_window.Count < 2)
58 var mean = Math.Abs(_window.Mean().SafeDecimalCast());
62 var std = _window.StandardDeviation().SafeDecimalCast();
63 _volatility = std / mean;
80 if (periods < 2)
throw new ArgumentOutOfRangeException(nameof(periods),
"'periods' must be greater than or equal to 2.");
81 _periodSpan = periodSpan;
83 _lastUpdate = GetLastUpdateInitialValue(periodSpan, periods);
94 var timeSinceLastUpdate = data.
EndTime - _lastUpdate;
95 if (timeSinceLastUpdate >= _periodSpan && data.
Price > 0)
100 _window.Add((
double)data.
Price);
116 throw new InvalidOperationException(
117 "RelativeStandardDeviationVolatilityModel.GetHistoryRequirements(): " +
118 "SubscriptionDataConfigProvider was not set."
126 .GetSubscriptionDataConfigs(security.
Symbol)
127 .OrderBy(c => c.TickType)
133 configurations.GetHighestResolution(),
142 _needsUpdate =
false;
144 _lastUpdate = GetLastUpdateInitialValue(_periodSpan, _window.Size);
148 private static DateTime GetLastUpdateInitialValue(TimeSpan periodSpan,
int periods)
150 return DateTime.MinValue + TimeSpan.FromMilliseconds(periodSpan.TotalMilliseconds * periods);