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

Indicator style

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

Create a buffer list. Add each series separately as its values arrive; either one may run ahead, and a result is added once both hold the same timestamp:

csharp
CorrelationList correlationList = new(lookbackPeriods);

// call from each series' message handler
void OnAReceived(IReusable value) => correlationList.AddSource(value);
void OnBReceived(IReusable value) => correlationList.AddOther(value);

// the list itself holds the results; fills as the handlers run
IReadOnlyList<CorrResult> results = correlationList;

A timestamp that only one series holds produces no result, where the Series method throws. Each series must arrive in strictly increasing order: an older or equal timestamp on a side throws an InvalidOperationException. Each side stages at most MaxListSize values, so a side that falls further behind than that loses the pairs the leading side has dropped.

See Buffer lists for the full usage guide.

Subscribe to a BarHub for each series and add bars as they arrive:

csharp
BarHub hubA = new();
BarHub hubB = new();
CorrelationHub observer = hubA.ToCorrelationHub(hubB, lookbackPeriods);

// call from each series' message handler
void OnAReceived(IBar bar) => hubA.Add(bar);
void OnBReceived(IBar bar) => hubB.Add(bar);

// results fill as the handlers run
IReadOnlyList<CorrResult> results = observer.Results;

A result is added once both providers hold the same timestamp. A value that arrives late, or is revised or removed, on either side is handled by the usual rebuild. Timestamps match exactly, and the lookback window spans N matched timestamps. Drive both providers from one thread at a time.

See Stream hubs for the full usage guide.

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.
csharp
CorrelationList : IReadOnlyList<CorrResult>
  • The list itself holds the results, one for each timestamp both series hold, in timestamp order.
  • It keeps up to 100,000 results by default and drops the oldest beyond that; set MaxListSize to keep fewer.
  • The first N-1 periods will have null values since there's not enough data to calculate.
csharp
IReadOnlyList<CorrResult> results = observer.Results;
  • observer.Results holds the results, one for each timestamp both providers hold, in timestamp order.
  • Results update as bars arrive, including bars that arrive late or are revised.
  • The cache keeps up to 100,000 results by default; set maxCacheSize on the BarHub to keep fewer.
  • 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

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.

Add each series from the results of other chain-enabled buffer lists with AddSource and AddOther. When a new result appears in this list, add it to the next chain-enabled buffer list yourself.

See Chaining buffer lists for more.

Subscribe other chain-enabled hubs to it to process Correlation further.

csharp
// example
SlopeHub observer = hubA
    .ToCorrelationHub(hubB, ..)
    .ToSlopeHub(..);

See Chaining stream hubs for more.

Utilities ​

Call these on the list itself, as in correlationList.Find(lookupDate). .Condense() returns a new list and leaves correlationList unchanged.

Warmup removal is left out. Once the list is full it drops its oldest results, so trimming by position would remove valid values.

Call these on observer.Results, as in observer.Results.Find(lookupDate). .Condense() returns a new list and leaves the hub's results unchanged.

Warmup removal is left out. Once the cache is full the hub drops its oldest results, so trimming by position would remove valid values.

See Utilities and helpers for more information.