Linear Regression

A statistical best-fit line over the lookback. Outputs the line's endpoint, slope, and goodness-of-fit (R-squared) — a quantitative trend reading.

Updated 2026-05-24
7 min read
intermediate

A statistical best-fit straight line through the price points over the lookback window. The endpoint of that line is the indicator's primary output; the slope tells you the trend's steepness; R-squared tells you how well the line actually fits the data — i.e., whether there's a real trend or just noise.

Placeholder · Video / Clip

Linear Regression(14) on MES 5m — line + R² showing trend quality

Author hint: MES 5m, dark theme, ~80-bar window. Linear Regression(14) overlaid on the price chart as a single line, with the R-squared shown in a sub-panel below (range 0-100). Show one trending stretch (R² high) and one choppy stretch (R² low). ≤ 10 seconds, no audio. Save to /public/images/guide/indicators/linear-regression/chart.mp4.

What it measures

  • The statistical "best fit" trend. Where price should be if it continues the current straight-line trajectory.
  • Trend slope. Positive slope = uptrend, negative = downtrend. Magnitude indicates steepness.
  • Trend quality (R-squared). Bounded 0-100. High R² means price is following the line closely (clean trend). Low R² means price is scattered around the line (chop).

Formula

How AlgoLift computes it

For each bar, fit a least-squares regression line to the prior period closes:

slope = Σ((x_i − x_mean)(price_i − price_mean)) / Σ((x_i − x_mean)²)
intercept = price_mean − slope × x_mean
Value = intercept + slope × (period − 1)    (endpoint of the line at the most recent bar)
R² = 1 − (SS_residual / SS_total) × 100     (goodness of fit, 0-100)

The R-squared output is the killer feature — it tells you whether the trend the line describes is real (R² near 100) or whether the line is essentially fitting noise (R² near 0).

Inputs in AlgoLift

SettingDefaultRangeNotes
Period142–500Lookback for fitting the regression line.

Recommended periods

  • 9–14: Short-to-medium-term trend analysis.
  • 14 (default): Standard.
  • 20–30: Slower, smoother fit for swing setups.
  • 50+: Long-horizon regime classification.

Outputs in AlgoLift

HandleTypePlottedNotes
Regression LineNumericAlwaysThe endpoint value — where price "should" be according to the fit.
SlopeNumericOn selectThe line's steepness. Sign indicates direction.
R-SquaredNumericOn select0-100. The line's goodness of fit. Above 70 = clean trend; below 30 = noise.
Placeholder · Screenshot

Linear Regression node — default state

Author hint: The Linear Regression node on a fresh canvas with default Period=14. All three output handles labeled (Regression Line, Slope, R-Squared). Dark theme. Tight crop. Save to /public/images/guide/indicators/linear-regression/node.png.

How to read it

  • Price above the regression line: Above-trend reading. May indicate overextension in a range, or trend strength in a real trend (R² tells you which).
  • Price below the regression line: Below-trend reading. Same caveat.
  • Slope positive: Uptrend in the lookback window.
  • Slope negative: Downtrend in the lookback window.
  • R² > 70: The fit is good — the trend is real and consistent. Trend-following strategies favored.
  • R² < 30: The fit is poor — price is mostly noise around the line. Mean-reversion strategies favored, or pause trading entirely.
  • R² between 30 and 70: Transitional. Neither extreme is reliable.
Key Takeaway

R-squared is the differentiating output. Most indicators give you direction; Linear Regression gives you direction plus confidence in that direction. Strategies that gate their entries on R² > 70 dramatically improve their signal-to-noise ratio.

Best in / worst in

Best in instruments and timeframes that produce clear linear trends — equity indices in established regimes, momentum-driven currencies during macro moves. R² lets you objectively classify "is this a real trend?" without subjective interpretation.

Worst in instruments with exponential-style growth (where a linear fit systematically underestimates the trend), and in markets with frequent gaps (the regression line is sensitive to single outlier bars).

Three setups

1. R-squared as a regime filter

The signature Linear Regression pattern — use R² to gate other strategies.

  • Trend-following strategies allowed when: R² > 70.
  • Mean-reversion strategies allowed when: R² < 30.
  • No trades when: R² between 30 and 70.

A simple, single-output regime classifier that maps directly to strategy-type selection.

Placeholder · Screenshot

R² as a strategy-routing regime classifier

Author hint: Node graph showing Linear Regression R-Squared output feeding two parallel comparisons (> 70 and < 30) that gate two different strategy sub-graphs via Conditional Flow. Save to /public/images/guide/indicators/linear-regression/setup-01.png.

2. Slope + R² combined entry

Trade when slope and R² both confirm a strong trend.

  • Long: Slope > 0 AND R² > 70 AND price > Regression Line.
  • Short: Slope < 0 AND R² > 70 AND price < Regression Line.

The slope gives direction; R² confirms the slope is meaningful; the price-vs-line check ensures price is moving in the slope's direction.

3. Regression Line as a dynamic support/resistance

Use the regression line value as a pullback target in trends.

  • Long pullback: Slope > 0 AND R² > 60 AND price touches within 0.5× ATR of the Regression Line value.
  • Short pullback: Symmetric.

Combined with a fixed-distance stop below the line, this captures pullbacks-to-trend efficiently.

Advanced patterns in AlgoLift

R² for position sizing. Scale position size by R²: size = base × R² / 100. Strong, clean trends get full size; noisy environments get small or no positions. Combined with fixed-fractional risk math, this produces an objective volatility/quality-adjusted sizing scheme.

R² + ADX dual confirmation. Both indicators measure trend strength via different methods. Require R² > 70 AND ADX/DMI > 25 before allowing any trend-following entry. The dual filter eliminates almost all noise-trend false signals — and trades become rare but high-conviction.

Regression Line as the trailing exit reference. In a long position, exit when price closes below the Regression Line. Acts as a dynamic, statistically-anchored trailing stop. Wire via Set Exit Condition.

Regression Slope for scale-in triggers. Use the initial entry to take a starter position. Use Scaled Entry to add when the Slope reaches a higher threshold (trend accelerating). The objective slope reading prevents arbitrary "trend looks strong" judgment calls.

Common mistakes

  • Trading direction without R² confirmation. A negative slope with R² = 15 means the line is fitting noise — the "downtrend" isn't real. Always gate trend signals on R² > 60 minimum.
  • Using on exponentially-growing instruments. Stock indices during long bull runs have exponential price action — linear regression systematically underestimates the slope. Use a log-scale linear regression instead, or switch to a moving-average-based trend tool.
  • Tuning the period to maximize R². A short period will always have high R² because there's less data to fit poorly. R² is informative within a fixed period — comparing R² across different periods is meaningless.
Common Misconception
Myth
High R² guarantees the trend will continue.
Reality
High R² means the *past* lookback window had a clean linear trend. It says nothing about whether the next bars will continue the trend or reverse. R² is a quality measure for the current reading, not a predictive measure for the future. Strategies that use R² to size positions tend to be more robust than those that use R² as a 'high-confidence entry' signal.