Linear regression
All indicators

LibraryTrend

Indicator

Linear regression

The end point of the straight line that fits the last bars best, a moving average that follows the trend with little lag.

TrendOn the price2 inputsLinReg

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

InputTypeDefaultRange or choices
LengthlengthWhole number20min 2, max 5000
SourcesourcePrice"close"close, open, high, low, hl2, hlc3, ohlc4, hlcc4

What it draws

LineDrawn as
LinRegLine

Used in

No strategy template reads it yet.

OrionQuant