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

//@version=5
indicator("Donchian Channels (Close)", "DCC", overlay=true)

name1 = "Period 1"
period1 = input.int(10, "Period", minval=1, group=name1, inline=name1)
offset1 = input.int(0, "Box offset", minval=0, group=name1, inline=name1)
color1  = input.color(color.yellow, "Color", group=name1, inline=name1)
showl1  = input.bool(true, "Show line", group=name1, inline=name1+"line2")
showb1  = input.bool(true, "Show box", group=name1, inline=name1+"line2")
showc1  = input.bool(false, "Show channel", group=name1, inline=name1+"line2")
showm1  = input.bool(false, "Show momentum change level", group=name1, inline=name1+"line3")

name2 = "Period 2"
period2 = input.int(15, "Period", minval=1, group=name2, inline=name2)
offset2 = input.int(0, "Box offset", minval=0, group=name2, inline=name2)
color2  = input.color(color.red, "Color", group=name2, inline=name2)
showl2  = input.bool(true, "Show line", group=name2, inline=name2+"line2")
showb2  = input.bool(true, "Show box", group=name2, inline=name2+"line2")
showc2  = input.bool(false, "Show channel", group=name2, inline=name2+"line2")
showm2  = input.bool(false, "Show momentum change level", group=name2, inline=name2+"line3")

name3 = "Period 3"
period3 = input.int(21, "Period", minval=1, group=name3, inline=name3)
offset3 = input.int(0, "Box offset", minval=0, group=name3, inline=name3)
color3  = input.color(color.purple, "Color", group=name3, inline=name3)
showl3  = input.bool(true, "Show line", group=name3, inline=name3+"line2")
showb3  = input.bool(true, "Show box", group=name3, inline=name3+"line2")
showc3  = input.bool(false, "Show channel", group=name3, inline=name3+"line2")
showm3  = input.bool(true, "Show momentum change level", group=name3, inline=name3+"line3")

name4 = "Period 4"
period4 = input.int(63, "Period", minval=1, group=name4, inline=name4)
offset4 = input.int(0, "Box offset", minval=0, group=name4, inline=name4)
color4  = input.color(color.blue, "Color", group=name4, inline=name4)
showl4  = input.bool(true, "Show line", group=name4, inline=name4+"line2")
showb4  = input.bool(true, "Show box", group=name4, inline=name4+"line2")
showc4  = input.bool(false, "Show channel", group=name4, inline=name4+"line2")
showm4  = input.bool(true, "Show momentum change level", group=name4, inline=name4+"line3")


get_channel_levels(period, show, tcolor, offset=0) =>
    up = ta.highest(close, period)
    dn = ta.lowest(close, period)
    md = 0.5 * (dn + up)

    bb = box(na)
    ll = line(na)
    if show and barstate.islast
        bb := box.new(bar_index[period+offset-1], up[offset], bar_index[offset], dn[offset], border_color=tcolor, bgcolor=color.new(tcolor, 95))
        ll := line.new(bar_index[period+offset-1], md[offset], bar_index[offset], md[offset], color=tcolor)
        box.delete(bb[1])
        line.delete(ll[1])

    [dn, md, up]


[dn1, md1, up1] = get_channel_levels(period1, showb1, color1, offset1)
[dn2, md2, up2] = get_channel_levels(period2, showb2, color2, offset2)
[dn3, md3, up3] = get_channel_levels(period3, showb3, color3, offset3)
[dn4, md4, up4] = get_channel_levels(period4, showb4, color4, offset4)

display_noplot = display.data_window+display.price_scale+display.status_line

plot(md1, "Box 1 center", color=color1, linewidth=2, display=showl1 ? display.all : display_noplot)
plot(up1, "Box 1 top", color=color1, linewidth=1, display=showc1 ? display.all : display_noplot)
plot(dn1, "Box 1 bottom", color=color1, linewidth=1, display=showc1 ? display.all : display_noplot)
plot(close, "Box 1 momentum change level", color=color1, linewidth=1, offset=period1-1, display=showm1 ? display.all : display_noplot)

plot(md2, "Box 2 center", color=color2, linewidth=2, display=showl2 ? display.all : display_noplot)
plot(up2, "Box 2 top", color=color2, linewidth=1, display=showc2 ? display.all : display_noplot)
plot(dn2, "Box 2 bottom", color=color2, linewidth=1, display=showc2 ? display.all : display_noplot)
plot(close, "Box 2 momentum change level", color=color2, linewidth=1, offset=period2-1, display=showm2 ? display.all : display_noplot)

plot(md3, "Box 3 center", color=color3, linewidth=2, display=showl3 ? display.all : display_noplot)
plot(up3, "Box 3 top", color=color3, linewidth=1, display=showc3 ? display.all : display_noplot)
plot(dn3, "Box 3 bottom", color=color3, linewidth=1, display=showc3 ? display.all : display_noplot)
plot(close, "Box 3 momentum change level", color=color3, linewidth=1, offset=period3-1, display=showm3 ? display.all : display_noplot)

plot(md4, "Box 4 center", color=color4, linewidth=2, display=showl4 ? display.all : display_noplot)
plot(up4, "Box 4 top", color=color4, linewidth=1, display=showc4 ? display.all : display_noplot)
plot(dn4, "Box 4 bottom", color=color4, linewidth=1, display=showc4 ? display.all : display_noplot)
plot(close, "Box 4 momentum change level", color=color4, linewidth=1, offset=period4-1, display=showm4 ? display.all : display_noplot)
