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

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.