indicator · own pane · open source

Linear Regression R-Squared

Deepwick · @deepwickv1Updated 2 Oct 2026▲ 0 users
Running…
Loading chart…

About

Linear Regression R-Squared measures how well price fits a straight line on a 0-1 scale. Above 0.7 = price is trending cleanly; below 0.5 = noise dominates.

  • Use as a trend-strength filter for any regression-based signal.
  • Drops below 0.5 during chop and during the early bars of a new trend — be wary.
  • Sustained readings above 0.7 are when trend systems make their money.

Source code

//@version=5
indicator("Linear Regression R-Squared", overlay=false)
len = input.int(14, "Length", minval=2)
lrVal = ta.linreg(close, len, 0)
slope = lrVal - lrVal[1]
mean = ta.sma(close, len)
ssRes = math.sum(math.pow(close - (lrVal + slope * (bar_index - bar_index[len])), 2), len)
ssTot = math.sum(math.pow(close - mean, 2), len)
r2 = 1 - ssRes / ssTot
hline(0.5, "Noise floor", color=color.gray, linestyle=hline.style_dashed)
hline(0.7, "Trending", color=color.green, linestyle=hline.style_dashed)
p_R2 = plot(r2, "R2", color=color.teal, linewidth=2)
alertcondition(ta.crossover(r2, 0.7), "R2 enters trending", "R-squared crossed above 0.7 — trend is clean")
alertcondition(ta.crossunder(r2, 0.5), "R2 exits trending", "R-squared dropped below 0.5 — trend is messy")

Comments

No comments yet.

Sign in to comment.