18 using MathNet.Numerics.Statistics;
45 private readonly
Symbol _referenceSymbol;
50 private readonly
Symbol _targetSymbol;
65 private decimal _beta;
91 throw new ArgumentException($
"Period parameter for Beta indicator must be greater than 2 but was {period}");
95 _referenceSymbol = referenceSymbol;
96 _targetSymbol = targetSymbol;
114 : this($
"B({period})", targetSymbol, referenceSymbol, period)
128 : this(name, targetSymbol, referenceSymbol, period)
145 var inputSymbol = input.
Symbol;
146 if (inputSymbol == _targetSymbol)
148 _targetDataPoints.Add(input.
Close);
150 else if(inputSymbol == _referenceSymbol)
152 _referenceDataPoints.Add(input.
Close);
156 throw new ArgumentException(
"The given symbol was not target or reference symbol");
159 if (_targetDataPoints.Samples == _referenceDataPoints.Samples && _referenceDataPoints.Count > 1)
161 _targetReturns.Add(GetNewReturn(_targetDataPoints));
162 _referenceReturns.Add(GetNewReturn(_referenceDataPoints));
177 return (
double) ((rollingWindow[0].SafeDivision(rollingWindow[1]) - 1));
184 private void ComputeBeta()
186 var varianceComputed = _referenceReturns.Variance();
187 var covarianceComputed = _targetReturns.Covariance(_referenceReturns);
190 var variance = !varianceComputed.IsNaNOrZero() ? varianceComputed : 1;
191 var covariance = !covarianceComputed.IsNaNOrZero() ? covarianceComputed : 0;
192 _beta = (decimal) (covariance / variance);
200 _targetDataPoints.Reset();
201 _referenceDataPoints.Reset();
203 _targetReturns.Reset();
204 _referenceReturns.Reset();