49 private decimal _correlation;
54 private readonly decimal _period;
64 private readonly
Symbol _referenceSymbol;
69 private readonly
Symbol _targetSymbol;
96 throw new ArgumentException($
"Period parameter for Correlation indicator must be greater than 2 but was {period}");
102 _referenceSymbol = referenceSymbol;
103 _targetSymbol = targetSymbol;
105 _correlationType = correlationType;
121 : this($
"Correlation({period})", targetSymbol, referenceSymbol, period, correlationType)
138 var inputSymbol = input.
Symbol;
139 if (inputSymbol == _targetSymbol)
141 _targetDataPoints.Add((
double)input.
Value);
143 else if (inputSymbol == _referenceSymbol)
145 _referenceDataPoints.Add((
double)input.
Value);
149 throw new ArgumentException(
"The given symbol was not target or reference symbol");
151 ComputeCorrelation();
159 private void ComputeCorrelation()
161 if (_targetDataPoints.Count < _period || _referenceDataPoints.Count < _period)
166 var newCorrelation = 0d;
169 newCorrelation = MathNet.Numerics.Statistics.Correlation.Pearson(_targetDataPoints, _referenceDataPoints);
173 newCorrelation = MathNet.Numerics.Statistics.Correlation.Spearman(_targetDataPoints, _referenceDataPoints);
175 if (newCorrelation.IsNaNOrZero())
179 _correlation = Extensions.SafeDecimalCast(newCorrelation);
187 _targetDataPoints.Reset();
188 _referenceDataPoints.Reset();