Indicator · Pine Script v4 · On the chart

[RS]Donchian Fans

by ricardosantos79· code credited to RicardoSantos

This indicator plots Donchian Channel boundary lines along with dynamic fan projections directly on the price chart.

View on GitHub

GitHub stars261on the repository
Pine versionv4older, still runs
Lines of code82
Settings1inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago13 Nov 2021

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

82 lines of Pine Script v4 from [RS]Donchian Fans.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

[RS]Donchian Fans.pinescript
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=4
study(title="[RS]Donchian Fans", shorttitle="DF", overlay=true)
//  Initiate bars:
var int b = -1, b := b + 1
//----------------------------------------------------------------------------------
//  Required functions:
f_highest_bar(_source, _bars, _length)=>
    float _h_value = _source
    int _h_bar = _bars
    for _i = 0 to max(_length-1, 0)
        bool _isSourceOver = _source[_i] > _h_value
        if _isSourceOver
            _h_value := _source[_i]
            _h_bar := _bars[_i]
    [_h_value, _h_bar]

f_lowest_bar(_source, _bars, _length)=>
    float _l_value = _source
    int _l_bar = _bars
    for _i = 0 to max(_length-1, 0)
        bool _isSourceUnder = _source[_i] < _l_value
        if _isSourceUnder
            _l_value := _source[_i]
            _l_bar := _bars[_i]
    [_l_value, _l_bar]

f_line(_x1, _y1, _r, _color)=>
    var line _li = na
    line.delete(id=_li)
    _li := line.new(x1=_x1-1, y1=_y1 - _r, x2=_x1, y2=_y1, xloc=xloc.bar_index, extend=extend.right, color=_color, style=line.style_dashed, width=1)
//----------------------------------------------------------------------------------

int length = input(defval=100, title='Channel length:', minval=1)

[h_price, h_bar] = f_highest_bar(high, b, length)
[l_price, l_bar] = f_lowest_bar(low, b, length)

float mid = (h_price + l_price) / 2

plot(series=h_price, title='H', color=color.black, linewidth=1, style=plot.style_line, transp=20)
plot(series=mid, title='M', color=color.black, linewidth=1, style=plot.style_line, transp=20)
plot(series=l_price, title='L', color=color.black, linewidth=1, style=plot.style_line, transp=20)

var float r = atr(length)
if change(mid) != 0
    r := atr(length)

var line h_1_04 = na
var line h_1_08 = na
var line h_1_16 = na
var line h_1_32 = na
var line h_1_64 = na

var line l_1_04 = na
var line l_1_08 = na
var line l_1_16 = na
var line l_1_32 = na
var line l_1_64 = na

bool is_new_h = h_price > h_price[1]
bool is_new_l = l_price < l_price[1]

vhb = valuewhen(high >= h_price, h_bar, 0)
vhp = valuewhen(high >= h_price, h_price, 0)
vlb = valuewhen(low <= l_price, l_bar, 0)
vlp = valuewhen(low <= l_price, l_price, 0)

if is_new_l
    h_1_04 := f_line(vhb, vhp, -r * (1. / 04.), color.black)
    h_1_08 := f_line(vhb, vhp, -r * (1. / 08.), color.black)
    h_1_16 := f_line(vhb, vhp, -r * (1. / 16.), color.black)
    h_1_32 := f_line(vhb, vhp, -r * (1. / 32.), color.black)
    h_1_64 := f_line(vhb, vhp, -r * (1. / 64.), color.black)
if is_new_h
    l_1_04 := f_line(vlb, vlp, r * (1. / 04.), color.black)
    l_1_08 := f_line(vlb, vlp, r * (1. / 08.), color.black)
    l_1_16 := f_line(vlb, vlp, r * (1. / 16.), color.black)
    l_1_32 := f_line(vlb, vlp, r * (1. / 32.), color.black)
    l_1_64 := f_line(vlb, vlp, r * (1. / 64.), color.black)

Overview

[RS]Donchian Fans calculates the highest high, lowest low, and median price over a customizable lookback period. Whenever price establishes a new channel high or low, it projects a series of dashed fan lines extending to the right using fractional offsets of the Average True Range (ATR). Traded levels update dynamically as new price extremes occur across the chosen channel length.

Details

Published by

ricardosantos79 on GitHub

Code credit

RicardoSantos, as written in the script's header

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

Support & resistance, Trend, Volatility & bands

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
Channel length:int100length
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

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

ATR Trailing Stop

by mihakralj
99 stars

Calculates an ATR-based trailing stop overlaid on the price chart to assist with trend identification and stop placement. Using a specified ATR period and multiplier, it projects upper and lower bounds around the closing price. The stop line ratchets up during

Indicator v5 TrendVolatility & bands
Updated 4 weeks ago · Apache-2.0 GitHub repository of ATR Trailing Stop

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