How it is calculated
A least squares line is fitted through the source over the last length bars, x running from 0 for the oldest bar, and the value is that line at the newest bar. The first value comes once length bars are in, as TA-Lib's LINEARREG. A window of equal prices gives a flat line at that price.
Reference: TA-Lib LINEARREG
How to read it
It follows price more closely than an SMA of the same length because it carries the fitted trend to the current bar, and overshoots at turns. A rising line means the fitted trend points up; the gap between price and line shows how far price strays from it.
In a strategy
A smooth trend line, ctx.ind("linreg", length=20), or the close minus it as a stretch measure: fade large gaps in a range, follow the line's direction in a trend.
Inputs
| Input | Type | Default | Range or choices |
|---|---|---|---|
Lengthlength | Whole number | 20 | min 2, max 5000 |
Sourcesource | Price | "close" | close, open, high, low, hl2, hlc3, ohlc4, hlcc4 |
What it draws
| Line | Drawn as |
|---|---|
| LinReg | Line |
Used in
No strategy template reads it yet.