StatisticalPine Script v6

ta.correlation()Correlation Coefficient

Describes the degree to which two series tend to deviate from their ta.sma values.

Syntax

Syntax
ta.correlation(source1, source2, length) → series float

Arguments

ParameterTypeDescription
source1series int/floatFirst series.
source2series int/floatSecond series.
lengthseries intNumber of bars in the rolling window.

Returns

Correlation coefficient.

Remarks

na values in the source series are ignored.

Code Examples

Pine Script v6 Example
//@version=6
indicator("ta.correlation")
plot(ta.correlation(close, open, 20), title="Close vs Open corr")

Trading Applications

Measure correlation between two assets

Build pairs trading strategies

Identify diversification opportunities

Monitor inter-market relationships

Related Functions

Frequently Asked Questions

It returns the Pearson correlation coefficient between two series over the last length bars, measuring linear co-movement from -1 (inverse) to +1 (same direction).

Values near 1 mean both series moved together; near -1 means opposite moves; near 0 means little linear relationship. Use longer lengths for smoother readings.

When two related symbols diverge but historical correlation is high, traders sometimes bet on mean reversion of the spread; correlation helps quantify whether the relationship is stable enough for that idea.

Generate ta.correlation() Code with AI

Skip the manual coding. Use Pineify's AI Coding Agent to generate Pine Script code using ta.correlation() and other built-in functions instantly.