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.
Linear Regression(14) on MES 5m — line + R² showing trend quality
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
| Setting | Default | Range | Notes |
|---|---|---|---|
| Period | 14 | 2–500 | Lookback 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
| Handle | Type | Plotted | Notes |
|---|---|---|---|
| Regression Line | Numeric | Always | The endpoint value — where price "should" be according to the fit. |
| Slope | Numeric | On select | The line's steepness. Sign indicates direction. |
| R-Squared | Numeric | On select | 0-100. The line's goodness of fit. Above 70 = clean trend; below 30 = noise. |
Linear Regression node — default state
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.
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.
R² as a strategy-routing regime classifier
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.