# R-squared Trend Fit

A Trend concept (Trend strength & direction) in the LuxAlgo Library, with 1 indicator implementation.

## What is an R-squared trend fit?

An R-squared trend fit measures trend strength by running a [linear regression](https://www.luxalgo.com/library/concept/linear-regression/) of price against time over a lookback window and reporting R-squared, the fraction of price variance explained by the fitted line. The output runs from 0 to 1: near 1 means price moved almost exactly along a straight line through the window, near 0 means the line explains essentially nothing and the window was noise or range. Because the square discards the line's sign, R-squared is pure strength; direction comes from the regression slope, checked separately.

The construction borrows directly from statistics, and that lineage is its main appeal. R-squared has known sampling behavior, so instead of an arbitrary threshold a trader can ask whether the fit is statistically distinguishable from random data for the window length used. Published critical values exist for common lookbacks: shorter windows need higher R-squared before the trend reading means anything, longer windows clear significance at lower values. This puts trend-versus-no-trend on firmer footing than eyeballing a slope.

R-squared is the unsigned square of the [Correlation Trend Indicator](https://www.luxalgo.com/library/concept/correlation-trend-indicator/) computed over the same window, and it is the same statistic that underlies the confidence one should place in an [LSMA](https://www.luxalgo.com/library/concept/lsma/) or a [linear regression channel](https://www.luxalgo.com/library/concept/linear-regression-channel/): a channel drawn through a low R-squared window is a line through noise, however clean it looks.

## How it's calculated

R-squared is the squared Pearson correlation between the source series and the bar index over the lookback:

```
r_t = corr(C, idx) over the last n bars
R2_t = r_t^2
equivalently: R2_t = 1 - SSE / SST

  t: current bar index
  n: lookback period in bars (commonly 14 to 21)
  C: source values in the window, usually closes
  idx: bar index sequence over the window (0..n-1)
  corr: Pearson correlation coefficient
  SSE: sum of squared residuals from the fitted regression line
  SST: sum of squared deviations of price from its window mean
  R2_t: R-squared value at bar t, 0 to 1
```

Both forms are algebraically identical for a simple regression on time.

Some traders regress the logarithm of price instead, which makes fits comparable across large percentage moves.

Direction is not in the output; pair R-squared with the sign of the regression slope.

## How traders use it

- As a trend/no-trend gate: enable trend-following rules only when R-squared exceeds the critical value for the chosen window, and treat low readings as range conditions where mean-reversion logic applies.
- Combined with slope into a two-factor signal: rising slope with high R-squared qualifies a clean uptrend, while high slope with poor R-squared flags a jumpy, gap-driven move that trend entries handle badly.
- As a quality check before trusting regression-derived tools such as the [standard error channel](https://www.luxalgo.com/library/concept/standard-error-channel/); channel projections mean little when the underlying fit explains a small share of variance.
- Falling R-squared after an extended trend is watched as an early sign of trend maturity, often before price violates any level. It is a decay symptom, not a reversal signal, and chop can persist long after the reading drops.

## R-squared trend fit vs other strength measures

- **Correlation Trend Indicator** (https://www.luxalgo.com/library/concept/correlation-trend-indicator/): CTI is the signed square root of the same statistic: one bounded series carrying both direction and strength. R-squared isolates strength and leans on the slope for direction.
- **ADX/DMI system** (https://www.luxalgo.com/library/concept/adx-dmi-system/): ADX accumulates smoothed directional movement and reacts slowly at both ends of a trend. R-squared is a direct windowed fit with a statistical interpretation and a hard 0-to-1 scale.
- **Choppiness Index** (https://www.luxalgo.com/library/concept/choppiness-index/): The Choppiness Index infers trendiness from how range compounds across the window, without fitting any line. R-squared asks specifically about linear fit, so the two can disagree on curved but directional moves.
- **Vertical Horizontal Filter** (https://www.luxalgo.com/library/concept/vertical-horizontal-filter/): The VHF compares net range to the sum of bar-to-bar movement, a path-efficiency idea. R-squared measures how well a straight line explains the path, penalizing even efficient moves that bend.

## FAQ

### What is a good R-squared threshold?

It depends on the window, because short windows produce high R-squared by chance. Statistical tables give the cutoff for each length; for lookbacks near 14 to 20 bars, values below roughly 0.2 to 0.3 are generally indistinguishable from noise.

### Does high R-squared mean the trend will continue?

No. It certifies that the last n bars fit a line well. Strong fits often persist for a while, but the statistic is descriptive of the past window and mature trends can show their highest readings just before stalling.

### How is this different from just using the regression slope?

Slope measures steepness in price units and says nothing about fit quality; a gap-and-chop window can show a steep slope with terrible fit. R-squared measures fit quality and says nothing about steepness. Most uses need both.

### Should I compute it on price or log price?

For short windows it rarely matters. Over long windows or large percentage moves, log price makes constant-percentage growth look linear, which is usually the more meaningful notion of trend.

## Implementations in the Library

- R-squared Trend Fit (LuxAlgo): https://www.luxalgo.com/library/indicator/r-squared-trend-fit/

## Related concepts

- Trend Regime Label: https://www.luxalgo.com/library/concept/trend-regime-label/
- ADX / DMI System: https://www.luxalgo.com/library/concept/adx-dmi-system/
- Aroon: https://www.luxalgo.com/library/concept/aroon/
- Vortex: https://www.luxalgo.com/library/concept/vortex/
- Vertical Horizontal Filter: https://www.luxalgo.com/library/concept/vertical-horizontal-filter/
- Trend Intensity Index: https://www.luxalgo.com/library/concept/trend-intensity-index/
- Kaufman Efficiency Ratio: https://www.luxalgo.com/library/concept/kaufman-efficiency-ratio/
- Correlation Trend Indicator: https://www.luxalgo.com/library/concept/correlation-trend-indicator/
- Random Walk Index: https://www.luxalgo.com/library/concept/random-walk-index/
- Trend-quality Composites: https://www.luxalgo.com/library/concept/trend-quality-composites/

---

Source: https://www.luxalgo.com/library/concept/r-squared-trend-fit/ (LuxAlgo Library, the encyclopedia of trading & technical analysis). Free to use with attribution: https://www.luxalgo.com/library/license/