LinearRegression
LinearRegression in ProBuilder returns the value of a least squares regression line fitted to the last N bars of price. Syntax, formula, worked examples.
Syntax
LinearRegression[N](price)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
N | integer | required | Number of bars in the regression window. Short windows such as 10 hug price, long windows such as 50 or 100 describe the broader trend. |
price | price source | close | Series the line is fitted to. Usually close, but open, high, low, typicalprice, or a custom variable are accepted. |
Formula
Fit y = a + b*x by least squares over the last N bars,
minimising sum( (price[i] - (a + b*i))^2 )
LinearRegression = value of the fitted line at the current barThe coefficients are recomputed from scratch on every bar, so the output traces the moving end point of a rolling regression line.
How it works
Least squares regression finds the straight line that passes as close as possible to the last N price points. LinearRegression evaluates that line at the newest bar and returns the single number, not the whole segment. Bar after bar, these end points form a smooth curve that behaves like a moving average with noticeably less lag, because a fitted line extrapolates the window's direction instead of averaging it symmetrically.
The returned value is often read as the equilibrium price for the window: where price "should" be if the recent trend were perfectly linear. The difference between the actual price and this value measures how stretched the market is relative to its own trend, which is the basis of the oscillator in Example 1.
Companion functions complete the picture. LinearRegressionSlope returns the coefficient b, the direction and steepness of the same fitted line, and R2 reports how well the line actually fits the data.
Examples
Example 1, Linear regression oscillator (Indicator)
// Regression value of the close over the last 10 bars
a = LinearRegression[10](close)
// Current close
b = close
// Oscillator: distance between price and its regression line
c = b - a
RETURN cPlots the gap between the close and its 10-bar regression value. Positive readings mean price trades above its short-term equilibrium, negative readings below it.
Example 2, Regression line as trend filter (ProOrder)
// Trade long only while price holds above its 50-bar regression value
eq = LinearRegression[50](close)
IF NOT LongOnMarket AND close CROSSES OVER eq THEN
BUY 1 CONTRACT AT MARKET
ELSIF LongOnMarket AND close CROSSES UNDER eq THEN
SELL AT MARKET
ENDIFUses the regression end point the way a moving average is normally used, with entries on the upward cross and exits on the downward cross.
Example 3, Stretched above equilibrium (ProScreener)
// Instruments trading well above their 20-bar regression value
eq = LinearRegression[20](close)
stretch = 100 * (close - eq) / eq
SCREENER[stretch > 2](stretch AS "Pct above regression")Returns instruments whose close is more than 2 percent above the 20-bar regression value, a simple overextension scan.
Interpretation
Price oscillating around the regression value is normal behavior; the value itself indicates the trend's current location. Persistent readings above the line signal buying pressure, persistent readings below signal selling pressure, and the size of the gap indicates stretch. Deviation-based tools such as Raff regression channels build bands around exactly this line.
The output says nothing about fit quality on its own. A regression through choppy, sideways data still returns a value every bar. Checking R2 alongside it separates meaningful trends from noise.
Common errors and gotchas
- A value, not a drawn line. The function returns one number per bar, the end point of the current fit. It does not draw the regression segment across the chart, and past outputs are end points of older windows, not points on today's line.
- Wrong bracket type.
LinearRegression(10, close)fails to compile. The period belongs in square brackets and the price source in parentheses:LinearRegression[10](close). - Sensitive to outliers. Least squares penalises squared errors, so a single spike inside the window pulls the whole line toward it. On thin instruments, one bad print distorts the value for
Nbars. - Short windows overreact. With small
Nthe line follows price almost tick for tick and the deviation oscillator barely leaves zero. ChooseNlarge enough that the equilibrium concept is meaningful.
Related instructions
LinearRegressionSlope, slope coefficient of the same fitted line.R2, coefficient of determination, measures how well the line fits.TimeSeriesAverage, closely related regression-based smoothing.Average, simple moving average for comparison.ExponentialAverage, low-lag smoothing alternative.STD, standard deviation, used to build bands around a regression line.EndPointAverage, another end-point based average.WeightedAverage, linearly weighted average with reduced lag.
