The Killer Whale - Multiple Keltner Channels by Joe
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © 2022 crypto-with-joe

//@version=5
indicator(title="The Killer Whale - Multiple Keltner Channels by Joe", shorttitle="The Killer Whale v1.0", overlay=true, timeframe="", timeframe_gaps=true)

// Keltner Channel Settings
kw_kelt_length = input.int(20, "Length", minval=1)
kw_kelt_src    = input(close, "Source")
kw_kelt_mult1  = input.float(1.0, "Channel 1 Multiplier")
kw_kelt_mult2  = input.float(2.0, "Channel 2 Multiplier")
kw_kelt_mult3  = input.float(3.0, "Channel 3 Multiplier")
kw_kelt_mult4  = input.float(4.0, "Channel 4 Multiplier")
kw_kelt_exp    = input.bool(true, "Use Exponential MA")
kw_kelt_bands_style  = input.string("Average True Range", options = ["Average True Range", "True Range", "Range"], title="Bands Style")
kw_atr_length  = input.int(20, "ATR Length", tooltip = "Typically, when the Keltner Length is 20, the ATR length is either 10 or 20.")

// Style Settings
kw_kelt_1_show       = input.bool(true, "Show Channel 1", group = "Channel Styles")
kw_kelt_upper1_color = input.color(#ffffff50, "Channel 1 Colors - Upper", group = "Channel Styles", inline = "Channel 1a")
kw_kelt_lower1_color = input.color(#ffffff50, "Lower", group = "Channel Styles", inline = "Channel 1a")
kw_kelt_bg1_color    = input.color(#ffffff20, "Background", group = "Channel Styles", inline = "Channel 1b")

kw_kelt_2_show       = input.bool(true, "Show Channel 2", group = "Channel Styles")
kw_kelt_upper2_color = input.color(#ffdf0050, "Channel 2 Colors - Upper", group = "Channel Styles", inline = "Channel 2a")
kw_kelt_lower2_color = input.color(#ffdf0050, "Lower", group = "Channel Styles", inline = "Channel 2a")
kw_kelt_bgu2_color   = input.color(#ffdf0020, "Upper Background", group = "Channel Styles", inline = "Channel 2b")
kw_kelt_bgl2_color   = input.color(#ffdf0020, "Lower Background", group = "Channel Styles", inline = "Channel 2b")

kw_kelt_3_show       = input.bool(true, "Show Channel 3", group = "Channel Styles")
kw_kelt_upper3_color = input.color(#2962ff50, "Channel 3 Colors - Upper", group = "Channel Styles", inline = "Channel 3a")
kw_kelt_lower3_color = input.color(#2962ff50, "Lower", group = "Channel Styles", inline = "Channel 3a")
kw_kelt_bgu3_color   = input.color(#2962ff20, "Upper Background", group = "Channel Styles", inline = "Channel 3b")
kw_kelt_bgl3_color   = input.color(#2962ff20, "Upper Background", group = "Channel Styles", inline = "Channel 3b")

kw_kelt_4_show       = input.bool(true, "Show Channel 4", group = "Channel Styles")
kw_kelt_upper4_color = input.color(#ff525250, "Channel 4 Colors - Upper", group = "Channel Styles", inline = "Channel 4a")
kw_kelt_lower4_color = input.color(#ff525250, "Lower", group = "Channel Styles", inline = "Channel 4a")
kw_kelt_bgu4_color   = input.color(#ff525220, "Upper Background", group = "Channel Styles", inline = "Channel 4b")
kw_kelt_bgl4_color   = input.color(#ff525220, "Upper Background", group = "Channel Styles", inline = "Channel 4b")

// Variables
kw_kelt_1_display    = kw_kelt_1_show == true ? display.all : display.none
kw_kelt_2_display    = kw_kelt_2_show == true ? display.all : display.none
kw_kelt_3_display    = kw_kelt_3_show == true ? display.all : display.none
kw_kelt_4_display    = kw_kelt_4_show == true ? display.all : display.none

esma(kw_kelt_src, kw_kelt_length)=>
	s = ta.sma(kw_kelt_src, kw_kelt_length)
	e = ta.ema(kw_kelt_src, kw_kelt_length)
	kw_kelt_exp ? e : s

kw_ma = esma(kw_kelt_src, kw_kelt_length)

kw_range_ma = kw_kelt_bands_style == "True Range" ? ta.tr(true) : kw_kelt_bands_style == "Average True Range" ? ta.atr(kw_atr_length) : ta.rma(high - low, kw_kelt_length)

kw_kelt_upper1 = kw_ma + kw_range_ma * kw_kelt_mult1
kw_kelt_lower1 = kw_ma - kw_range_ma * kw_kelt_mult1
kw_kelt_upper2 = kw_ma + kw_range_ma * kw_kelt_mult2
kw_kelt_lower2 = kw_ma - kw_range_ma * kw_kelt_mult2
kw_kelt_upper3 = kw_ma + kw_range_ma * kw_kelt_mult3
kw_kelt_lower3 = kw_ma - kw_range_ma * kw_kelt_mult3
kw_kelt_upper4 = kw_ma + kw_range_ma * kw_kelt_mult4
kw_kelt_lower4 = kw_ma - kw_range_ma * kw_kelt_mult4

// Plots

kw_kelt_u1 = plot(kw_kelt_upper1, color = kw_kelt_upper1_color, title = "Keltner 1 Upper", display = kw_kelt_1_display, editable = false)
kw_kelt_l1 = plot(kw_kelt_lower1, color = kw_kelt_lower1_color, title = "Keltner 1 Lower", display = kw_kelt_1_display, editable = false)
kw_kelt_u2 = plot(kw_kelt_upper2, color = kw_kelt_upper2_color, title = "Keltner 2 Upper", display = kw_kelt_2_display, editable = false)
kw_kelt_l2 = plot(kw_kelt_lower2, color = kw_kelt_lower2_color, title = "Keltner 2 Lower", display = kw_kelt_2_display, editable = false)
kw_kelt_u3 = plot(kw_kelt_upper3, color = kw_kelt_upper3_color, title = "Keltner 3 Upper", display = kw_kelt_3_display, editable = false)
kw_kelt_l3 = plot(kw_kelt_lower3, color = kw_kelt_lower3_color, title = "Keltner 3 Lower", display = kw_kelt_3_display, editable = false)
kw_kelt_u4 = plot(kw_kelt_upper4, color = kw_kelt_upper4_color, title = "Keltner 4 Upper", display = kw_kelt_4_display, editable = false)
kw_kelt_l4 = plot(kw_kelt_lower4, color = kw_kelt_lower4_color, title = "Keltner 4 Lower", display = kw_kelt_4_display, editable = false)

fill(kw_kelt_u1, kw_kelt_l1, color = kw_kelt_bg1_color,  title = "Channel 1 Background",       display = kw_kelt_1_display, editable = false)
fill(kw_kelt_u2, kw_kelt_u1, color = kw_kelt_bgu2_color, title = "Channel 2 Upper Background", display = kw_kelt_2_display, editable = false)
fill(kw_kelt_l1, kw_kelt_l2, color = kw_kelt_bgl2_color, title = "Channel 2 Lower Background", display = kw_kelt_2_display, editable = false)
fill(kw_kelt_u3, kw_kelt_u2, color = kw_kelt_bgu3_color, title = "Channel 3 Upper Background", display = kw_kelt_3_display, editable = false)
fill(kw_kelt_l2, kw_kelt_l3, color = kw_kelt_bgl3_color, title = "Channel 3 Lower Background", display = kw_kelt_3_display, editable = false)
fill(kw_kelt_u4, kw_kelt_u3, color = kw_kelt_bgu4_color, title = "Channel 4 Upper Background", display = kw_kelt_4_display, editable = false)
fill(kw_kelt_l3, kw_kelt_l4, color = kw_kelt_bgl4_color, title = "Channel 4 Lower Background", display = kw_kelt_4_display, editable = false)

plot(kw_ma, style = plot.style_line, linewidth = 1, color = #ffffffff, title = "Basis")
