FATL, SATL, RFTL, & RSTL Digital Signal Filter Smoother [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("FATL, SATL, RFTL, & RSTL Digital Signal Filter (DSP) Smoother [Loxx]", 
     shorttitle = "FSRRDFSC [Loxx]", 
     overlay = true, 
     timeframe="", 
     timeframe_gaps = true)
     
import loxx/loxxexpandedsourcetypes/3
import loxx/loxxmas/1

greencolor = #2DD204
redcolor = #D2042D 

lightgreencolor = #96E881 
lightredcolor =  #DF4F6C

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)"])

per= input.int(30, "Period", group = "Baseline Settings")
maintype = input.string("Simple Moving Average - SMA", "Baseline Type", options = ["ADXvma - Average Directional Volatility Moving Average",  "Ahrens Moving Average"
     , "Alexander Moving Average - ALXMA", "Double Exponential Moving Average - DEMA", "Double Smoothed Exponential Moving Average - DSEMA"
     , "Exponential Moving Average - EMA", "Fast Exponential Moving Average - FEMA", "Fractal Adaptive Moving Average - FRAMA"
     , "Hull Moving Average - HMA", "IE/2 - Early T3 by Tim Tilson", "Integral of Linear Regression Slope - ILRS"
     , "Instantaneous Trendline", "Laguerre Filter", "Leader Exponential Moving Average", "Linear Regression Value - LSMA (Least Squares Moving Average)"
     , "Linear Weighted Moving Average - LWMA", "McGinley Dynamic", "McNicholl EMA", "Non-Lag Moving Average", "Parabolic Weighted Moving Average"
     , "Recursive Moving Trendline", "Simple Moving Average - SMA", "Sine Weighted Moving Average", "Smoothed Moving Average - SMMA"
     , "Smoother", "Super Smoother", "Three-pole Ehlers Butterworth", "Three-pole Ehlers Smoother"
     , "Triangular Moving Average - TMA", "Triple Exponential Moving Average - TEMA", "Two-pole Ehlers Butterworth", "Two-pole Ehlers smoother"
     , "Volume Weighted EMA - VEMA", "Zero-Lag DEMA - Zero Lag Double Exponential Moving Average", "Zero-Lag Moving Average"
     , "Zero Lag TEMA - Zero Lag Triple Exponential Moving Average"],
     group = "Baseline Settings")


dspfilttype = input.string("FATL - Fast Adaptive Trend Line", "Digital Filter Type", options = ["FATL - Fast Adaptive Trend Line", "SATL - Slow Adaptive Trend Line", 
     "RFTL - Reference Fast Trend Line", "RSTL - Reference Slow Trend Line"], group = "Digital Filter Settings")
    
bbper= input.int(30, "Bollinger Bands Period", group = "Bollinger Bands Settings")

atrper= input.int(15, "ATR Period", group = "Keltner Channels Settings")
bandtype = input.string("Keltner Channels", "Bands Type", options = ["Keltner Channels", "Bollinger Bands"], group = "Keltner Channels Settings")

atrType = input.string('Smoothed Moving Average - SMMA', title='Range Smoothing MA', 
     options=["Exponential Moving Average - EMA", "Smoothed Moving Average - SMMA", 
     "Linear Weighted Moving Average - LWMA", "Simple Moving Average - SMA", "Super Smoother"], 
     group = "Keltner Channels Settings")
     
BandsStyle = input.string("Average True Range", options = ["Average True Range", "True Range", "Range"], title="Keltner Channels Style", group = "Keltner Channels Settings")

minMult = input.int(1, "Min Multiplier", group = "Shared Settings")
midMult = input.int(2, "Mid Multiplier", group = "Shared Settings")
maxMult = input.int(3, "Max Multiplier", group = "Shared Settings")

showmin = input.bool(true, "Show min channels?", group = "UI Options")
showmid  = input.bool(true, "Show mid channels?", group = "UI Options")
showmax = input.bool(true, "Show max channels?", group = "UI Options")
fillcolor = input.bool(true, "Show fill color?", group = "UI Options")

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

frama_FC = input.int(defval=1, title="* Fractal Adjusted (FRAMA) Only - FC", group = "Moving Average Inputs")
frama_SC = input.int(defval=200, title="* Fractal Adjusted (FRAMA) Only - SC", group = "Moving Average Inputs")
instantaneous_alpha = input.float(defval=0.07, minval = 0, title="* Instantaneous Trendline (INSTANT) Only - Alpha", group = "Moving Average Inputs")
_laguerre_alpha = input.float(title='* Laguerre Filter (LF) Only - Alpha', minval=0, maxval=1, step=0.1, defval=0.7, group = "Moving Average Inputs")
lsma_offset = input.int(defval=0, title="* Least Squares Moving Average (LSMA) Only - Offset", group = "Moving Average Inputs")
_pwma_pwr = input.int(2, '* Parabolic Weighted Moving Average (PWMA) Only - Power', minval=0, group = "Moving Average Inputs")
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
	
variant_atr(type, src, len) =>
    sig = 0.0
    if type == "Simple Moving Average - SMA"
        [t, s, b] = loxxmas.sma(src, len)
        sig := s
    else if type == "Exponential Moving Average - EMA"
        [t, s, b] = loxxmas.ema(src, len)
        sig := s
    else if type == "Linear Weighted Moving Average - LWMA"
        [t, s, b] = loxxmas.lwma(src, len)
        sig := s
    else if type == "Smoothed Moving Average - SMMA"
        [t, s, b] = loxxmas.smma(src, len)
        sig := s
    else if type == "Super Smoother"
        [t, s, b] = loxxmas.super(src, len)
        sig := s
    sig

variant(type, src, len) =>
    sig = 0.0
    trig = 0.0
    special = false
    if type == "ADXvma - Average Directional Volatility Moving Average"
        [t, s, b] = loxxmas.adxvma(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Ahrens Moving Average"
        [t, s, b] = loxxmas.ahrma(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Alexander Moving Average - ALXMA"
        [t, s, b] = loxxmas.alxma(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Double Exponential Moving Average - DEMA"
        [t, s, b] = loxxmas.dema(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Double Smoothed Exponential Moving Average - DSEMA"
        [t, s, b] = loxxmas.dsema(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Exponential Moving Average - EMA"
        [t, s, b] = loxxmas.ema(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Fast Exponential Moving Average - FEMA"
        [t, s, b] = loxxmas.fema(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Fractal Adaptive Moving Average - FRAMA"
        [t, s, b] = loxxmas.frama(src, len, frama_FC, frama_SC)
        sig := s
        trig := t
        special := b
    else if type == "Hull Moving Average - HMA"
        [t, s, b] = loxxmas.hma(src, len)
        sig := s
        trig := t
        special := b
    else if type == "IE/2 - Early T3 by Tim Tilson"
        [t, s, b] = loxxmas.ie2(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Integral of Linear Regression Slope - ILRS"
        [t, s, b] = loxxmas.ilrs(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Instantaneous Trendline"
        [t, s, b] = loxxmas.instant(src, instantaneous_alpha)
        sig := s
        trig := t
        special := b
    else if type == "Laguerre Filter"
        [t, s, b] = loxxmas.laguerre(src, _laguerre_alpha)
        sig := s
        trig := t
        special := b
    else if type == "Leader Exponential Moving Average"
        [t, s, b] = loxxmas.leader(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Linear Regression Value - LSMA (Least Squares Moving Average)"
        [t, s, b] = loxxmas.lsma(src, len, lsma_offset)
        sig := s
        trig := t
        special := b
    else if type == "Linear Weighted Moving Average - LWMA"
        [t, s, b] = loxxmas.lwma(src, len)
        sig := s
        trig := t
        special := b
    else if type == "McGinley Dynamic"
        [t, s, b] = loxxmas.mcginley(src, len)
        sig := s
        trig := t
        special := b
    else if type == "McNicholl EMA"
        [t, s, b] = loxxmas.mcNicholl(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Non-Lag Moving Average"
        [t, s, b] = loxxmas.nonlagma(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Parabolic Weighted Moving Average"
        [t, s, b] = loxxmas.pwma(src, len, _pwma_pwr)
        sig := s
        trig := t
        special := b
    else if type == "Recursive Moving Trendline"
        [t, s, b] = loxxmas.rmta(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Simple Moving Average - SMA"
        [t, s, b] = loxxmas.sma(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Sine Weighted Moving Average"
        [t, s, b] = loxxmas.swma(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Smoothed Moving Average - SMMA"
        [t, s, b] = loxxmas.smma(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Smoother"
        [t, s, b] = loxxmas.smoother(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Super Smoother"
        [t, s, b] = loxxmas.super(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Three-pole Ehlers Butterworth"
        [t, s, b] = loxxmas.threepolebuttfilt(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Three-pole Ehlers Smoother"
        [t, s, b] = loxxmas.threepolesss(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Triangular Moving Average - TMA"
        [t, s, b] = loxxmas.tma(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Triple Exponential Moving Average - TEMA"
        [t, s, b] = loxxmas.tema(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Two-pole Ehlers Butterworth"
        [t, s, b] = loxxmas.twopolebutter(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Two-pole Ehlers smoother"
        [t, s, b] = loxxmas.twopoless(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Volume Weighted EMA - VEMA"
        [t, s, b] = loxxmas.vwema(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Zero-Lag DEMA - Zero Lag Double Exponential Moving Average"
        [t, s, b] = loxxmas.zlagdema(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Zero-Lag Moving Average"
        [t, s, b] = loxxmas.zlagma(src, len)
        sig := s
        trig := t
        special := b
    else if type == "Zero Lag TEMA - Zero Lag Triple Exponential Moving Average"
        [t, s, b] = loxxmas.zlagtema(src, len)
        sig := s
        trig := t
        special := b
    [trig, sig, special] 
    

fatl_val = 0.4360409450 * 
     src + 0.3658689069 * 
     src[1] + 0.2460452079 * 
     src[2] + 0.1104506886 * 
     src[3] - 0.0054034585 * 
     src[4] - 0.0760367731 * 
     src[5] - 0.0933058722 * 
     src[6] - 0.0670110374 * 
     src[7] - 0.0190795053 * 
     src[8] + 0.0259609206 * 
     src[9] + 0.0502044896 * 
     src[10] + 0.0477818607 * 
     src[11] + 0.0249252327 * 
     src[12] - 0.0047706151 * 
     src[13] - 0.0272432537 * 
     src[14] - 0.0338917071 * 
     src[15] - 0.0244141482 * 
     src[16] - 0.0055774838 * 
     src[17] + 0.0128149838 * 
     src[18] + 0.0226522218 * 
     src[19] + 0.0208778257 * 
     src[20] + 0.0100299086 * 
     src[21] - 0.0036771622 * 
     src[22] - 0.0136744850 * 
     src[23] - 0.0160483392 * 
     src[24] - 0.0108597376 * 
     src[25] - 0.0016060704 * 
     src[26] + 0.0069480557 * 
     src[27] + 0.0110573605 * 
     src[28] + 0.0095711419 * 
     src[29] + 0.0040444064 * 
     src[30] - 0.0023824623 * 
     src[31] - 0.0067093714 * 
     src[32] - 0.0072003400 * 
     src[33] - 0.0047717710 * 
     src[34] + 0.0005541115 * 
     src[35] + 0.0007860160 * 
     src[36] + 0.0130129076 * 
     src[37] + 0.0040364019 * 
     src[38]

rftl_val = -0.0025097319 * 
     src + 0.0513007762 * 
     src[1] + 0.1142800493 * 
     src[2] + 0.1699342860 * 
     src[3] + 0.2025269304 * 
     src[4] + 0.2025269304 * 
     src[5] + 0.1699342860 * 
     src[6] + 0.1142800493 * 
     src[7] + 0.0513007762 * 
     src[8] - 0.0025097319 * 
     src[9] - 0.0353166244 * 
     src[10] - 0.0433375629 * 
     src[11] - 0.0311244617 * 
     src[12] - 0.0088618137 * 
     src[13] + 0.0120580088 * 
     src[14] + 0.0233183633 * 
     src[15] + 0.0221931304 * 
     src[16] + 0.0115769653 * 
     src[17] - 0.0022157966 * 
     src[18] - 0.0126536111 * 
     src[19] - 0.0157416029 * 
     src[20] - 0.0113395830 * 
     src[21] - 0.0025905610 * 
     src[22] + 0.0059521459 * 
     src[23] + 0.0105212252 * 
     src[24] + 0.0096970755 * 
     src[25] + 0.0046585685 * 
     src[26] - 0.0017079230 * 
     src[27] - 0.0063513565 * 
     src[28] - 0.0074539350 * 
     src[29] - 0.0050439973 * 
     src[30] - 0.0007459678 * 
     src[31] + 0.0032271474 * 
     src[32] + 0.0051357867 * 
     src[33] + 0.0044454862 * 
     src[34] + 0.0018784961 * 
     src[35] - 0.0011065767 * 
     src[36] - 0.0031162862 * 
     src[37] - 0.0033443253 * 
     src[38] - 0.0022163335 * 
     src[39] + 0.0002573669 * 
     src[40] + 0.0003650790 * 
     src[41] + 0.0060440751 * 
     src[42] + 0.0018747783 * 
     src[43]

satl_val = 0.0982862174 * 
     src + 0.0975682269 * 
     src[1] + 0.0961401078 * 
     src[2] + 0.0940230544 * 
     src[3] + 0.0912437090 * 
     src[4] + 0.0878391006 * 
     src[5] + 0.0838544303 * 
     src[6] + 0.0793406350 * 
     src[7] + 0.0743569346 * 
     src[8] + 0.0689666682 * 
     src[9] + 0.0632381578 * 
     src[10] + 0.0572428925 * 
     src[11] + 0.0510534242 * 
     src[12] + 0.0447468229 * 
     src[13] + 0.0383959950 * 
     src[14] + 0.0320735368 * 
     src[15] + 0.0258537721 * 
     src[16] + 0.0198005183 * 
     src[17] + 0.0139807863 * 
     src[18] + 0.0084512448 * 
     src[19] + 0.0032639979 * 
     src[20] - 0.0015350359 * 
     src[21] - 0.0059060082 * 
     src[22] - 0.0098190256 * 
     src[23] - 0.0132507215 * 
     src[24] - 0.0161875265 * 
     src[25] - 0.0186164872 * 
     src[26] - 0.0205446727 * 
     src[27] - 0.0219739146 * 
     src[28] - 0.0229204861 * 
     src[29] - 0.0234080863 * 
     src[30] - 0.0234566315 * 
     src[31] - 0.0231017777 * 
     src[32] - 0.0223796900 * 
     src[33] - 0.0213300463 * 
     src[34] - 0.0199924534 * 
     src[35] - 0.0184126992 * 
     src[36] - 0.0166377699 * 
     src[37] - 0.0147139428 * 
     src[38] - 0.0126796776 * 
     src[39] - 0.0105938331 * 
     src[40] - 0.0084736770 * 
     src[41] - 0.0063841850 * 
     src[42] - 0.0043466731 * 
     src[43] - 0.0023956944 * 
     src[44] - 0.0005535180 * 
     src[45] + 0.0011421469 * 
     src[46] + 0.0026845693 * 
     src[47] + 0.0040471369 * 
     src[48] + 0.0052380201 * 
     src[49] + 0.0062194591 * 
     src[50] + 0.0070340085 * 
     src[51] + 0.0076266453 * 
     src[52] + 0.0080376628 * 
     src[53] + 0.0083037666 * 
     src[54] + 0.0083694798 * 
     src[55] + 0.0082901022 * 
     src[56] + 0.0080741359 * 
     src[57] + 0.0077543820 * 
     src[58] + 0.0073260526 * 
     src[59] + 0.0068163569 * 
     src[60] + 0.0062325477 * 
     src[61] + 0.0056078229 * 
     src[62] + 0.0049516078 * 
     src[63] + 0.0161380976 * 
     src[64]

rstl_val = -0.0074151919 * 
     src - 0.0060698985 * 
     src[1] - 0.0044979052 * 
     src[2] - 0.0027054278 * 
     src[3] - 0.0007031702 * 
     src[4] + 0.0014951741 *
     src[5] + 0.0038713513 * 
     src[6] + 0.0064043271 * 
     src[7] + 0.0090702334 * 
     src[8] + 0.0118431116 * 
     src[9] + 0.0146922652 * 
     src[10] + 0.0175884606 * 
     src[11] + 0.0204976517 * 
     src[12] + 0.0233865835 * 
     src[13] + 0.0262218588 * 
     src[14] + 0.0289681736 * 
     src[15] + 0.0315922931 * 
     src[16] + 0.0340614696 * 
     src[17] + 0.0363444061 * 
     src[18] + 0.0384120882 * 
     src[19] + 0.0402373884 * 
     src[20] + 0.0417969735 * 
     src[21] + 0.0430701377 * 
     src[22] + 0.0440399188 * 
     src[23] + 0.0446941124 * 
     src[24] + 0.0450230100 * 
     src[25] + 0.0450230100 * 
     src[26] + 0.0446941124 * 
     src[27] + 0.0440399188 * 
     src[28] + 0.0430701377 * 
     src[29] + 0.0417969735 * 
     src[30] + 0.0402373884 * 
     src[31] + 0.0384120882 * 
     src[32] + 0.0363444061 * 
     src[33] + 0.0340614696 * 
     src[34] + 0.0315922931 * 
     src[35] + 0.0289681736 * 
     src[36] + 0.0262218588 * 
     src[37] + 0.0233865835 * 
     src[38] + 0.0204976517 * 
     src[39] + 0.0175884606 * 
     src[40] + 0.0146922652 *
     src[41] + 0.0118431116 * 
     src[42] + 0.0090702334 * 
     src[43] + 0.0064043271 * 
     src[44] + 0.0038713513 * 
     src[45] + 0.0014951741 * 
     src[46] - 0.0007031702 * 
     src[47] - 0.0027054278 * 
     src[48] - 0.0044979052 * 
     src[49] - 0.0060698985 * 
     src[50] - 0.0074151919 * 
     src[51] - 0.0085278517 * 
     src[52] - 0.0094111161 *
     src[53] - 0.0100658241 * 
     src[54] - 0.0104994302 * 
     src[55] - 0.0107227904 * 
     src[56] - 0.0107450280 * 
     src[57] - 0.0105824763 * 
     src[58] - 0.0102517019 * 
     src[59] - 0.0097708805 * 
     src[60] - 0.0091581551 * 
     src[61] - 0.0084345004 * 
     src[62] - 0.0076214397 * 
     src[63] - 0.0067401718 * 
     src[64] - 0.0058083144 * 
     src[65] - 0.0048528295 * 
     src[66] - 0.0038816271 * 
     src[67] - 0.0029244713 * 
     src[68] - 0.0019911267 * 
     src[69] - 0.0010974211 * 
     src[70] - 0.0002535559 * 
     src[71] + 0.0005231953 * 
     src[72] + 0.0012297491 * 
     src[73] + 0.0018539149 * 
     src[74] + 0.0023994354 * 
     src[75] + 0.0028490136 * 
     src[76] + 0.0032221429 * 
     src[77] + 0.0034936183 * 
     src[78] + 0.0036818974 * 
     src[79] + 0.0038037944 * 
     src[80] + 0.0038338964 * 
     src[81] + 0.0037975350 * 
     src[82] + 0.0036986051 * 
     src[83] + 0.0035521320 * 
     src[84] + 0.0033559226 * 
     src[85] + 0.0031224409 * 
     src[86] + 0.0028550092 * 
     src[87] + 0.0025688349 * 
     src[88] + 0.0022682355 * 
     src[89] + 0.0073925495 * 
     src[90]
     
filtout = 
     dspfilttype == "FATL - Fast Adaptive Trend Line" ? fatl_val : 
     dspfilttype == "SATL - Slow Adaptive Trend Line" ? rftl_val : 
     dspfilttype == "RFTL - Reference Fast Trend Line" ? rftl_val : 
     satl_val
     
atrout = 
     BandsStyle == "True Range" ? ta.tr(true) : 
     BandsStyle == "Average True Range" ? variant_atr(atrType, ta.tr(true), atrper) : 
     variant_atr(atrType, high - low, atrper)

[avg, _, _] = variant(maintype, filtout, per)

dev = bandtype == "Keltner Channels" ? atrout : ta.stdev(filtout, bbper)

minUp = avg + dev * minMult
midUp = avg + dev * midMult
maxUp = avg + dev * maxMult

minDn = avg - dev * minMult
midDn = avg - dev * midMult
maxDn = avg - dev * maxMult

colorout = avg > avg[1] ? greencolor : redcolor

midplot = plot(avg, color = colorout, linewidth = 2) 

plot(showmin ? minUp : na, "minUp", color = color.new(greencolor, 50))
plot(showmin ? minDn : na, "minDn", color = color.new(redcolor, 50))

plot(showmid ? midUp : na, "midUp", color = color.new(greencolor, 50))
plot(showmid ? midDn : na, "midDn", color = color.new(redcolor, 50))

mupplot = plot(showmax ? maxUp : na, "maxUp", color = color.new(greencolor, 50))
mdnplot = plot(showmax ? maxDn : na, "maxDn", color = color.new(redcolor, 50))

fill(mupplot, midplot, fillcolor ? color.new(greencolor, 95) : na)
fill(mdnplot, midplot, fillcolor ? color.new(redcolor, 95) : na)

barcolor(colorbars ? colorout : na)



