Indicators/probuilder · probacktest · proorder · proscreener

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

probuilder
LinearRegression[N](price)

Parameters

NameTypeDefaultDescription
NintegerrequiredNumber of bars in the regression window. Short windows such as 10 hug price, long windows such as 50 or 100 describe the broader trend.
priceprice sourcecloseSeries the line is fitted to. Usually close, but open, high, low, typicalprice, or a custom variable are accepted.

Formula

code
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 bar

The 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)

probuilder
// 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 c

Plots 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)

probuilder
// 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
ENDIF

Uses 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)

probuilder
// 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 N bars.
  • Short windows overreact. With small N the line follows price almost tick for tick and the deviation oscillator barely leaves zero. Choose N large enough that the equilibrium concept is meaningful.