Indicator · Pine Script v5 · On the chart

Linear Regression ++ [Dev Lucem]

by theonetrade· code credited to DevLucem

This overlay indicator calculates a multi-timeframe linear regression channel with customizable length, offset, and standard deviation multiplier.

View on GitHub

GitHub stars34on the repository
Pine versionv5runs as is
Lines of code88
Settings12inputs
LicenceMIT
Last update2 weeks ago8 Nov 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

88 lines of Pine Script v5 from assets/indicator/bit-exact/linear_regression_HaVyhZnP.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

linear_regression_HaVyhZnP.pine
//  (                   (                            
//  )\ )                )\ )                         
// (()/(     (    )    (()/(   (          (     )    
//  /(_))   ))\  /((    /(_)) ))\   (    ))\   (     
// (_))_   /((_)(_))\  (_))  /((_)  )\  /((_)  )\  ' 
//  |   \ (_))  _)((_) | |  (_))(  ((_)(_))  _((_))  
//  | |) |/ -_) \ V /  | |__| || |/ _| / -_)| '  \() 
//  |___/ \___|  \_/   |____|\_,_|\__| \___||_|_|_|  
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DevLucem

//@version=5
indicator('Linear Regression ++ [Dev Lucem]', 'Lin Reg ++ [DL]', overlay=true)


mtf_val = input.timeframe('', 'Resolution', inline="config", group="Algorithm Config")
source = input.source(close, inline="config", group="Algorithm Config")
length = input.int(100, minval=1, inline="config1", group="Algorithm Config")
dev = input(2.0, 'Deviation', inline="config1", group="Algorithm Config")
offset = input.int(0, minval=0, inline="config2", group="Algorithm Config")
smoothing = input.int(1, minval=1, inline="config2", group="Algorithm Config")


line_thick = input.int(4, 'S&R Size', minval=1, maxval=4, inline="levels", group="Display Config")
show_last = input.bool(true, "Hide Old Signals", group="Display Config")
p = input.color(color.lime, "Up", inline="colors", group="Display Config")
q = input.color(color.red, "Down", inline="colors", group="Display Config")
displacement = input.bool(false, "Show Displacement")
goto = input.time(timestamp("20 Jul 2050 00:00 +0300"), "End Of Calculating")

data(x) =>
    ta.sma(request.security(syminfo.tickerid, mtf_val != '' ? mtf_val : timeframe.period, x), smoothing)

linreg = data(ta.linreg(source, length, offset))
linreg_p = data(ta.linreg(source, length, offset + 1))
plot(linreg, 'Regression Line', linreg > linreg[1] ? p : q, editable=true)

x = bar_index
slope = linreg - linreg_p
intercept = linreg - x * slope
deviationSum = 0.0
for i = 0 to length - 1 by 1
    deviationSum += math.pow(source[i] - (slope * (x - i) + intercept), 2)
    deviationSum
deviation = math.sqrt(deviationSum / length)
x1 = x - length
x2 = x
y1 = slope * (x - length) + intercept
y2 = linreg

var line b = na
var line dp = na
var line dm = na

updating = goto >= time

if updating
    b := line.new(x1, y1, x2, y2, xloc.bar_index, extend.right, color.aqua, width=line_thick)
    if not displacement
        line.delete(b[1])
    dp := line.new(x1, deviation * dev + y1, x2, deviation * dev + y2, xloc.bar_index, extend.right, q, width=line_thick)
    if not displacement
        line.delete(dp[1])
    dm := line.new(x1, -deviation * dev + y1, x2, -deviation * dev + y2, xloc.bar_index, extend.right, p, width=line_thick)
    if not displacement
        line.delete(dm[1])

dm_current = -deviation * dev + y2
dp_current = deviation * dev + y2
buy = ta.crossunder(close, dm_current)
sell = ta.crossover(close, dp_current)
alertcondition(buy, 'Buy Lin Reg', 'Crossing On the Lower Regression Channel')
alertcondition(sell, 'Sell Lin Reg', 'Crossing On the Higher Regression Channel')

plotshape(buy, 'BUY', shape.labelup, location.belowbar, color.new(color.lime, 0), text='BUY', textcolor=color.new(color.black, 0), show_last=not show_last? 99999999 : length)
plotshape(sell, 'SELL', shape.labeldown, location.abovebar, color.new(color.red, 0), text='SELL', textcolor=color.new(color.white, 0), show_last=not show_last ? 99999999 : length)


//  (                   (                            
//  )\ )                )\ )                         
// (()/(     (    )    (()/(   (          (     )    
//  /(_))   ))\  /((    /(_)) ))\   (    ))\   (     
// (_))_   /((_)(_))\  (_))  /((_)  )\  /((_)  )\  ' 
//  |   \ (_))  _)((_) | |  (_))(  ((_)(_))  _((_))  
//  | |) |/ -_) \ V /  | |__| || |/ _| / -_)| '  \() 
//  |___/ \___|  \_/   |____|\_,_|\__| \___||_|_|_|  
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DevLucem

Overview

Linear Regression ++ [Dev Lucem] plots the linear regression line directly on the price chart along with upper and lower deviation channel lines. Buy labels trigger when price crosses below the lower channel line, while Sell labels trigger when price crosses above the upper channel line. Users can adjust inputs for chart resolution, price source, length, smoothing, deviation multiplier, line thickness, and display parameters.

Details

Published by

theonetrade on GitHub

Code credit

DevLucem, as written in the script's header

Draws

On the price chart

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

algorithmic-tradingbacktestingbitcoinccxtcrypto-tradingllm-agentoffline-backtestingopen-source-tradingpine-scriptpine-to-pythonpinescriptpynecorepythonquantquantitative-financestrategy-backtestingtechnical-analysistrading-indicatorstrading-strategiestradingview

Settings

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

SettingTypeDefaultVariable
Resolutiontimeframemtf_val
sourcesourceclosesource
lengthint100length
Deviationfloat2.0dev
offsetint0offset
smoothingint1smoothing
S&R Sizeint4line_thick
Hide Old Signalsbooltrueshow_last
Upcolorcolor.limep
Downcolorcolor.redq
Show Displacementboolfalsedisplacement
End Of Calculatingtimetimestamp("20 Jul 2050 00:00 +0300")goto
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

257 stars

This indicator plots auto trade signals for E-mini S&P 500 futures on the price chart. It computes an EMA200 filter along with day ETH RTH session levels and percentage-based entry zones. Signals trigger CALL or PUT cases when price crosses defined thresholds

Indicator v6 TrendSupport & resistance
Updated 2 months ago · None stated GitHub repository of Auto Trade Futures ES
5 stars

This indicator implements Tom DeMark's TD Sequential methodology on the price chart to detect trend exhaustion and reversal zones. It counts the Setup phase of nine consecutive bars comparing closes to four bars prior, tracks setup perfection, and advances int

Indicator v6 TrendMean reversion
Updated 1 year ago · None stated GitHub repository of TD Sequential - Official DeMark
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

Swing labels

by tradoor-terminal
320 stars

Calculates swing highs and swing lows on the price chart using pivot high and pivot low detection over a specified length. It places labels above pivot highs and below pivot lows to highlight key structural swing points. The indicator measures and displays the

Indicator v6 Support & resistanceTrend
Updated 3 days ago · AGPL-3.0 GitHub repository of Swing labels

More Trend scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers