16 using MathNet.Numerics.Statistics;
17 using MathNet.Numerics.Distributions;
30 private readonly
double _confidenceLevel;
58 public ValueAtRisk(
string name,
int period,
double confidenceLevel)
63 throw new ArgumentException($
"Period parameter for ValueAtRisk indicator must be greater than 2 but was {period}");
67 _confidenceLevel = confidenceLevel;
79 : this($
"VaR({period}, {confidenceLevel})", period, confidenceLevel)
91 _rateOfChange.Update(input);
92 _returns.Add((
double)_rateOfChange.Current.Value);
94 if (_returns.Count < 2)
99 var mean = _returns.Mean();
100 var standardDeviation = _returns.StandardDeviation();
101 return (decimal)Normal.InvCDF(mean, standardDeviation, 1 - _confidenceLevel);
109 _rateOfChange.Reset();