Average Deviation Bands (ADB)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © peacefulLizard50262

//@version=5
indicator("Average Deviation Bands", overlay = true)

dev = input.int(20, "Length")

x1  = input.bool(true, "Band 1", inline = "x1")
x2  = input.bool(false, "Band 2", inline = "x2")
x3  = input.bool(false, "Band 3", inline = "x3")
xn  = input.bool(false, "Band 4", inline = "x4")
l1  = input.float(1 , "", 0, 100, 0.25, inline = "x1")
l2  = input.float(2 , "", 0, 100, 0.25, inline = "x2")
l3  = input.float(3 , "", 0, 100, 0.25, inline = "x3")
l4  = input.float(4 , "", 0, 100, 0.25, inline = "x4")

deviation = ta.stdev(close, dev)
average   = ta.sma(close, dev)

var float average_deviation_sum = na
var int average_deviation_counter = na

if close == close
    average_deviation_counter := nz(average_deviation_counter[1]) + 1
    average_deviation_sum := nz(average_deviation_sum[1]) + deviation

average_deviation = average_deviation_sum/average_deviation_counter

center = plot(average, "Average", color.orange)
x1_top = plot(x1 ? average + average_deviation * l1 : na, "x1 Top", #163adb)
x1_bot = plot(x1 ? average - average_deviation * l1 : na, "x1 Bot", #163adb)
x2_top = plot(x2 ? average + average_deviation * l2 : na, "x2 Top", #2342cc)
x2_bot = plot(x2 ? average - average_deviation * l2 : na, "x2 Bot", #2342cc)
x3_top = plot(x3 ? average + average_deviation * l3 : na, "x3 Top", #2e46af)
x3_bot = plot(x3 ? average - average_deviation * l3 : na, "x3 Bot", #2e46af)
xn_top = plot(xn ? average + average_deviation * l4 : na, "x3 Top", #5b2eaf)
xn_bot = plot(xn ? average - average_deviation * l4 : na, "x3 Bot", #5b2eaf)

fill(x1_top, x1_bot, color.new(#163adb, 95))
fill(x2_top, x2_bot, color.new(#2342cc, 95))
fill(x3_top, x3_bot, color.new(#2e46af, 95))
fill(xn_top, xn_bot, color.new(#5b2eaf, 95))