Indicator · Pine Script v6 · On the chart

Donchian Channels (DC)

by mihakralj

This indicator calculates and plots Donchian Channels directly on the price chart.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code50
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

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

dc.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Donchian Channels (DC)", "DC", 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
dc(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] = dc(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 (DC) 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 average. The script draws upper, lower, and basis lines while filling the background area between the channel bounds. Traders can customize the period length and price source inputs.

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

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
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

47 stars

The Donchian Channels indicator plots the middle basis line as the average of the high and low over the user-defined period. It 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 ma

Indicator v6 Volatility & bandsSupport & resistance
Updated 8 months ago · MIT GitHub repository of Donchian Channels (DCHANNEL)
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

This indicator identifies and plots price compression zones directly on the price chart using multi-module scoring. It measures ATR compression, ADX chop levels, boundary touches, volume dry-up, and historical pivot levels to identify consolidation areas and s

Indicator v6 BreakoutSupport & resistance
Updated yesterday · MIT GitHub repository of CoilForge Zones [WavesUnchained]

Dereköy-Darvas

by Kalumur
4 stars

The Dereköy-Darvas indicator combines Darvas Box boundary levels with dual Bollinger Bands plotted directly on the main price chart. It calculates top and bottom box lines based on recent high and low extremes to outline price consolidation zones. Simultaneous

Indicator v5 Volatility & bandsSupport & resistance
Updated 2 years ago · None stated GitHub repository of Dereköy-Darvas
3 stars

This overlay indicator plots ATR-based horizontal levels around the previous close of a selected higher timeframe. A trading-type input maps day, multiday, swing, position, and long-term modes to daily through yearly periods. It draws trigger bands from a perc

Indicator v5 Volatility & bandsSupport & resistance Repaints
Updated 5 months ago · None stated GitHub repository of ATR Levels + CALL/PUT

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