ATR-Adaptive, Smoothed Laguerre RSI [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("ATR-Adaptive, Smoothed Laguerre RSI [Loxx]", 
     shorttitle = "ATRASLRSI [Loxx]", 
     overlay = false, 
     timeframe="", 
     timeframe_gaps = true)
     
import loxx/loxxexpandedsourcetypes/3

greencolor = #2DD204
redcolor = #D2042D 

_lagfiltosc(src, per) =>
    _gamma = 1.0 - 10.0 / (per + 9.0)
    L0 = 0.0, L1 = 0.0, L2 = 0.0, L3 = 0.0
    L0 := (1 - _gamma) * src + _gamma * nz(L0[1])
    L1 := -_gamma * L0 + nz(L0[1]) + _gamma * nz(L1[1])
    L2 := -_gamma * L1 + nz(L1[1]) + _gamma * nz(L2[1])
    L3 := -_gamma * L2 + nz(L2[1]) + _gamma * nz(L3[1])
    CU = 0., CD = 0.
    if (L0 >= L1)
        CU := L0 - L1
    else 
        CD := L1 - L0
    if (L1 >= L2)
        CU += L1 - L2
    else 
        CD += L2 - L1
    if (L2 >= L3)
        CU += L2 - L3
    else 
        CD += L3 - L2
    out = ((CU + CD != 0) ? CU / (CU + CD) : 0)
    out
 
smthtype = input.string("Kaufman", "Heiken-Ashi Better Smoothing", options = ["AMA", "T3", "Kaufman"], group=  "Source Settings")

srcoption = input.string("Close", "Source", group= "Source Settings", 
     options = 
     ["Close", "Open", "High", "Low", "Median", "Typical", "Weighted", "Average", "Average Median Body", "Trend Biased", "Trend Biased (Extreme)", 
     "HA Close", "HA Open", "HA High", "HA Low", "HA Median", "HA Typical", "HA Weighted", "HA Average", "HA Average Median Body", "HA Trend Biased", "HA Trend Biased (Extreme)",
     "HAB Close", "HAB Open", "HAB High", "HAB Low", "HAB Median", "HAB Typical", "HAB Weighted", "HAB Average", "HAB Average Median Body", "HAB Trend Biased", "HAB Trend Biased (Extreme)"])


atrper = input.int(15, "ATR Period", group = "Basic Settings")
srcsmth = input.int(5, "Source Smoothing Period", group = "Basic Settings")
inpLevelUp = input.float(0.85, "Upper Boundary", group = "Basic Settings") 
inpLevelDown = input.float(0.15, "Lower Boundary", group = "Basic Settings") 

colorbars = input.bool(false, "Color bars?", group= "UI Options")

kfl=input.float(0.666, title="* Kaufman's Adaptive MA (KAMA) Only - Fast End", group = "Moving Average Inputs")
ksl=input.float(0.0645, title="* Kaufman's Adaptive MA (KAMA) Only - Slow End", group = "Moving Average Inputs")
amafl = input.int(2, title="* Adaptive Moving Average (AMA) Only - Fast", group = "Moving Average Inputs")
amasl = input.int(30, title="* Adaptive Moving Average (AMA) Only - Slow", group = "Moving Average Inputs")

haclose = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)
haopen = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open)
hahigh = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high)
halow = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low)
hamedian = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hl2)
hatypical = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlc3)
haweighted = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlcc4)
haaverage = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, ohlc4)

float src = switch srcoption
	"Close" => loxxexpandedsourcetypes.rclose()
	"Open" => loxxexpandedsourcetypes.ropen()
	"High" => loxxexpandedsourcetypes.rhigh()
	"Low" => loxxexpandedsourcetypes.rlow()
	"Median" => loxxexpandedsourcetypes.rmedian()
	"Typical" => loxxexpandedsourcetypes.rtypical()
	"Weighted" => loxxexpandedsourcetypes.rweighted()
	"Average" => loxxexpandedsourcetypes.raverage()
    "Average Median Body" => loxxexpandedsourcetypes.ravemedbody()
	"Trend Biased" => loxxexpandedsourcetypes.rtrendb()
	"Trend Biased (Extreme)" => loxxexpandedsourcetypes.rtrendbext()
	"HA Close" => loxxexpandedsourcetypes.haclose(haclose)
	"HA Open" => loxxexpandedsourcetypes.haopen(haopen)
	"HA High" => loxxexpandedsourcetypes.hahigh(hahigh)
	"HA Low" => loxxexpandedsourcetypes.halow(halow)
	"HA Median" => loxxexpandedsourcetypes.hamedian(hamedian)
	"HA Typical" => loxxexpandedsourcetypes.hatypical(hatypical)
	"HA Weighted" => loxxexpandedsourcetypes.haweighted(haweighted)
	"HA Average" => loxxexpandedsourcetypes.haaverage(haaverage)
    "HA Average Median Body" => loxxexpandedsourcetypes.haavemedbody(haclose, haopen)
	"HA Trend Biased" => loxxexpandedsourcetypes.hatrendb(haclose, haopen, hahigh, halow)
	"HA Trend Biased (Extreme)" => loxxexpandedsourcetypes.hatrendbext(haclose, haopen, hahigh, halow)
	"HAB Close" => loxxexpandedsourcetypes.habclose(smthtype, amafl, amasl, kfl, ksl)
	"HAB Open" => loxxexpandedsourcetypes.habopen(smthtype, amafl, amasl, kfl, ksl)
	"HAB High" => loxxexpandedsourcetypes.habhigh(smthtype, amafl, amasl, kfl, ksl)
	"HAB Low" => loxxexpandedsourcetypes.hablow(smthtype, amafl, amasl, kfl, ksl)
	"HAB Median" => loxxexpandedsourcetypes.habmedian(smthtype, amafl, amasl, kfl, ksl)
	"HAB Typical" => loxxexpandedsourcetypes.habtypical(smthtype, amafl, amasl, kfl, ksl)
	"HAB Weighted" => loxxexpandedsourcetypes.habweighted(smthtype, amafl, amasl, kfl, ksl)
	"HAB Average" => loxxexpandedsourcetypes.habaverage(smthtype, amafl, amasl, kfl, ksl)
    "HAB Average Median Body" => loxxexpandedsourcetypes.habavemedbody(smthtype, amafl, amasl, kfl, ksl)
	"HAB Trend Biased" => loxxexpandedsourcetypes.habtrendb(smthtype, amafl, amasl, kfl, ksl)
	"HAB Trend Biased (Extreme)" => loxxexpandedsourcetypes.habtrendbext(smthtype, amafl, amasl, kfl, ksl)
	=> haclose

atr = ta.atr(atrper)

prevMax = atr
prevMin = atr

for k = 2 to atrper
    if (prevMax < nz(atr[k])) 
        prevMax := nz(atr[k])
    if (prevMin > nz(atr[k])) 
        prevMin := nz(atr[k])

srcout = ta.ema(src, srcsmth)
_max = prevMax > atr ? prevMax : atr
_min = prevMin < atr ? prevMin : atr
_coeff = (_min != _max) ? 1.0 - (atr - _min) / (_max - _min) : 0.5

val = _lagfiltosc(srcout, atrper * (_coeff + 0.75))
valc = (val > inpLevelUp) ? 1 : (val < inpLevelDown) ? 2 : 0

plval = plot(val, color = valc == 1 ? greencolor : valc == 2 ? redcolor : na, linewidth = 3)
plot(val, color = valc == 0 ? color.gray : na, linewidth = 1)

plup = plot(inpLevelUp, color = bar_index % 2 ? color.gray : na)
pldn = plot(inpLevelDown, color = bar_index % 2 ? color.gray : na)

fill(plup, plval, color = valc == 1 ?  greencolor : na)
fill(pldn, plval, color = valc == 2 ?  redcolor : na)

barcolor(colorbars ? valc == 1 ? greencolor : valc == 2 ? redcolor : color.gray : na)






























