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.

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 intOffset along the regression line; default 0.

Returns

Linear regression curve value.

Remarks

na values in the source series are ignored.

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

Predict future price direction based on regression slope

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.

Offset selects which point on the fitted line you read—commonly 0 for the line at the current bar, or nonzero to shift along the line (e.g., mild projection or alignment with indicator conventions).

Generate ta.linreg() Code with AI

Skip the manual coding. Use Pineify's AI Coding Agent to generate Pine Script code using ta.linreg() and other built-in functions instantly.