Indicator · Pine Script v6 · On the chart

Donchian Channels (DCHANNEL)

by mihakralj

The Donchian Channels indicator plots the middle basis line as the average of the high and low over the user-defined period.

View on GitHub

GitHub stars47on the repository
Pine versionv6current
Lines of code50
Settings3inputs
LicenceMIT
Last update8 months ago28 Feb 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

50 lines of Pine Script v6 from indicators/channels/dchannel.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

dchannel.pine
// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Donchian Channels (DCHANNEL)", "DCHANNEL", overlay=true)

//@function Calculates the Donchian Channel (DC) efficiently using monotonic deques
//@param hi Source series for the highest high calculation (usually high)
//@param lo Source series for the lowest low calculation (usually low)
//@param p Lookback period (p > 0)
//@returns Tuple containing [basis, upper_band, lower_band]
//@optimized Uses monotonic deque for O(1) amortized complexity per bar
dchannel(series float hi, series float lo, simple int p) =>
    if p <= 0
        runtime.error("Period must be > 0")
    var float[] hbuf = array.new_float(p, na)
    var float[] lbuf = array.new_float(p, na)
    var int[] hq = array.new_int()
    var int[] lq = array.new_int()
    int idx = bar_index % p
    array.set(hbuf, idx, hi)
    array.set(lbuf, idx, lo)
    while array.size(hq) > 0 and array.get(hq, 0) <= bar_index - p
        array.shift(hq)
    while array.size(hq) > 0 and array.get(hbuf, array.get(hq, -1) % p) <= hi
        array.pop(hq)
    array.push(hq, bar_index)
    while array.size(lq) > 0 and array.get(lq, 0) <= bar_index - p
        array.shift(lq)
    while array.size(lq) > 0 and array.get(lbuf, array.get(lq, -1) % p) >= lo
        array.pop(lq)
    array.push(lq, bar_index)
    float top = array.get(hbuf, array.get(hq, 0) % p)
    float bot = array.get(lbuf, array.get(lq, 0) % p)
    [math.avg(top, bot), top, bot]

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

// Inputs
i_period = input.int(20, "Period", minval=1)
i_high = input.source(high, "High Source")
i_low = input.source(low, "Low Source")

// Calculation
[basis, upper, lower] = dchannel(i_high, i_low, i_period)

// Plot
plot(basis, "Basis", color=color.yellow, linewidth=2)
p1 = plot(upper, "Upper", color=color.yellow, linewidth=2)
p2 = plot(lower, "Lower", color=color.yellow, linewidth=2)
fill(p1, p2, color=color.new(color.blue, 90), title="Band Fill")

Overview

Donchian Channels (DCHANNEL) also draws the upper band as the highest high and the lower band as the lowest low across the same period on the price chart. The main setting changes the channel width by adjusting the period length. It draws on the price chart with a fill between the upper and lower bands.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Volatility & bands, Support & resistance, Breakout

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

pinescriptpinescript-indicatorstechnical-analysistradingview-indicators

Settings

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

SettingTypeDefaultVariable
Periodint20i_period
High Sourcesourcehighi_high
Low Sourcesourcelowi_low
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

Donchian Channels (DC)

by mihakralj
99 stars

This indicator calculates and plots Donchian Channels directly on the price chart. It determines the highest high and lowest low over a user-selected lookback period using an optimized monotonic deque algorithm, as well as a middle basis line computed as their

Indicator v6 Volatility & bandsSupport & resistance
Updated 4 weeks ago · Apache-2.0 GitHub repository of Donchian Channels (DC)
43 stars

This indicator plots Donchian channel bands and a midline on the price chart. It finds the highest and lowest values over a user-set length, then draws the upper band, lower band, and their average as the basis. The source setting chooses high and low, or the

Indicator v4 Volatility & bandsBreakout
Updated 1 year ago · None stated GitHub repository of Donchian Channels Experiment

Zen ABR Against

by Zen-Tim
29 stars

This overlay indicator measures average bar range as a simple moving average of high minus low, then draws short horizontal ticks at one to four times that range above and below the close. The user chooses which multipliers appear, their colors, and the line w

Indicator v6 Volatility & bandsSupport & resistance
Updated 2 weeks ago · MIT GitHub repository of Zen ABR Against

The Market Tradability Engine calculates multi-factor tradability, balance, compression, and direction scores to evaluate market regime quality. It classifies market conditions into eight distinct states, including Balance, Compression, Ready, Breakout, Trend,

Indicator v6 TrendVolatility & bands
Updated yesterday · MIT GitHub repository of Market Tradability Engine [WavesUnchained]

ICT Liquidity Void Fill

by ArunKBhaskar
575 stars

This overlay indicator marks ICT liquidity void fills on the price chart and can show long and short signals. It places a Fibonacci level inside a displacement candle, defaulting to the midpoint, and draws lines in a chosen style. Optional filters limit the fi

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Liquidity Void Fill

[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

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