Sidechair

ta.correlation()

Browse the reference

Returns the Pearson correlation coefficient between two series over the last length bars, from -1 to 1.

Try it in the editor

ta.correlation measures how closely source1 and source2 move together inside the window. 1 means they rise and fall in perfect step, -1 means they move in exact opposition, 0 means no linear relationship.

The coefficient starts once the window holds length bars of both series. If either series is constant across the window, the result is na.

Syntax

Pine Script
ta.correlation(source1: series int/float, source2: series int/float, length: series int) -> series float

Arguments

NameTypeDefaultDescription
source1series int/floatrequiredThe first series of the pair. Its values are compared with source2 over length bars.
source2series int/floatrequiredThe second series of the pair. Its values are compared with source1 over length bars.
lengthseries intrequiredNumber of bars in the window. A whole number of 2 or more.

Returns

series float: the correlation coefficient from -1 to 1, or na while the window is not full.

Remarks

  • Correlating raw prices can read high just because both trend. Correlate ta.change of each series to compare their moves.

Example

Pine ScriptClose against volumeOpen in editor
//@version=6
indicator("Close vs volume correlation")
c = ta.correlation(close, volume, 30)
plot(c, "Correlation", color = c >= 0 ? color.green : color.red, style = plot.style_columns)
hline(0)

See also

Runs on Sidechair