STD Adaptive ADXm w/ Floating Levels [Loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
indicator("STD Adaptive ADXm w/ Floating Levels [Loxx]", shorttitle = "STDAADXMFL [Loxx]", 
     overlay = false, 
     timeframe="", 
     timeframe_gaps = true)

greencolor = #2DD204
redcolor = #D2042D 

super(src, len) =>
    f = (math.sqrt(2) * math.pi) / len
    a = math.exp(-f)
    c2 = 2 * a * math.cos(f)
    c3 = -a * a
    c1 = 1 - c2 - c3
    smooth = 0.0
    smooth := c1 * (src + src[1]) * 0.5 + c2 * nz(smooth[1]) + c3 * nz(smooth[2])
    smooth
    
period = input.int(14, "Period", group = "Basic Settings")
Smooth = input.int(15, "Smoothing Period", group = "Basic Settings")
AdaptPeriod = input.int(14, "Adaptive Period", group = "Basic Settings")

upLevel = input.int(90, "Upper Boundary", group = "Basic Settings")
downLevel = input.int(10, "Lower Boundary", group = "Basic Settings")

colorbars = input.bool(false, "Color bars?", group = "UI Options")
showfloat = input.bool(true, "Show Floating Levels?", group = "UI Options")
showfill = input.bool(true, "Fil Floating Levels?", group = "UI Options")

dev = ta.stdev(close, AdaptPeriod)
avg = ta.sma(dev, AdaptPeriod)

tperiod = dev != 0 ? math.ceil(period * avg / dev) : period

alpha = 2.0/(tperiod + 1.0)

workSsm11 = super(high, Smooth) 
workSsm12 = super(low, Smooth) 
workSsm13 = super(nz(close[1]), Smooth) 
workSsm14 = super(nz(high[1]), Smooth) 
workSsm15 = super(nz(low[1]), Smooth) 

dh = math.max(workSsm11 - workSsm14, 0)
dl = math.max(workSsm15 - workSsm12, 0)

if(dh == dl) 
    dh:=0
    dl:=0
else if(dh < dl) 
    dh:=0
else if(dl < dh) 
    dl:=0

ttr = math.max(workSsm11, workSsm13) - math.min(workSsm12, workSsm13)
dhk = 0., dlk = 0.
      
dhk := ttr != 0 ? 100.0*dh/ttr : dhk
dlk := ttr != 0 ? 100.0*dl/ttr : dlk

workzdh = 0., workzdl = 0.

workzdh := nz(workzdh[1]) + alpha * (dhk- nz(workzdh[1]))
workzdl := nz(workzdl[1]) + alpha * (dlk- nz(workzdl[1]))

dDI = workzdh - workzdl
div  = math.abs(workzdh + workzdl)

temp = 0.
temp := div != 0.0 ? 100 * dDI/div : temp

aADX = 0.
aADX := nz(aADX[1]) + alpha * (temp - nz(aADX[1]))

flLevelUp = upLevel  
flLevelDown = downLevel  

maxi = ta.highest(aADX, nz(math.ceil(tperiod), 1))
mini = ta.lowest(aADX, nz(math.ceil(tperiod), 1))

rrange = maxi - mini

flu = mini + flLevelUp * rrange / 100.0 
fld = mini + flLevelDown * rrange / 100.0 
flm = mini + 0.5 * rrange 

colorout =  aADX > nz(aADX[1]) ? greencolor : redcolor

plot(aADX, "ADX", color = colorout, linewidth = 3)

top = plot(showfloat ? flu : na, "Top float", color = color.new(greencolor, 50), linewidth = 1)
mid = plot(showfloat ? flm : na, "Mid float", color = color.new(redcolor, 50), linewidth = 1)
bot = plot(showfloat ? fld : na, "Bottom Float", color = color.new(color.white, 50), linewidth = 1)

fill(top, mid, title = "Top fill color", color = showfill ? color.new(greencolor, 95) : na)
fill(bot, mid, title = "Bottom fill color",  color = showfill ?  color.new(redcolor, 95) : na)

barcolor(colorbars ? colorout : na)
















