# Linear Regression

Linear Regression is an indicator on [Vela](https://vela.luxalgo.com/chart), our next-generation charting platform, written in Pine Script®.

Linear Regression fits a least-squares regression to price over a 100-bar lookback and plots the result according to a mode input: a regression curve (default), the slope value, or a channel around the fitted line with bands at plus and minus two standard deviations. It shows where price sits relative to its statistical trend over the window. The calculation is the standard least-squares method with no proprietary adjustments.

- Indicator in the LuxAlgo Library, by LuxAlgo, published Apr 13, 2020.
- Access: free.
- Concepts: [Linear-regression Channel](https://www.luxalgo.com/library/concept/linear-regression-channel/) (primary)
- Open on Vela: https://vela.luxalgo.com/chart?pineScriptCodeId=q0d8coxswpc2ug0h9vvlssbh
- Page: https://www.luxalgo.com/library/indicator/linear-regression/

The **Linear Regression** indicator fits a least-squares regression to price over a 100-bar lookback and displays the result according to a Mode input: a regression curve drawn on the price chart (default), the per-bar slope of the fit, or a channel around the fitted line with bands offset by two standard deviations of price around the fit. It shows where price sits relative to its statistical trend over the window, using the standard least-squares method with no proprietary adjustments. The Slope plots in the pane around a dashed zero line.

## How to Trade the Linear Regression?

- **Price above or below the Curve**: the source trading above the fitted value means price sits above its statistical trend; sustained readings on one side describe a directional window.
- **Slope above zero**: positive per-bar drift in the fit, an objective trend-direction read, with zero-line crossings marking inflections.
- **Steepening or flattening Slope**: acceleration or deceleration of the fitted trend, often visible before direction changes.
- **Price at a Channel band**: roughly two deviations from the fitted line. Stretched within the window, with the base line as the fair-value reference.

## Linear Regression Settings

- **Mode** (default Curve): selects the display: Curve on the price chart, Slope in the pane, Channel as the fitted line and deviation bands over the last Length bars.
- **Length** (default 100): number of bars used to fit the regression. Longer for a steadier trend estimate.
- **Source** (default close): price series the regression is fitted to.
- **Deviation Multiplier** (default 2): channel band offset in standard deviations of price around the fitted line, also used by the band cross alerts.

## Frequently Asked Questions

### How is the regression Curve different from a moving average?

A moving average weights past values and always trails a trending series, while the Curve is the endpoint of a best-fit line that projects the window's trend right up to the current bar. It hugs directional moves more closely, at the cost of overshooting when the fitted trend breaks down.

### Why does the Channel change shape as new bars print?

Every new bar triggers a fresh fit over the latest Length bars, so slope, base line, and band width are all recomputed as the window rolls forward. That makes the channel a present-tense map of the current window rather than a backtestable structure.

### What alerts does this build include?

Upper Band Cross fires when the source crosses above the upper deviation band, Lower Band Cross when it crosses below the lower one. Both use the rolling fit with the Deviation Multiplier offset and evaluate in every mode, so stretch alerts keep working while the Curve or Slope display is active.

## Related indicators

- [Linear Regression Histogram](https://www.luxalgo.com/library/indicator/linear-regression-histogram/) (Linear-regression Channel): markdown at https://www.luxalgo.com/library/indicator/linear-regression-histogram.md
- [Periodic Linear Regressions](https://www.luxalgo.com/library/indicator/periodic-linear-regressions/) (Linear-regression Channel): markdown at https://www.luxalgo.com/library/indicator/periodic-linear-regressions.md
- [Volume Profile Regression Channel](https://www.luxalgo.com/library/indicator/volume-profile-regression-channel/) (Linear-regression Channel): markdown at https://www.luxalgo.com/library/indicator/volume-profile-regression-channel.md
- [Volumetric Regression Heatmap](https://www.luxalgo.com/library/indicator/volumetric-regression-heatmap/) (Linear-regression Channel): markdown at https://www.luxalgo.com/library/indicator/volumetric-regression-heatmap.md
- [Machine Learning Regression Trend](https://www.luxalgo.com/library/indicator/machine-learning-regression-trend/) (Linear-regression Channel): markdown at https://www.luxalgo.com/library/indicator/machine-learning-regression-trend.md
- [Trendlines with Breaks](https://www.luxalgo.com/library/indicator/trendlines-with-breaks/) (Trendline): markdown at https://www.luxalgo.com/library/indicator/trendlines-with-breaks.md
- [Trendline Breakout Navigator](https://www.luxalgo.com/library/indicator/trendline-breakout-navigator/) (Trendline): markdown at https://www.luxalgo.com/library/indicator/trendline-breakout-navigator.md
- [FVG Channel](https://www.luxalgo.com/library/indicator/fvg-channel/) (Parallel Channel): markdown at https://www.luxalgo.com/library/indicator/fvg-channel.md

---

Source: https://www.luxalgo.com/library/indicator/linear-regression/ (LuxAlgo Library). Free to use with attribution: https://www.luxalgo.com/library/license/
