LinearRegressionSlope
LinearRegressionSlope in ProBuilder returns the slope of a least squares regression line over N bars, measuring trend direction and strength. Examples.
Syntax
LinearRegressionSlope[N](price)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
N | integer | required | Number of bars in the regression window. Small values react quickly but flip sign often; larger values describe the sustained trend. |
price | price source | close | Series the line is fitted to. Usually close; open, high, low, or a custom variable also work. |
Formula
Fit y = a + b*x by least squares over the last N bars
b = ( N * sum(x*y) - sum(x) * sum(y) ) / ( N * sum(x^2) - sum(x)^2 )
LinearRegressionSlope = bwhere x is the bar index inside the window and y the price. The result is expressed in price units per bar.
How it works
While LinearRegression returns the level of the fitted line, LinearRegressionSlope returns its direction. Each bar, the function refits the line over the latest N prices and outputs the coefficient b: how much the fitted line rises or falls per bar. A reading of 0.5 on a daily chart means the regression trend is climbing half a point per day.
Because the fit considers every point in the window rather than just the first and last, the slope is less sensitive to a single noisy bar than a plain N-bar price difference. It is effectively a smoothed rate of change, which is why it doubles as a momentum measure.
The zero line is the natural pivot. Crossings from negative to positive flag a possible transition to an uptrend, crossings the other way a possible downtrend. Pairing the slope with R2 filters these signals, since a steep slope through poorly fitting data is less trustworthy than a moderate slope through a clean trend.
Examples
Example 1, Slope zero-cross signals (Indicator)
// Slope of the 10-bar regression on the close
i1 = LinearRegressionSlope[10](close)
// Flag transitions of the slope through zero
IF i1 > 0 AND i1[1] < 0 THEN
bullish = 1
bearish = 0
ELSIF i1 < 0 AND i1[1] > 0 THEN
bullish = 0
bearish = -1
ELSE
bullish = 0
bearish = 0
ENDIF
RETURN bullish, bearishMarks the bar where the 10-bar slope turns positive with a value of 1 and the bar where it turns negative with -1, a compact trend-change detector.
Example 2, Quality-filtered trend scan (ProScreener)
// Rising instruments where the regression line actually fits the data
slope = LinearRegressionSlope[20](close)
quality = R2[20](close)
SCREENER[slope > 0 AND quality > 0.8](slope AS "Slope")Returns instruments with a positive 20-bar slope whose R-squared exceeds 0.8, keeping only trends that are both rising and well defined.
Example 3, Slope as entry filter (ProBacktest)
// Only take breakout longs while the 50-bar slope is positive
slope = LinearRegressionSlope[50](close)
IF NOT OnMarket AND slope > 0 AND close CROSSES OVER Highest[20](high)[1] THEN
BUY 1 CONTRACT AT MARKET
ENDIF
IF LongOnMarket AND slope < 0 THEN
SELL AT MARKET
ENDIFRequires the medium-term regression slope to be positive before accepting a 20-bar breakout, and closes the position once the slope turns down.
Interpretation
Sign gives direction: positive slope, uptrend; negative slope, downtrend. Magnitude gives steepness, but in absolute price units, so it must be judged against the instrument's own scale. A slope of 2 is steep on an instrument trading at 50 and negligible on one trading at 5000.
Slope behavior also describes trend maturity. A rising slope means the trend is accelerating, a falling but still positive slope means an uptrend losing momentum, a pattern that often precedes the actual price turn.
Common errors and gotchas
- Not comparable across instruments. The raw slope depends on price scale. For screeners covering many instruments, normalise it, for example
100 * LinearRegressionSlope[20](close) / close, to get percent per bar. - Whipsaws around zero. In sideways markets the slope hovers near zero and changes sign frequently. Zero-cross logic without a magnitude threshold or an
R2filter generates a stream of false signals. - Wrong bracket type.
LinearRegressionSlope(10, close)does not compile. Period in square brackets, price source in parentheses. - Steep does not mean reliable. A large slope through scattered data reflects a few outliers, not a trend. Check fit quality with
R2before acting on the value.
Related instructions
LinearRegression, the level of the same fitted line.R2, fit quality of the regression, the natural companion filter.ROC, rate of change, a simpler momentum measure.Momentum, raw price difference overNbars.Average, moving average whose slope serves a similar purpose.ADX, trend strength without direction, an alternative filter.STE, standard error of the regression estimate.TimeSeriesAverage, regression-based smoothing of price.
