Returns the Pearson correlation coefficient between two series over the last length bars, from -1 to 1.
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
ta.correlation(source1: series int/float, source2: series int/float, length: series int) -> series floatArguments
| Name | Type | Default | Description |
|---|---|---|---|
| source1 | series int/ | required | The first series of the pair. Its values are compared with source2 over length bars. |
| source2 | series int/ | required | The second series of the pair. Its values are compared with source1 over length bars. |
| length | series int | required | Number 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.changeof each series to compare their moves.
Example
//@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