Correlation coefficient
Created by Karl Pearson, the Correlation coefficient depicts the linear statistical correlation between two price bar histories; includes R-squared (R²) / coefficient of determination, variance, and covariance. [Discuss] 💬
// C# usage syntax
IReadOnlyList<CorrResult> results =
barsA.ToCorrelation(barsB, lookbackPeriods);Parameters
| param | type | description |
|---|---|---|
barsA | IReadOnlyList<TBar> | Historical price bars (A) must have at least the same matching date elements of barsB. |
barsB | IReadOnlyList<TBar> | Historical price bars (B) must have at least the same matching date elements of barsA. |
lookbackPeriods | int | Number of periods (N) in the lookback period. Must be greater than 0 to calculate; however we suggest a larger period for statistically appropriate sample size. |
Historical price bars requirements
You must have at least N periods for both versions of bars to cover the warmup periods. More than the minimum is typically specified.
barsA and barsB must have consistent frequency (day, hour, minute, etc). Mismatch histories will throw InvalidBarsException. See the Guide for more information.
Response
IReadOnlyList<CorrResult>- This method returns a time series of all available indicator values for the
barsprovided. - It always returns the same number of elements as there are in the historical price bars.
- It does not return a single incremental indicator value.
- The first
N-1periods will havenullvalues since there's not enough data to calculate.
CorrResult
| property | type | description |
|---|---|---|
Timestamp | DateTime | Date from evaluated TBar |
VarianceA | double | Variance of A |
VarianceB | double | Variance of B |
Covariance | double | Covariance of A+B |
Correlation | double | Correlation R |
RSquared | double | R-squared (R²), aka Coefficient of determination |
Utilities
See Utilities and helpers for more information.
Chaining
This indicator may be generated from any chain-enabled indicator or method.
// example
var results = bars
.Use(CandlePart.HL2)
.ToCorrelation(barsMarket.Use(CandlePart.HL2),20);🚩
Both barsA and barsB arguments must contain the same number of elements and be the results of a chainable indicator or .Use() method.
Results can be further processed on Correlation with additional chain-enabled indicators.
// example
var results = bars
.ToCorrelation(..)
.ToSlope(..);See Chaining indicators for more.
Streaming
Streaming is not supported for this indicator. This indicator requires a second synchronized bar series, which cannot be expressed in the single-series streaming model. Use the Series (batch) implementation with periodic recalculation instead.