Advanced Donchian Channels
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SamRecio

//@version=5
indicator("Advanced Donchian Channels", shorttitle = "[ADC]",overlay = true, max_lines_count = 500)

len = input.int(20, title ="Len", minval = 1, maxval = 166, group = "Advanced Donchian Channel")
fut_tog = input.bool(true, title = "Plot Future Lines?")
color1 = input.color(color.rgb(38,208,124), title = "", inline = "1", group = "Colors")
color2 = input.color(color.rgb(0,150,255), title = "", inline = "1", group = "Colors")
color3 = input.color(color.rgb(255,3,62), title = "", inline = "1", group = "Colors")

highst = ta.highest(high,len)
lowst = ta.lowest(low,len)
mean = math.avg(highst,lowst)

highbar = ta.barssince(high == highst)
lowbar = ta.barssince(low == lowst)

dir = highbar>lowbar?2:lowbar>highbar?1:0

var fut_lines = array.new_line(na)
if array.size(fut_lines) > 0
	for i = 0 to array.size(fut_lines) - 1
		line.delete(array.get(fut_lines, i))
array.clear(fut_lines)
if fut_tog
    for i = len to 2
        low1 = ta.lowest(low,i)
        low2 = ta.lowest(low,i-1)
        high1 = ta.highest(high,i)
        high2 = ta.highest(high,i-1)
        array.push(fut_lines,line.new(bar_index + (len-i), high1,(bar_index +(len-i))+ 1,high2, color = color1, style = line.style_dotted))
        array.push(fut_lines,line.new(bar_index + (len-i), math.avg(high1,low1),(bar_index +(len-i))+ 1,math.avg(high2,low2), color = color2, style = line.style_dotted))
        array.push(fut_lines,line.new(bar_index + (len-i), low1,(bar_index +(len-i))+ 1,low2, color = color3, style = line.style_dotted))

//PLOTS
plot(highst, title = "Highest", color = color1, linewidth = 2, editable = false)
plot(mean,title = "Mean", color = color2, linewidth = 1, editable = false)
plot(lowst, title = "Lowest", color = color3, linewidth = 2, editable = false)

//ALERTS////////////////////////////////////////////////////////////////////////
up = (close<=mean)[1] and (close>mean)
down = (close>=mean)[1] and (close<mean)

alertcondition(up, "Close Over Mean")
alertcondition(down, "Close Under Mean")
