Indicator · Pine Script v5 · On the chart

Donchian Channel

by JoeTonDev· code credited to //@version=5

An overlay that plots a Donchian channel from the prior highest high and lowest low over an adjustable length, with an optional midline and fill, plus a fast and a slow EMA.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code75
Settings8inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago11 Aug 2024

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

75 lines of Pine Script v5 from indicators/donchianchannel.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

donchianchannel.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © 

//@version=5
indicator("Donchian Channel", overlay = true)

// Step 1. Script settings
// Channel settings
fastMaLen = input.int(25, title = "Moving Average Length", minval = 1)
slowMaLen = input.int(350, title = "Moving Average Length", minval = 1)

rsiLen = input.int(14, title = "RSI Length", minval = 1)

dcLen = input.int(20, title = "Donchian Channel Length", minval = 2)

showMid = input.bool(false, title="Show Midline?")
fillDc = input.bool(false, title="Fill Donchian Channel?")

brekOutFilter = input.int(0, title = "Breakout Filter (Ticks)", minval = 0) * syminfo.mintick

showBreakouts = input.bool(true, title="Show Breakouts?")

// Step 2. Calculate indicator values
hiHighs = ta.highest(high, dcLen)[1]
loLows = ta.lowest(low, dcLen)[1]

midPoint = (hiHighs + loLows) / 2

fastMa = ta.ema(close, fastMaLen)
slowMa = ta.ema(close, slowMaLen)

upTrend = fastMa > slowMa
downTrend = fastMa < slowMa

rsi = ta.rsi(close, rsiLen)
rsiCrossover = ta.crossover(rsi, 70)
rsiCrossunder = ta.crossunder(rsi, 30)

// Step 3. Determine indicator signals
// See if prices crossed above/below Donchian Channel
upBrkOut = barstate.isconfirmed and ta.crossover(close, hiHighs + brekOutFilter) and close > fastMa and upTrend and rsiCrossover
downBrkOut = barstate.isconfirmed and ta.crossunder(close, loLows - brekOutFilter) and close < fastMa and downTrend and rsiCrossunder

// Step 4. Output indicator data
// Plot Donchian Channel on the chart
ub = plot(hiHighs, color=color.green, title=" Donchian UB")
lb = plot(loLows, color=color.red, title=" Donchian LB")

// Fill background between Donchian bands
fill(plot1=ub, plot2=lb, color=fillDc ? color.new(#4169e1, 95) : 
      color.new(color.white, 100))

// Show midline
plot(showMid ? midPoint : na, color=color.orange, title="Midline")

// Highlight breakouts with background color
highlightColour = not showBreakouts ? na :
     upBrkOut ? color.new(color.green, 80) :
     downBrkOut ? color.new(color.red, 80) :
     na

bgcolor(highlightColour)

plot(fastMa, color=color.blue, title="Moving Average")
plot(slowMa  )

// Step 5. Create indicator alerts
alertcondition(condition=upBrkOut, title="Long entry", message = "Price closed above Donchian Channel")

alertcondition(condition=downBrkOut, title="Short entry", message="Price closed below Donchian Channel")

if (upBrkOut)
    alert("Long Entry", alert.freq_once_per_bar_close)
else if (downBrkOut)
    alert("Short Entry", alert.freq_once_per_bar_close)

Overview

Donchian Channel highlights a breakout bar when the close crosses the channel, sits on the matching side of the fast EMA, the fast EMA is on the matching side of the slow one, and RSI has just crossed 70 or 30. A tick filter can widen the breakout test. Alerts fire on confirmed bars.

Details

Published by

JoeTonDev on GitHub

Code credit

//@version=5, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Breakout, 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.

Settings

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

SettingTypeDefaultVariable
Moving Average Lengthint25fastMaLen
Moving Average Lengthint350slowMaLen
RSI Lengthint14rsiLen
Donchian Channel Lengthint20dcLen
Show Midline?boolfalseshowMid
Fill Donchian Channel?boolfalsefillDc
Breakout Filter (Ticks)int0brekOutFilter
Show Breakouts?booltrueshowBreakouts
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

Sideway Zones Live

by dearvn
257 stars

Sideway Zones Live is an overlay indicator that estimates inner and outer sideways bands from recent highs and lows with a rational quadratic kernel. It tracks swing pivots over a loopback period and counts bars that remain between the outer high and low. Afte

Indicator v5 TrendSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of Sideway Zones Live
114 stars

This indicator adapts Michael Chalek's Dual Thrust trading concept to plot breakout levels and overlay bands directly on the price chart. It calculates lookback ranges based on historical price action and provides two algorithmic variations along with multiple

Indicator v4 TrendBreakout Repaints
Updated 1 year ago · None stated GitHub repository of Dual Thrust Trading Algorithm
56 stars

This indicator calculates dynamic upper and lower trendlines from pivot highs and pivot lows across a specified bar length. Trendline slopes are derived using ATR, standard deviation, or linear regression methods. It plots trendlines and breakout signal labels

Indicator v5 TrendSupport & resistance Repaints
Updated 2 months ago · None stated GitHub repository of Trendlines With Breaks [LUX]

Reversal Breakout System

by TamaraKGreen
46 stars

This indicator detects inside candles inside larger parent candles on the price chart. It plots colored boxes around inside and parent candles, fills them with user-adjustable transparency, draws Fibonacci retracement lines and mother candle range lines, and d

Indicator v6 Candlestick patternsBreakout
Updated 7 weeks ago · None stated GitHub repository of Reversal Breakout System

RSC Trend Trading Study

by readysetcryptocodes
43 stars

This indicator plots Donchian Channels alongside fast and slow simple moving averages directly on the price chart. It generates bullish breakout signals when the high reaches the upper channel while the close is above the 50 SMA (and optionally the 200 SMA). B

Indicator v4 TrendBreakout
Updated 2 years ago · None stated GitHub repository of RSC Trend Trading Study

Candle Stage Analysis

by ChartsLector
41 stars

This indicator classifies market trends into structured stages based on moving average alignments, compression, and ATR distance from the 50-period SMA. It tracks multiple exponential moving averages against the 50 SMA to detect basing phases, breakouts, trend

Indicator v6 TrendMoving averages
Updated 3 days ago · None stated GitHub repository of Candle Stage Analysis

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