Linear Regression
Fits a straight line through recent price data using least-squares to estimate the current trend direction and forecast near-term price levels.
Overview
Linear Regression in trading fits a straight trend line through a defined number of recent price bars using ordinary least squares (OLS). The slope of the regression line indicates trend direction and strength; the endpoint of the line (the fitted value at the most recent bar) serves as a dynamic "fair value" level. It can be used to generate trend signals, identify price deviation from trend, or as a simple short-term price forecast.
How it looks on a chart
Illustration only — synthetic data generated for visual reference.
Linear Regression draws the best-fit straight line through recent closing prices. Unlike a moving average that always follows price from behind, the regression line finds the central tendency of recent price movement mathematically. The slope of the line tells you the direction and speed of the trend: a steeply rising slope means a fast uptrend; a flat line means sideways action. The "Linear Regression Value" (the point where the trend line ends at the most recent bar) acts as a dynamic fair value. If price is significantly above this line, it may be extended. If price is below it, it may be undervalued relative to its recent trend. The "Linear Regression Channel" adds parallel lines above and below the central regression line at a set distance (usually based on the standard error of the regression). Price oscillating between these channel boundaries is normal; a breakout suggests a potential trend change.
For a lookback window of n periods, the regression of closing prices on time gives a slope β = n×Σ(t×P) − Σt×ΣP) / (n×Σt² − (Σt)²) and intercept α. The fitted value at the end of the window is the Linear Regression Value (LRV). The R-squared of the regression measures trend linearity — values above 0.8 indicate a strong linear trend. The slope (in price units per bar) can be normalized to a percentage: β / Mean Price × 100, giving a comparable trend strength metric across assets. The "Linear Regression Slope" indicator plots only this normalized slope, turning positive when the trend is accelerating upward and negative when decelerating or reversing. Using rolling linear regression to track slope changes over time creates a momentum indicator that is less noisy than simple price rate of change. A slope that turns from positive to negative while still relatively elevated is an early warning of trend exhaustion. Combining rolling R² (trend quality) with slope magnitude creates a composite trend-quality score.
From a statistical standpoint, applying OLS to financial time series violates the independence assumption — price returns are not i.i.d. and exhibit autocorrelation and heteroskedasticity. The Newey-West HAC estimator corrects for these issues when using regression coefficients as inputs to hypothesis tests, though for pure signal generation the OLS estimates remain useful heuristics. Time Series Momentum research (Moskowitz, Ooi, Pedersen 2012) is fundamentally built on a regression framework — the sign of the regression beta over a lookback window determines the long/short position. The 12-month linear regression slope has been shown to be equivalent in predictive power to the raw 12-month return difference, but with slightly lower noise. In Gilito's predictive model suite, linear regression serves as the baseline forecasting model. Its out-of-sample performance sets the benchmark against which ARIMA, Random Forest, and LSTM models are compared. For many assets and timeframes, simple linear regression on 20–60 day windows is surprisingly competitive with more complex models, particularly in high signal-to-noise trending environments.
Formula
ŷ = α + β×t β = (n×Σ(t×P) − Σt×ΣP) / (n×Σt² − (Σt)²) α = (ΣP − β×Σt) / n
- 1.Select a lookback window n (e.g., 20 bars) and index the bars t = 1 to n.
- 2.Compute the slope: β = (n×Σ(t×Pt) − Σt×ΣPt) / (n×Σt² − (Σt)²).
- 3.Compute the intercept: α = (ΣPt − β×Σt) / n.
- 4.The Linear Regression Value at bar n is: LRV = α + β×n.
- 5.Plot the LRV as an overlay; optionally add ±2 standard error channels around the regression line.
Parameters
| Parameter | Default | Range | Description |
|---|---|---|---|
| Period | 20 | 5–100 | Number of bars in the regression window. |
| Channel Width | 2 | 1–3 | Standard error multiplier for channel boundaries. |
Trading signals
bullish: Regression slope turns positive from negative
Trend direction shifting upward — potential long entry or regime change.
bearish: Regression slope turns negative from positive
Trend direction shifting downward — potential short entry or exit.
bullish: Price reverts to regression line from below
Mean reversion back to linear trend — potential continuation long.
bearish: Price breaks below lower regression channel
Trend breakdown — move exceeds normal deviation from linear trend.
Limitations
- •Assumes price follows a linear trend — invalid for most financial data over long periods.
- •Highly sensitive to the lookback window — different periods give very different slopes and values.
- •OLS regression assumes homoskedastic, independent errors — both assumptions violated in financial data.
- •Curve-fitting risk: a regression line always fits the in-sample data perfectly; out-of-sample extension is unreliable.
Linear regression serves as Gilito's baseline forecasting model against which all more complex predictive models are benchmarked. It is also used to compute rolling trend slope features (10, 20, 60-day windows) for every asset, feeding these slope values as inputs into the machine learning strategy selection layer.