Concept
Linear Regression
Linear Regression, also known as slope/intercept/forecast, R², is a Statistics concept. The Library holds 14 implementations, each one a working definition you can pull into Quant.
Top Linear Regression indicators
14 total
What is Linear Regression?
Linear regression fits a straight line through the last N bars of price by ordinary least squares. With the bar index as the input and price (usually the close) as the output, it chooses the slope and intercept that minimize the sum of squared vertical distances between the line and price. The result is a best-fit line through the middle of the window, summarized by its slope (the fitted change per bar), its intercept, and its value at the most recent bar.
Three readings fall out of one fit. The slope is a computed trend measure: its sign gives direction, its magnitude gives velocity. The fitted value at the newest bar, recomputed as the window rolls forward, traces the LSMA (least squares moving average); projected one bar ahead, it is often called a time series forecast. R², the squared correlation between price and time over the window, scores how much of the price variance the line explains, a common trend-quality proxy. The residuals around the line seed linear-regression channels and standard-error channels.
It matters because it swaps an eyeballed line for a computed one. A hand-drawn trendline depends on which swings you connect; the least squares line is identical for anyone using the same window and source, which is why regression slope and R² sit inside many trend filters and regime classifiers. The trade-off is window sensitivity: change the lookback and the line, slope, and forecast all change, and the fit assumes a linearity markets only deliver some of the time.
How to calculate Linear Regression
The fit is deterministic: given a window and a source series, ordinary least squares produces exactly one line, so any two platforms with the same inputs draw the same result.
- 1Pick a window and a source. Choose a lookback of N bars and a price series, typically closes. Number the bars 1 through N as the x values; the prices are the y values.
- 2Fit by least squares: the slope equals the covariance of x and y divided by the variance of x, and the intercept equals the mean of y minus the slope times the mean of x. This is the unique straight line that minimizes the sum of squared vertical distances to price.
- 3Read the outputs: the line's value at the latest bar is the regression value (the current LSMA value), the slope is the fitted change per bar, and R² (the squared correlation between price and time) grades the fit from 0 to 1.
- 4Refit every bar: when a new bar closes, the window shifts and the entire line is recomputed, so anchored drawings such as regression channels redraw over history while the endpoint series simply extends by one value.
How traders use it
- As a trend filter: the sign and size of the regression slope give a computed direction read, often normalized by ATR or expressed as a percentage so thresholds transfer across symbols and timeframes. It plays the same role as an MA slope filter, with less endpoint lag than an SMA slope of the same length.
- As a low-lag average: plotting each rolling window's endpoint produces the LSMA, which turns faster than an SMA of the same length at the cost of more overshoot in chop.
- As a trend-quality gate: R² near 1 marks windows a straight line describes well, while values near 0 mark sideways noise. Many trend/range classifiers only take directional setups when slope and R² agree.
- As a mean-reversion frame: residual bands around the line (a multiple of the standard deviation or standard error) mark stretched excursions, and the gap between price and the one-bar-ahead regression forecast is the basis of the Chande Forecast Oscillator. Extrapolating the line forward assumes the drift persists, which is not guaranteed.
Linear Regression vs related concepts
Trendline: A trendline is drawn through swing highs or lows and acts as a boundary price reacts to; a regression line is computed through the middle of the data and acts as a trend axis. One is a discretionary level, the other is a fit.
LSMA: The LSMA is the time series of regression endpoints, one value per bar, used like a moving average. Linear regression proper is the whole fitted line over one window, with slope, R², and residuals attached.
Linear-regression Channel: The channel takes a single fitted line over an anchored window and adds parallel bands a set number of standard deviations or standard errors away. Regression is the fitting step; the channel is one drawing built on top of it.
Polynomial Regression: Polynomial regression fits a curve of degree 2 or higher instead of a straight line, so it can bend with the data. The flexibility tracks turns better in-sample but overfits more easily and swings harder at the endpoints.
More Linear Regression implementations
Related concepts · Regression & filtering
Concept family
Statistics
45 concepts mapped · 37 in the Library
Linear Regression FAQ
What does the slope of a linear regression line tell you?
Direction and rate: a positive slope means the best-fit path of price over the window rises, and its value is the fitted change per bar. Raw slope is in price units per bar, so it is not comparable across symbols or timeframes; many tools normalize it by ATR, by percentage, or against its own history before applying thresholds.
What is a good R-squared value for trend trading?
There is no universal threshold. R² runs from 0 to 1 and measures how much of the window's price variance the straight line explains, so higher means cleaner drift. Typical values shift with timeframe, window length, and market, which is why many systems treat it as a relative filter rather than a fixed cutoff. A slow grind can score high while a violent but choppy rally scores low.
Can linear regression predict future prices?
It extrapolates rather than predicts: projecting the fitted line forward assumes the window's average drift continues, and markets do not guarantee that. Regime changes break the assumption exactly when it matters most, and the projection error grows the further ahead the line is extended. Treat the forecast as a baseline to measure deviation against, not as a prediction.
Is linear regression better than a moving average?
It answers a different question. A moving average smooths price into a single level; a regression fit adds direction (slope) and fit quality (R²), and its endpoint lags less than an SMA of equal length. The cost is sensitivity: the fit re-estimates every bar and reacts harder to window choice and outliers. Many systems use both rather than choosing.
Why do regression channels redraw or repaint on the chart?
Because the whole line is refit each bar. When the lookback shifts or the anchor extends, every point of the drawing is recomputed, so regression channels move over history by design. The endpoint series (the LSMA) does not revise past values. If you alert on regression tools, confirm the value on closed bars; see repaint-safe engineering.
Build Linear Regression your way.
Quant writes, tests, and refines it with you — then it runs on LuxAlgo charting or ports to TradingView.
.png&w=3840&q=75)

