Skip to content

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] 💬

csharp
// C# usage syntax
IReadOnlyList<CorrResult> results =
  sourceA.ToCorrelation(sourceB, lookbackPeriods);

Parameters ​

paramdescription
sourceA
IReadOnlyList<IReusable>
First source series (A), such as an indicator's results or .Use() output. It must have at least the same matching date elements of sourceB.
sourceB
IReadOnlyList<IReusable>
Second source series (B), such as an indicator's results or .Use() output. It must have at least the same matching date elements of sourceA.
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 sourceA and sourceB to cover the warmup periods. More than the minimum is typically specified.

sourceA and sourceB must have consistent frequency (day, hour, minute, etc). Mismatch histories will throw InvalidBarsException. See the Guide for more information.

Response ​

csharp
IReadOnlyList<CorrResult>
  • This method returns a time series of all available indicator values for the sourceA and sourceB provided.
  • 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-1 periods will have null values since there's not enough data to calculate.

CorrResult ​

propertydescription
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.

csharp
// example
var results = bars
    .Use(CandlePart.HL2)
    .ToCorrelation(barsMarket.Use(CandlePart.HL2),20);

🚩 Matching inputs required

Both sourceA and sourceB 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.

csharp
// 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.