ta.linreg
Fit a straight line to the last n bars and read its value at the current bar.
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.
| Name | Kind | Type | Default | Range | Description |
|---|---|---|---|---|---|
source | Series | number | - | - | The series to read. |
length | Constant | number | - | 1 to 10000 | Window in observed bars. |
offset | Constant | number | 0 | 0 to 10000 | Bars back along the fitted line. 0 is its value at the current bar. |
Output
| Name | Type |
|---|---|
value | number |
Warm-up and missing values
- Warm-up
lengthbars. Names in the warm-up are this operator’s parameters.- Missing values
- Returns na until
lengthconsecutive non-na observations are in the window. An na resets the accumulation.
Example
ta.linreg(close, 20, 0)plot(ta.linreg(close, 20, 0), "Regression 20")