You are on page 1of 4

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

//----------------------------------------------------------------------------------

//----------------------------------------------------------------------------------

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

bool show_channel = input(true)

bool show_fan_from_low = input(true)

bool show_fan_from_high = input(true)

bool show_price_levels_from_low = input(true)

bool show_price_levels_from_high = input(true)

//----------------------------------------------------------------------------------

// calculate donchian channel:

[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=not show_channel ? na : h_price, title='H', color=color.black, linewidth=1, style=plot.style_line, transp=20)

plot(series=not show_channel ? na : mid, title='M', color=color.black, linewidth=1, style=plot.style_line, transp=20)

plot(series=not show_channel ? na : l_price, title='L', color=color.black, linewidth=1, style=plot.style_line, transp=20)

//----------------------------------------------------------------------------------

// Calculate ATR when makes a new donchian high/low

var float r = atr(length)

if change(mid) != 0

r := atr(length)

//----------------------------------------------------------------------------------

// lines variables:

// fan from highs

var line h_1_02 = na

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

// fan from lows

var line l_1_02 = 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

// price levels from highs

var line h_0000 = na

var line h_0382 = na

var line h_0500 = na

var line h_0618 = na

var line h_1000 = na

var line h_1618 = na

// price levels from lows

var line l_0000 = na

var line l_0382 = na

var line l_0500 = na

var line l_0618 = na

var line l_1000 = na

var line l_1618 = na

bool is_new_high = h_price > h_price[1]

bool is_new_low = 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)

// fan from highs when making new lows

if show_fan_from_high

if is_new_low

h_1_02 := f_line(vhb, vhp, -r * (1. / 02.), color.maroon)

h_1_04 := f_line(vhb, vhp, -r * (1. / 04.), color.navy)

h_1_08 := f_line(vhb, vhp, -r * (1. / 08.), color.navy)

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)

// fan from lows when making new highs


if show_fan_from_low

if is_new_high

l_1_02 := f_line(vlb, vlp, r * (1. / 02.), color.maroon)

l_1_04 := f_line(vlb, vlp, r * (1. / 04.), color.navy)

l_1_08 := f_line(vlb, vlp, r * (1. / 08.), color.navy)

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)

if show_price_levels_from_high

if is_new_low

h_0000 := f_line(vhb, vhp, 0, color.maroon)

h_0382 := f_line(vhb, vhp - (vhp-vlp) * 0.382, 0, color.navy)

h_0500 := f_line(vhb, vhp - (vhp-vlp) * 0.500, 0, color.navy)

h_0618 := f_line(vhb, vhp - (vhp-vlp) * 0.618, 0, color.black)

h_1000 := f_line(vhb, vhp - (vhp-vlp) * 1.000, 0, color.black)

h_1618 := f_line(vhb, vhp - (vhp-vlp) * 1.618, 0, color.black)

if show_price_levels_from_low

if is_new_high

l_0000 := f_line(vlb, vlp, 0, color.maroon)

l_0382 := f_line(vlb, vlp + (vhp-vlp) * 0.382, 0, color.navy)

l_0500 := f_line(vlb, vlp + (vhp-vlp) * 0.500, 0, color.navy)

l_0618 := f_line(vlb, vlp + (vhp-vlp) * 0.618, 0, color.black)

l_1000 := f_line(vlb, vlp + (vhp-vlp) * 1.000, 0, color.black)

l_1618 := f_line(vlb, vlp + (vhp-vlp) * 1.618, 0, color.black)

You might also like