TradingView indicators

Least squares moving average: regression endpoints

The least squares moving average (LSMA) fits a straight line to the latest N prices and, at offset zero, returns that line's newest endpoint. Every new bar produces a new fit. The result describes the observed window; it is not a forecast of the next price.

For prices 100, 102, 101, 105 and 107, the five-bar LSMA is 106.4. The fitted slope is 1.7 price units per bar. Reproduce both numbers below.

Calculate the endpoint of one window

Number the observations x = 0 to N − 1, oldest to newest, with y as the selected price source and N ≥ 2. Least squares chooses intercept a and slope b to minimize the sum of squared vertical errors between y and a + bx.

x̄ = (N − 1) / 2;  ȳ = sum(y) / N
b = sum((x − x̄) × (y − ȳ)) / sum((x − x̄)²)
a = ȳ − b × x̄
LSMA(offset) = a + b × (N − 1 − offset)

In the five-price example, x̄ = 2 and ȳ = 103. The slope numerator is 17 and its denominator is 10, so b = 1.7. Then a = 103 − 1.7 × 2 = 99.6, and the endpoint is 99.6 + 1.7 × 4 = 106.4. The latest price, 107, sits 0.6 above the fitted endpoint. SMA for the same window is 103.

This is a single rolling fitted-value series. A regression channel adds a separate band-width calculation, which this LSMA example does not use. TradingView's LSMA implementation uses ta.linreg; its documented regression formula specifies the offset term above.

Regression offset, historical value and plot offset

Changing the regression offset keeps the current window and fitted line, but evaluates it at another x coordinate. For a = 99.6, b = 1.7 and N = 5:

Regression offset values for the five-price example
Regression offsetEvaluation pointOutputMeaning
0x = 4106.4Newest fitted endpoint
+1x = 3104.7Earlier point on this same fit
−1x = 5108.1One-step extrapolation of this fit

Offset +1 is not the previous bar's LSMA: that earlier endpoint came from a different window. Offset −1 extends the current slope without reading a future observation. The extrapolated 108.1 is only what this line would reach if extended.

A plot offset instead shifts where an already calculated value is drawn. Moving 106.4 one bar to the right leaves the value at 106.4; it does not calculate 108.1 or validate a forecast. Keep the drawing unshifted when comparing signals with their decision bar.

What changes in trends, ranges and rolling windows?

These synthetic five-bar windows use offset zero and arbitrary price units. They illustrate arithmetic, not historical returns.

Five-bar LSMA and SMA calculations
WindowPrices, oldest firstFitted slopeLSMASMA
Steady rise100, 102, 104, 106, 1082.0108.0104.0
Uneven rise100, 102, 101, 105, 1071.7106.4103.0
Next rolling window102, 101, 105, 107, 1041.0105.8103.8
Alternating range100, 104, 100, 104, 1000.0101.6101.6
Range, one bar later104, 100, 104, 100, 1040.0102.4102.4
Rise then pause100, 102, 104, 106, 1061.6106.8103.6
  • On the exact straight-line rise, LSMA equals the latest price, 108. SMA is 104. This special case does not prove a general lag or performance advantage.
  • When the uneven-rise window drops 100 and adds 104, LSMA falls from 106.4 to 105.8. Its change is −0.6 even though the new fitted slope remains positive at +1.0.
  • In the alternating range, price crosses from below LSMA to above it on the next bar. Both windows have zero fitted slope, so the cross alone does not establish an upward trend.
  • After a rise then pause, LSMA reaches 106.8 although no price in the window exceeds 106. A fitted endpoint can overshoot the observed range.

Length controls which observations enter the fit. A longer window can retain an old trend after a reversal; a short one reacts to fewer observations and can change direction quickly. Compare lengths with source, timeframe and confirmation timing held constant. No length removes gaps or sideways whipsaws.

LSMA Pine Script with a defined slope condition

This v6 indicator plots the offset-zero endpoint. An optional orange line shows another regression offset. The Data Window separates fitted slope from the change between consecutive endpoints. Both calculations use the chart timeframe.

Pine Script v6
//@version=6
indicator("LSMA: endpoint and fitted slope", overlay=true)

length = input.int(25, "Length", minval=2, maxval=500)
src = input.source(close, "Source")
regressionOffset = input.int(0, "Regression offset", minval=-50, maxval=50)

endpoint = ta.linreg(src, length, 0)
fittedSlope = endpoint - ta.linreg(src, length, 1)
selectedValue = ta.linreg(src, length, regressionOffset)
endpointChange = endpoint - endpoint[1]
crossedAbove = ta.crossover(src, endpoint)
confirmedCross = barstate.isconfirmed and crossedAbove and fittedSlope > 0

plot(endpoint, "LSMA endpoint (offset 0)", color=color.blue, linewidth=2)
plot(regressionOffset != 0 ? selectedValue : na, "Selected regression offset", color=color.orange)
plot(fittedSlope, "Fitted slope (price per bar)", display=display.data_window)
plot(endpointChange, "Change between rolling endpoints", display=display.data_window)
plotshape(confirmedCross, title="Confirmed source cross with positive slope", style=shape.triangleup, location=location.belowbar, color=color.blue, size=size.tiny)
alertcondition(confirmedCross, "Confirmed LSMA cross", "Source closed above LSMA with positive fitted slope")

Copy it into the TradingView Pine Editor and add it to the chart. Compare the blue line with the built-in LSMA using the same source, length and offset 0. TradingView publishes 25 as its default length; the example uses it as a starting input.

The marker requires the source to cross above the zero-offset endpoint, a positive fitted slope and a confirmed bar. The crossover is calculated on every bar before the confirmation filter. Changing the orange line's offset does not change this rule. Create an alert from the named condition if needed.

The fitted slope is the difference between offsets 0 and 1 on the same window. It is not endpoint minus endpoint[1]. With length 2, the line passes through both observations and its endpoint equals the newest source, so a source/endpoint crossover is not a useful rule. Allow a full window of valid observations before evaluating any signal.

The lines can change on an open bar. The marker waits for bar-close confirmation, but that does not prevent revised market data or repainting inherited from an external source. This is an indicator, not a complete trading strategy: exits, sizing, fees and slippage still need definitions and separate testing.

Turn the LSMA rule into reviewable code

Pineify's Pine Script AI Coding Agent can generate or edit a script from explicit rules. Specify the source and length, which slope definition you mean, and when the condition is confirmed. A request for this example would be:

Use a 25-bar LSMA of close at regression offset 0. Mark a cross above the endpoint only after bar close and only when the current regression slope is positive. Keep every plot on its calculation bar. Show the fitted slope in the Data Window.

Review the generated formulas, run a syntax check and compare the chart with the built-in indicator. Code generation does not establish a profitable trading rule.

Explore the Pine Script AI Coding Agent

LSMA questions

Sources and related calculations

Primary documentation checked on October 17, 2026. Numerical examples are calculated illustrations.

Compare the SMA arithmetic mean, WMA linear weights and ALMA Gaussian weights. Browse the TradingView indicator guides for other calculations.

Educational information, not investment advice. Fitted trends and confirmed signals do not guarantee future returns.