Linear regression
Least squares line fitted to the last N values, read at the newest bar.
Inputs
| Name | Type | Default | Range |
|---|---|---|---|
| source | source | close | |
| period | int | 14 | 2 to 5000 |
Outputs
| Name | Type | Required | Description |
|---|---|---|---|
| linreg | value | Optional | color theme.accent, style line, width 1 |
- Warm-up
- period - 1 bars (13 at the defaults)
- Repaints
- No
- Parity
- TA-Lib, within 1e-9
Formula
Fit a straight line by least squares to the last period values, with t = 0 at the oldest.
b = (period * sum(t*y) - sum(t) * sum(y)) / (period * sum(t^2) - sum(t)^2)
a = (sum(y) - b * sum(t)) / period
LinReg = a + b * (period - 1)