Adaptive Supertrend 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("Adaptive Supertrend w/ Floating Levels [Loxx]", shorttitle = "ASTFL [Loxx]", 
     overlay = true, 
     timeframe="", 
     timeframe_gaps = true)

greencolor = #2DD204
redcolor = #D2042D 

RMA(x, t) =>
    EMA1 = x
    EMA1 := na(EMA1[1]) ? x : (x - nz(EMA1[1])) * (1/t) + nz(EMA1[1])
    EMA1

pine_supertrend(src, factor, atrPeriod) =>
	atr = RMA(ta.tr(true), atrPeriod)
	upperBand = src + factor * atr
	lowerBand = src - factor * atr
	prevLowerBand = nz(lowerBand[1])
	prevUpperBand = nz(upperBand[1])

	lowerBand := lowerBand > prevLowerBand or close[1] < prevLowerBand ? lowerBand : prevLowerBand
	upperBand := upperBand < prevUpperBand or close[1] > prevUpperBand ? upperBand : prevUpperBand
	int direction = na
	float superTrend = na
	prevSuperTrend = superTrend[1]
	if na(atr[1])
		direction := 1
	else if prevSuperTrend == prevUpperBand
		direction := close > upperBand ? -1 : 1
	else
		direction := close < lowerBand ? 1 : -1
	superTrend := direction == -1 ? lowerBand : upperBand
	[superTrend, direction]

src = input.source(hl2, "Source", group = "Basic Settings")
period = input.int(7, "Period", group = "Basic Settings")
mult = input.float(3.0, "Period", group = "Basic Settings")
adapt = input.bool(true, "Make it adaptive?", group = "Basic Settings")

flLookBack = input.int(25, "Floating Level Lookbacl Period", group = "Advanced Settings")
flLevelUp  = input.float(90, "Floating Levels Up Level %", group = "Advanced Settings")  
flLevelDown =  input.float(25, "Floating Levels Down Level %", group = "Advanced 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")

SumX  = 0., SumXX = 0., SumXY = 0., SumYY = 0., SumY  = 0.

for k = 0 to period-1 by 1
    SumX  += SumX+(k+1)
    SumXX += SumXX+((k+1)*(k+1))
    SumXY += SumXY+((k+1)*src)
    SumYY += SumYY+(src*src)
    SumY  += SumY+src

Q1 = SumXY - SumX*SumY/period
Q2 = SumXX - SumX*SumX/period
Q3 = SumYY - SumY*SumY/period
 
iRsq= (Q1*Q1)/(Q2*Q3)
[supertrend, direction]  = pine_supertrend(src, mult, adapt ?  math.ceil(period + period * (iRsq-0.25)) : period)

mini = ta.lowest(supertrend, flLookBack)
maxi = ta.highest(supertrend, flLookBack)

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

top = plot(showfloat ? flu : na, "Top float", color = color.new(greencolor, 50), linewidth = 1)
bot = plot(showfloat ? fld : na, "bottom float", color = color.new(redcolor, 50), linewidth = 1)
mid = plot(showfloat ? flm : na, "Mid Float", color = color.new(color.white, 10), 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 ? direction == -1 ? greencolor : redcolor : na)
plot(supertrend, "Supertrend", color = direction == -1 ? greencolor : redcolor, linewidth = 3)


