R-Squared

Windowed · default period 20Statistics

The R-Squared (Coefficient of Determination) node measures the goodness-of-fit of a linear trend to the rolling window, producing a value from 0 to 1. R² near 1 means the data is nearly perfectly linear (strong, clean trend); R² near 0 means the data is random or non-linear (choppy, noisy). R² is the premier trend-quality filter — use it to gate trend-following signals only when the market is genuinely trending.

Algorithm

  • Fit OLS regression to window pairs (i, v[i])
  • SStot = Σ(y − ȳ)², SSres = Σ(y − ŷ)²
  • R² = 1 − SSres / SStot
  • When SStot = 0 (all values equal): R² = 1 (perfect line)
  • Requires ≥ 2 non-null pairs; returns null otherwise

Parameters

NameTypeDefaultDescription
periodnumber20Rolling window size.

Inputs & Outputs

PortTypeDescription
Inputs
inputnumber[]Source numeric array
Outputs
valuesnumber | nullComputed value at each bar; null before the warmup period completes
timestampsnumber[]Bar timestamps (UNIX ms), aligned 1-to-1 with values
Live mode: In live streaming mode the node updates only the last bar in-place rather than recalculating the full array, keeping CPU usage minimal during real-time data feeds.

Use Cases

Trend Quality Gate

Only take trend-following signals when R² > 0.7 — this filters out noisy periods and retains entries only during clean, linear price advances.

Trending vs. Ranging Regime

R² > 0.5 → trending regime; R² < 0.3 → ranging regime. Use this threshold to switch between trend-following and mean-reversion strategy modes.

Slope Confidence

High R² combined with steep Slope confirms a high-confidence directional move. Low R² with steep Slope warns of a noisy, unreliable move.