The endpoint of a rolling linear regression — a statistics-based average that fits a trend line to each window of bars.
The Least Squares Moving Average (LSMA), also called the linear regression moving average or endpoint moving average, is built from a genuine statistical procedure rather than a weighting scheme. For every bar, it fits a straight line through the last N prices using ordinary least squares — the method that minimizes the sum of squared vertical distances between the prices and the line — and plots the value of that fitted line at the current (most recent) bar. Repeating this on every bar produces a smooth curve that tracks trends with less lag than an SMA of the same length, because a fitted line's endpoint leans toward where the recent trend is heading within the window. It is important to be precise about what the plotted history means: each historical LSMA point is the endpoint of the regression as it stood on that bar, which is what you would have seen live — but the full regression line drawn across a window (as in a regression channel) refits and changes shape as new bars arrive. LSMA is a description of past prices, not a prediction. Educational material only, not financial advice; trading involves risk of loss.
For each bar, take the last N closes and index them by time (1, 2, …, N). Ordinary least squares finds the slope and intercept of the line y = a + b·x that minimizes the squared errors against those closes; the LSMA value is that line evaluated at x = N, the newest bar. Equivalently, the LSMA is a weighted moving average with fixed linear weights that go negative for the oldest bars — which is why it can respond faster than price-averaging alone would allow, and also why it can overshoot. In Pine Script this is exactly ta.linreg(source, length, 0): the third argument is the offset, where 0 means 'evaluate the fitted line at the current bar'. A crucial honesty point: the LSMA plotted on historical bars is trustworthy (each point used only data available at that time), but a regression channel or full regression line drawn over a window is refit on every new bar — the line you see over last month's data is not the line you would have seen live during last month. The endpoint series does not repaint; the drawn line object does. Backtests must use the endpoint series, never conditions on the redrawn line.
Common lengths are 25 (a widespread platform default), with 14 for faster reads and 50–100 for slower ones, applied to the close. The offset parameter is normally 0 (evaluate at the current bar); positive offsets shift the evaluation point back in time, and negative offsets — projecting the fitted line into the future — should be avoided, since they turn a description into an unfounded extrapolation. As always, shorter windows fit noise, longer windows fit slower structure; there is no correct length.
//@version=6
indicator("Least Squares MA (LSMA)", overlay = true)
length = input.int(25, "Length", minval = 2)
src = input.source(close, "Source")
offset = input.int(0, "Offset", minval = 0, tooltip = "0 = evaluate the fitted line at the current bar")
// Endpoint of the rolling least-squares regression
lsma = ta.linreg(src, length, offset)
// Slope of the fit: difference between endpoint and the point one bar back
slope = lsma - ta.linreg(src, length, offset + 1)
plot(lsma, "LSMA", color = slope >= 0 ? color.teal : color.maroon, linewidth = 2)
plot(ta.sma(src, length), "SMA (reference)", color = color.gray)Pro tip: Keep the two regression objects straight in your head: the LSMA endpoint series is honest history (each point used only past data), while a regression channel drawn over the same window silently refits every bar — so never test rules against how a channel looked in hindsight. If you want the trend estimate itself, plot the fitted slope rather than the line; watching its sign and magnitude is often clearer than watching crossovers. Educational information only, not financial advice; a least-squares fit describes the past and predicts nothing, and all trading carries risk of loss.
Educational only — not financial advice, not a recommendation to trade. No indicator is predictive; trading involves substantial risk of loss.