Docs

ta.regress

Regress one series on another to get intercept, slope, fit and the current residual.

itm
ta.regress(y, x, length, min=length)

Ordinary least squares of y on x over the window: [intercept, slope, r-squared, this bar's residual]. beta = cov(y, x)/var(x) and is the same number ta.beta reports. alpha = mean(y) - beta*mean(x). r2 is the square of ta.correlation over the same window. The residual is y - (alpha + beta*x) on this bar, which is the tradeable spread rather than a diagnostic. It is the two-series twin of ta.linreg, which regresses one series on bar position.

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
ySeriesnumber--The dependent series.
xSeriesnumber--The explanatory series.
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.

Outputs

NameType
alphanumber
betanumber
r2number
residualnumber

The outputs come back in this order: [alpha, beta, r2, residual] = ta.regress(…).

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. All four outputs are na when x has zero variance over the window. r2 alone is na when y has zero variance, because it is then 0/0. The residual is na on a bar where either series is na.

Example

itm
[alpha, beta, r2, residual] = ta.regress(ta.ret(close), ta.ret(open), 100)
itm
spy = series(symbol="SPY")
[alpha, beta, r2, residual] = ta.regress(ta.ret(close), ta.ret(spy.close), 100)
plot(beta, "Slope")