R-Squared
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
| Name | Type | Default | Description |
|---|---|---|---|
| period | number | 20 | Rolling window size. |
Inputs & Outputs
| Port | Type | Description |
|---|---|---|
| Inputs | ||
| input | number[] | Source numeric array |
| Outputs | ||
| values | number | null | Computed value at each bar; null before the warmup period completes |
| timestamps | number[] | Bar timestamps (UNIX ms), aligned 1-to-1 with values |
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.