Indicator · Pine Script v4 · On the chart

Linear Regression (All Data)

by everget

This indicator fits a least-squares linear regression to every available bar, up to a 5000-bar limit, using the selected source.

View on GitHub

GitHub stars948on the repository
Pine versionv4older, still runs
Lines of code86
Settings1inputs
LicenceGPL-3.0
Last update10 days ago18 Oct 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

86 lines of Pine Script v4 from statistics/linear_regression_all_data.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

linear_regression_all_data.pine
//@version=4
// Copyright (c) 2020-present, Alex Orekhov (everget)
// Linear Regression (All Data) script may be freely distributed under the terms of the GPL-3.0 license.
var int MAX_BARS_BACK = 5000
study("Linear Regression (All Data)", overlay=true, max_bars_back=MAX_BARS_BACK)

n = bar_index + 1

X = n
Y = input(title="Source", defval=close)

sumX = cum(X)
sumY = cum(Y)
sumX2 = cum(pow(X, 2))
sumY2 = cum(pow(Y, 2))
sumXY = cum(X * Y)

// Pearson correlation coefficient
r = (n * sumXY - sumX * sumY) / sqrt((n * sumY2 - pow(sumY, 2)) * (n * sumX2 - pow(sumX, 2)))

// Coefficient of determination
r2 = pow(r, 2)

meanX = sumX / n
meanY = sumY / n

slope = (n * sumXY - sumX * sumY) / (n * sumX2 - pow(sumX, 2))
intercept = meanY - slope * meanX

int startIndex = n > MAX_BARS_BACK ? int(n - MAX_BARS_BACK) : 0
startY = slope * startIndex + intercept
endY = slope * (n - 1) + intercept

var line regLine = na
if barstate.islast
    if na(regLine)
        regLine := line.new(startIndex, startY, bar_index, endY, width=2, extend=extend.none, color=color.orange)
    else
        line.set_xy1(regLine, startIndex, startY)
        line.set_xy2(regLine, bar_index, endY)
        na

//------------------------------------------------------------------------

// Formatter
_format(value, decimals) =>
    out = tostring(value, "#.#")
    out :=
         decimals == 2 ? tostring(value, "#.##") :
         decimals == 3 ? tostring(value, "#.###") :
         decimals == 4 ? tostring(value, "#.####") :
         decimals == 5 ? tostring(value, "#.#####") :
         decimals == 6 ? tostring(value, "#.######") :
         decimals == 7 ? tostring(value, "#.#######") :
         decimals == 8 ? tostring(value, "#.########") :
         decimals == 9 ? tostring(value, "#.#########") :
         decimals == 10 ? tostring(value, "#.##########") :
         decimals == 11 ? tostring(value, "#.###########") :
         decimals == 12 ? tostring(value, "#.############") :
         decimals == 13 ? tostring(value, "#.#############") :
         decimals == 14 ? tostring(value, "#.##############") :
         decimals == 15 ? tostring(value, "#.###############") :
         decimals == 16 ? tostring(value, "#.################") :
         out
    out

summaryText =
     " Slope: " + _format(slope, 6) +
     "\n Intercept: " + _format(intercept, 6) +
     "\n r: " + _format(r, 3) +
     "\n r2: " + _format(r2, 3)

var label summaryLabel = label.new(
     x=time,
     y=na,
     xloc=xloc.bar_time,
     text="",
     textalign=text.align_left,
     textcolor=color.white,
     color=#1e90ff,
     style=label.style_label_left,
     size=size.small
     )

label.set_text(summaryLabel, summaryText)
label.set_xy(summaryLabel, time + 10 * (time - time[1]), endY)

Overview

Linear Regression (All Data) draws one orange regression line on the price chart and refreshes that line on the last bar. A label reports the slope, intercept, Pearson correlation, and coefficient of determination. The only setting chooses the source series, which defaults to close. It does not generate entries, exits, or alerts.

Details

Published by

everget on GitHub

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

Trend

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pinepinescripttechnical-indicatorsindicatorsoscillatorbandsaveragetradingviewindicatormoving-average

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
SourcesourcecloseY
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

575 stars

This indicator detects equal highs and lows by comparing prices within user-defined left and right bars using a tolerance percentage. It plots horizontal lines on the price chart for equal highs in one color and lows in another. A screener allows input of mult

Indicator v5 Support & resistanceTrend
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows with Screener
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator calculates an ATR trailing stop line derived from the highest high minus an average true range multiple over a specified lookback period. It plots the trailing stop line directly on the price chart and colors price bars based on whether the clos

Indicator v5 TrendVolatility & bands
Updated 2 months ago · MIT GitHub repository of ATR Trailing Stoploss

All Candlestick Patterns

by shunjizhan
292 stars

All Candlestick Patterns detects and highlights a wide range of bullish, bearish, and neutral candlestick patterns directly on the price chart. It features optional trend filtering using either a 50-period simple moving average or a combination of 50-period an

Indicator v4 Candlestick patternsTrend
Updated 3 years ago · None stated GitHub repository of All Candlestick Patterns

NVDA Trend Ribbon

by longsurf-ai
277 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated yesterday · MIT GitHub repository of NVDA Trend Ribbon

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers