Docs

ta.correlation

Measure how closely two series move together, on a scale from -1 to 1.

itm
ta.correlation(a, b, length, min=length)

Rolling Pearson correlation of a and b over the window, in [-1, 1]. Moments are population moments over the valid pairs.

Parameters

A series parameter takes a value on every bar. A constant parameter must be known before the first bar, such as a number or an input.

NameKindTypeDefaultRangeDescription
aSeriesnumber--The series to read.
bSeriesnumber--The series to read.
lengthConstantnumber-1 to 10000Window in observed bars.
minConstantnumber-1 to 10000Valid pairs the window must hold before it reports (an option, written min=40). Pairs with an na on either side are skipped. Absent = length: the window must be full and gap-free, which is the reset rule.

Output

NameType
valuenumber

Warm-up and missing values

Warm-up
length bars. Names in the warm-up are this operator’s parameters.
Missing values
Returns na until the last length bars hold at least min pairs where both series are present. Pairs with an na on either side are skipped. With min absent (= length) that is length consecutive valid pairs, so an na in either series resets the window. na when either series has zero variance over the window.

Example

itm
ta.correlation(ta.ret(close), ta.ret(spy.close), 50)
itm
spy = series(symbol="SPY")
plot(ta.correlation(ta.ret(close), ta.ret(spy.close), 50), "Correlation 50")