Supertrend Channels [LuxAlgo]
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo

//@version=5
indicator("Supertrend Channels [LuxAlgo]",overlay=true,max_lines_count=500)
length = input(14)
mult   = input(2)
//------------------------------------------------------------------------------
upper = 0.,lower = 0.,os = 0,max = 0.,min = 0.

src = close
atr = ta.atr(length)*mult
up = hl2 + atr
dn = hl2 - atr
upper := src[1] < upper[1] ? math.min(up,upper[1]) : up
lower := src[1] > lower[1] ? math.max(dn,lower[1]) : dn

os := src > upper ? 1 : src < lower ? 0 : os[1]
spt = os == 1 ? lower : upper

max := ta.cross(src,spt) ? nz(math.max(max[1],src),src) : 
  os == 1 ? math.max(src,max[1]) : 
  math.min(spt,max[1])

min := ta.cross(src,spt) ? nz(math.min(min[1],src),src) : 
  os == 0 ? math.min(src,min[1]) : 
  math.max(spt,min[1])

avg = math.avg(max,min)
//------------------------------------------------------------------------------
var area_up_col  = color.new(#0cb51a,80)
var area_dn_col = color.new(#ff1100,80)

plot0 = plot(max,'Upper Channel'
  ,max != max[1] and os == 1 ? na : #0cb51a)
plot1 = plot(avg,'Average'
  ,#ff5d00)
plot2 = plot(min,'Lower Channel'
  ,min != min[1] and os == 0 ? na : #ff1100)

fill(plot0,plot1,area_up_col,'Upper Area')
fill(plot1,plot2,area_dn_col,'Lower Area')