---
url: /indicators/correlation.md
description: >-
  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.
package: FacioQuo.Stock.Indicators
docs_version: v3
canonical: https://dotnet.stockindicators.dev/indicators/correlation
generated: 2026-09-30T04:41:06.055Z
commit: ae1f432eaa0b03ae9f39e032c69434b557efa4a2
---

# Correlation coefficient

Created by Karl Pearson, the [Correlation coefficient](https://en.wikipedia.org/wiki/Correlation_coefficient) depicts the linear statistical correlation between two price bar histories; includes R-squared (R²) / coefficient of determination, variance, and covariance. [\[Discuss\] 💬](https://github.com/facioquo/stock-indicators-dotnet/discussions/259 "Community discussion about this indicator")

```csharp
// C# usage syntax
IReadOnlyList<CorrResult> results =
  barsA.ToCorrelation(barsB, lookbackPeriods);
```

## Parameters

| param | type | description |
| ----- | ---- | ----------- |
| `barsA` | *`IReadOnlyList<TBar>`* | [Historical price bars](/guide/getting-started.md#historical-bars) (A) must have at least the same matching date elements of `barsB`. |
| `barsB` | *`IReadOnlyList<TBar>`* | [Historical price bars](/guide/getting-started.md#historical-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](/guide/getting-started.md#historical-bars) for more information.

## Response

```csharp
IReadOnlyList<CorrResult>
```

* This method returns a time series of all available indicator values for the `bars` 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`

| 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

* [.Condense()](/utilities/results.md#condense)
* [.Find(lookupDate)](/utilities/results.md#find-by-date)
* [.RemoveWarmupPeriods()](/utilities/results.md#remove-warmup-periods)
* [.RemoveWarmupPeriods(removePeriods)](/utilities/results.md#remove-warmup-periods)

See [Utilities and helpers](/utilities.md) 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);
```

> [!WARNING]
> **🚩**
>
> 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.

```csharp
// example
var results = bars
    .ToCorrelation(..)
    .ToSlope(..);
```

See [Chaining indicators](/guide/chaining.md) 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.
