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

//@version=5
indicator(title="McGinley Dynamic x Donchian Channels", shorttitle='McGxD Channels', overlay=true, timeframe='', timeframe_gaps=true)
len_hc = input.int(14, minval=1)
src = input.source(ohlc4, title='Source')
hc_lower = ta.lowest(len_hc)
hc_upper = ta.highest(len_hc)

hc_u = 0.0
hc_l = 0.0
hc_u := na(hc_u[1]) ? ta.ema(hc_upper, len_hc) : hc_u[1] + (hc_upper - hc_u[1]) / (len_hc * math.pow(hc_upper/hc_u[1], 4))
hc_l := na(hc_l[1]) ? ta.ema(hc_lower, len_hc) : hc_l[1] + (hc_lower - hc_l[1]) / (len_hc * math.pow(hc_lower/hc_l[1], 4))
basis = math.avg(hc_u, hc_l)

dev_1 = 1 * ta.stdev(src, len_hc)
dev_2 = 2 * ta.stdev(src, len_hc)
dev_3 = 3 * ta.stdev(src, len_hc)

upper_1 = hc_u + dev_1
upper_2 = hc_u + dev_2
upper_3 = hc_u + dev_3

lower_1 = hc_l - dev_1
lower_2 = hc_l - dev_2
lower_3 = hc_l - dev_3

plot(hc_u, color=color.green, linewidth=3)
plot(upper_1, color=color.lime, linewidth=2)
plot(upper_2, color=color.lime, linewidth=2)
plot(upper_3, color=color.lime, linewidth=2)
plot(hc_l, color=color.red, linewidth=3)
plot(lower_1, color=color.orange, linewidth=2)
plot(lower_2, color=color.orange, linewidth=2)
plot(lower_3, color=color.orange, linewidth=2)
plot(basis, color=color.white, linewidth=3)
