Indicator · Pine Script v6 · On the chart

Regression Channels (REGCHANNEL)

by mihakralj

Regression Channels calculates a linear regression trend line and standard deviation channel bands over a specified lookback period.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code60
Settings3inputs
LicenceApache-2.0
Last update4 weeks ago27 Sep 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

60 lines of Pine Script v6 from lib/channels/regchannel/regchannel.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

regchannel.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Regression Channels (REGCHANNEL)", "REGCHANNEL", overlay=true)

//@function Calculates Regression Channels with parallel lines equidistant from a central linear regression line
//@param period Lookback period for regression calculation (period > 1)
//@param source Source series for regression calculation (usually close)
//@param multiplier Distance multiplier for channel bands (multiplier > 0)
//@returns Tuple containing [upper_band, regression_line, lower_band]
//@optimized Uses linear regression with O(n) complexity per bar
regchannel(simple int period, series float source = close, simple float multiplier = 2.0) =>
    if period <= 1
        runtime.error("Period must be > 1")
    if multiplier <= 0.0
        runtime.error("Multiplier must be > 0")
    float sumX = 0.0
    float sumY = 0.0
    float sumXY = 0.0
    float sumX2 = 0.0
    for i = 0 to period - 1
        float x = float(i)
        float y = source[period - 1 - i]
        sumX := sumX + x
        sumY := sumY + y
        sumXY := sumXY + x * y
        sumX2 := sumX2 + x * x
    float n = float(period)
    float slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX)
    float intercept = (sumY - slope * sumX) / n
    float currentX = float(period - 1)
    float regression = slope * currentX + intercept
    float sumResiduals2 = 0.0
    for i = 0 to period - 1
        float x = float(i)
        float y = source[period - 1 - i]
        float predicted = slope * x + intercept
        float residual = y - predicted
        sumResiduals2 := sumResiduals2 + residual * residual
    float stdDev = math.sqrt(sumResiduals2 / n)
    float upperBand = regression + multiplier * stdDev
    float lowerBand = regression - multiplier * stdDev
    [upperBand, regression, lowerBand]

// ---------- Main loop ----------

// Inputs
i_period = input.int(20, "Period", minval=2)
i_source = input.source(close, "Source")
i_multiplier = input.float(2.0, "Standard Deviation Multiplier", minval=0.1, step=0.1)

// Calculation
[upperBand, midLine, lowerBand] = regchannel(i_period, i_source, i_multiplier)

// Plot
p1 = plot(upperBand, "Upper Band", color=color.yellow, linewidth=2)
p2 = plot(midLine, "Regression Line", color=color.yellow, linewidth=2)
p3 = plot(lowerBand, "Lower Band", color=color.yellow, linewidth=2)
fill(p1, p2, color=color.new(color.red, 90), title="Upper Fill")
fill(p2, p3, color=color.new(color.green, 90), title="Lower Fill")

Overview

Regression Channels (REGCHANNEL) computes standard deviations from the residuals to form symmetrical upper and lower channel bands around the central regression fit. The script plots the middle line, upper band, and lower band directly on the price chart with transparent background fills. Users can configure the evaluation period, price source, and standard deviation multiplier.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Trend, Volatility & bands, Support & resistance

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-tradingcsharpdotnetpinescriptpythonquantitative-financesimdta-libtechnical-analysistechnical-indicators

Settings

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

SettingTypeDefaultVariable
Periodint20i_period
Sourcesourceclosei_source
Standard Deviation Multiplierfloat2.0i_multiplier
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

[RS]Donchian Fans

by ricardosantos79
261 stars

This indicator plots Donchian Channel boundary lines along with dynamic fan projections directly on the price chart. It calculates the highest high, lowest low, and median price over a customizable lookback period. Whenever price establishes a new channel high

Indicator v4 Support & resistanceTrend
Updated 4 years ago · None stated GitHub repository of [RS]Donchian Fans

Dynamic Linear Regression

by TradersPost
218 stars

Dynamic Linear Regression calculates and plots linear regression trendlines and standard deviation channels directly on the price chart over a user-selected time period. Traders can configure static channels locked at the end time, dynamic channels that contin

Indicator v6 TrendSupport & resistance
Updated 3 months ago · None stated GitHub repository of Dynamic Linear Regression

Moon Dev Lunar Sniper

by moondevonyt
92 stars

Moon Dev Lunar Sniper is a multi-indicator overlay that scores confluence across fast and slow EMAs, RSI, MACD, DMI, and BB width to fire signals only when score meets the minimum threshold. It plots buy and sell signals on the price chart, highlights cosmic b

Indicator v6 TrendOscillators & momentum Repaints
Updated 8 weeks ago · Custom licence GitHub repository of Moon Dev Lunar Sniper

SuperTrend & Pivots

by dcaoyuan
55 stars

This overlay indicator plots a SuperTrend line on the price chart using a fixed ATR length of 10 and a factor of 3. When the trend direction is up it draws a green line and a light green fill toward the candle midpoint; when down it draws a red line and a ligh

Indicator v6 TrendVolatility & bands
Updated 6 months ago · Apache-2.0 GitHub repository of SuperTrend & Pivots
47 stars

The Ultimate Channel calculates an ultra-smoothed centerline alongside upper and lower volatility bands directly on the main price chart. It processes true range values through an advanced infinite impulse response smoothing filter to establish dynamic channel

Indicator v6 Volatility & bandsTrend
Updated 8 months ago · MIT GitHub repository of Ultimate Channel (UCHANNEL)

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