Moving AveragesPine Script v6

ta.linreg()Linear Regression

Computes the value of the linear regression line fitted to the source over the last length bars at a given offset. It summarizes the best-fit straight-line trend through the window and can project forward or backward along that line.

TradingView language reference

Syntax

Syntax
ta.linreg(source, length, offset) → series float

Arguments

ParameterTypeDescription
sourceseries int/floatSeries of values to process.
lengthseries intNumber of bars used for the regression window.
offsetsimple intRequired offset along the fitted line; use 0 for its current endpoint.

Returns

Linear regression curve value.

Remarks

na source values are included and produce an na result. The returned value is intercept + slope × (length - 1 - offset). Offset changes the evaluation point on the current fit; it does not replace the fit with an earlier bar’s regression.

Code Examples

Pine Script v6 Example
//@version=6
indicator("ta.linreg")
plot(ta.linreg(close, 20, 0))

Trading Applications

Fit a linear trend line to recent price data

Describe the fitted historical slope without treating extrapolation as a forecast

Identify overbought/oversold when price deviates from regression

Use as a dynamic trend channel center line

Related Functions

Frequently Asked Questions

ta.linreg() performs ordinary least-squares linear regression on the source over length bars and returns the regression line value at the specified offset from the current bar context.

Moving averages smooth by averaging or weighting past values. Linear regression fits a single straight line to the window and reads a point on that line—it encodes slope and level together rather than only a smoothed level.

The function evaluates intercept + slope × (length - 1 - offset). Zero reads the current endpoint; a positive offset reads an earlier point on that same current-window fit. It does not shift the plot location or provide a previously calculated regression value.

Generate ta.linreg() Code with AI

Draft Pine Script using ta.linreg() with Pineify's AI Coding Agent, then review the arguments and test the result on your chart. Usage depends on your plan and available credits.