Docs

ta.linreg

Fit a straight line to the last n bars and read its value at the current bar.

itm
ta.linreg(source, length, offset)

Linear regression curve: the ordinary least-squares line fitted to the last length observations against bar position 0..length-1, evaluated offset bars back from the current bar: intercept + slope*(length - 1 - offset). offset = 0 is the line's endpoint, which is what a linear-regression curve plots.

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
sourceSeriesnumber--The series to read.
lengthConstantnumber-1 to 10000Window in observed bars.
offsetConstantnumber00 to 10000Bars back along the fitted line. 0 is its value at the current bar.

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 length consecutive non-na observations are in the window. An na resets the accumulation.

Example

itm
ta.linreg(close, 20, 0)
itm
plot(ta.linreg(close, 20, 0), "Regression 20")