R2
R2 in ProBuilder returns the R-Squared coefficient of a linear regression over N bars, a 0 to 1 measure of trend strength. Syntax, formula, worked examples.
Syntax
R2[N](price)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
N | integer | none | Number of bars in the regression window. Common values are 14 to 30. Short windows react faster but flag many false trends. |
price | price source | close | The series the regression is fitted to. Usually close, also accepts open, high, low, or a custom variable. |
Formula
R2 = 1 - (SSres / SStot)
where SSres = sum of squared residuals from the regression line
SStot = sum of squared deviations of price from its meanR-Squared is the coefficient of determination of the least-squares line fitted to the last N values of price. It equals the squared correlation between price and time over the window, so it is bounded between 0 and 1.
How it works
On each bar, ProBuilder fits a straight line through the last N price points and measures how much of the price variation that line explains. If the points sit almost exactly on the line, residuals are tiny and R2 approaches 1. If price wanders randomly around its mean, the line explains little and R2 approaches 0.
R2 is direction-blind. A perfect uptrend and a perfect downtrend both return values near 1, so R2 says how trendy the market is, not which way it trends. For direction, pair it with LinearRegressionSlope or compare consecutive LinearRegression values, as in the first example.
A common rule of thumb treats readings above roughly 0.4 to 0.6, depending on N, as statistically meaningful trends and lower readings as noise. Because R2 is computed on a rolling window, it drops quickly when a clean trend enters a consolidation, which makes it useful as a regime filter that switches trend-following logic on and off.
Examples
Example 1, Trend strength with direction detection (Indicator)
i1 = R2[20](close)
LR1 = linearregression[20](close)
LR2 = linearregression[20](close[1])
// A significant fit plus a falling regression line means a bearish trend
if(i1 > 0.4 AND LR1 < LR2) THEN
bullish = 0
bearish = -1
ELSIF(i1 > 0.4 AND LR1 > LR2) THEN
bullish = 1
bearish = 0
ELSE
bullish = 0
bearish = 0
ENDIF
RETURN bullish, bearishR2 supplies the strength test and the change in the regression line supplies the direction. Both outputs stay at 0 when the fit is too weak to trust, filtering out sideways periods.
Example 2, Regime filter for a trend-following system (ProOrder)
DEFPARAM CumulateOrders = false
fit = R2[20](close)
slope = LinearRegressionSlope[20](close)
// Trade only when the market is measurably trending upward
IF NOT LongOnMarket AND fit > 0.6 AND slope > 0 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// Exit when the trend quality degrades
IF LongOnMarket AND fit < 0.3 THEN
SELL AT MARKET
ENDIFEntries require both a strong fit and a positive slope. The exit triggers on trend quality alone, closing the position once price stops behaving linearly, regardless of direction.
Example 3, Screening for the most orderly uptrends (ProScreener)
fit = R2[20](close)
slope = LinearRegressionSlope[20](close)
SCREENER[fit > 0.7 AND slope > 0](fit AS "R-Squared")Lists instruments whose last 20 closes fit a rising straight line with R-Squared above 0.7, sorted by the fit column.
Interpretation
| R2 range | Reading |
|---|---|
| 0.7 to 1.0 | Strong linear trend. Price is tracking its regression line closely. |
| 0.4 to 0.7 | Moderate trend. Directional bias exists but with meaningful noise. |
| 0.0 to 0.4 | Weak or no trend. Mean-reversion behaviour dominates. |
Exact thresholds depend on N. Shorter windows produce higher R2 values by chance, so a 0.7 reading over 10 bars is far less significant than 0.7 over 50 bars. R2 is best read as a regime gauge, rising through a threshold suggests trend logic should be active, falling through it suggests range logic.
Common errors and gotchas
- No direction information. R2 near 1 occurs in both strong uptrends and strong downtrends. Trading long on high R2 alone buys into crashes. Always combine with a slope or price comparison.
- Short windows inflate the reading. Any few points fit a line reasonably well, so
R2[5]regularly prints high values in pure noise. Use longer windows or raise the threshold whenNis small. - Lag at turning points. R2 stays elevated for several bars after a trend ends because the window still contains the old trend. Expect regime signals to arrive late by design.
- Bounded output, unbounded expectations. The value never leaves the 0 to 1 range. Conditions such as
R2[20](close) > 5can never be true and silently disable the logic that depends on them.
Related instructions
LinearRegression, value of the fitted regression line itself.LinearRegressionSlope, slope of the regression line, supplies direction.STD, standard deviation, the dispersion measure behind SStot.STE, standard error of the regression estimate.TimeSeriesAverage, moving average built from regression endpoints.EndPointAverage, related endpoint-based smoothing.ADX, alternative trend-strength gauge based on directional movement.Average, simple moving average, often paired as a baseline trend filter.
