Rollin' pseudo-Bollinger Bands 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EsIstTurnt

//@version=5
indicator("Rollin' pseudo-Bollinger Bands ")

 //Integers the Boundary is Built upon
src=close>open and close>high[3]?high:low
len1        = input.int(title="Regression Length 1",defval=64 , minval=1)
len2        = input.int(title="Regression Length 2",defval=96 , minval=1)
len3        = input.int(title="Regression Length 3",defval=128, minval=1)
len4        = input.int(title="Regression Length 4",defval=256, minval=1)
len5        = input.int(title="Regression Length 5",defval=512, minval=1)

//User Options
maswitch    = input.bool(true ,title='Curve Calc Mode 1/2         ')
hllevels    = input.bool(false,title='Show HL Levels?             ')
plotupper   = input.bool(true ,title='Show Upper Extension Levels?')
plotlower   = input.bool(true ,title='Show Lower Extension Levels?')
p_hl1       = input.bool(false,title='Show H/L Level 1?           ')
p_hl2       = input.bool(false,title='Show H/L Level 2?           ')
p_hl3       = input.bool(false,title='Show H/L Level 3?           ')
p_hl4       = input.bool(false,title='Show H/L Level 4?           ')
p_hl5       = input.bool(false,title='Show H/L Level 5?           ')
tf          = input.timeframe('',title='TimeFrame?')

//Get Sources and Make Boundary Lines
src1        = request.security  (syminfo.ticker,tf,ta.ema(close,16))
src2        = request.security  (syminfo.ticker,tf,ta.ema(close,32))
src3        = request.security  (syminfo.ticker,tf,ta.ema(close,48))
src4        = request.security  (syminfo.ticker,tf,ta.ema(close,64))
src5        = request.security  (syminfo.ticker,tf,ta.ema(close,80))
up1         = ta.swma(ta.highest(high,8 ))
dn1         = ta.swma(ta.lowest (low ,8 ))
up2         = ta.swma(ta.highest(high,32))
dn2         = ta.swma(ta.lowest (low ,32))
up3         = ta.highest        (high,48 )
dn3         = ta.lowest         (low ,48 )
up4         = ta.highest        (high,64 )
dn4         = ta.lowest         (low ,64 )
up5         = ta.highest        (high,360)
dn5         = ta.lowest         (low ,360)
updngap1    = up1-dn1
updngap2    = up2-dn2
updngap3    = up3-dn3
updngap4    = up4-dn4
updngap5    = up5-dn5
out0        = maswitch?ta.linreg(ta.sma(close,8), 8, -8): ta.ema(ta.sma(close,8),8)
out1        = maswitch?ta.linreg(src1, len1, 0)   : ta.ema(ta.linreg(src1,len1,0),len1/3)
out2        = maswitch?ta.linreg(src2, len2, 0)   : ta.ema(ta.linreg(src2,len2,0),len2/3)
out3        = maswitch?ta.linreg(src3, len3, 0)   : ta.ema(ta.linreg(src3,len3,0),len3/3)
out4        = maswitch?ta.linreg(src4, len4, 0)   : ta.ema(ta.linreg(src4,len4,0),len4/3)
out5        = maswitch?ta.linreg(src5, len5, 0)   : ta.ema(ta.linreg(src5,len5,0),len5/3)
line1       = up1>out5?up2:out5>out4?out5:out4>out3?out4:out3>out2?out3:out2>out1?out2:out1>out0?out1:out0
line2       = dn1<out5?dn2:out5<out4?out5:out4<out3?out4:out3<out2?out3:out2<out1?out2:out1

//Calculate Other Lines Based on Boundarys
gap1        = (line1-close)
gap2        = (close-line2)
gap3        = (line1-line2)
avline      = math.avg(line1,line2)
avsma       = ta.sma  (avline,8   )
hllev1a     = ta.swma (ta.vwap    (ta.sma(avline+            ((updngap1)),64  )))
hllev2a     = ta.swma (ta.vwap    (ta.sma(avline+            ((updngap2)),64  )))
hllev3a     = ta.swma (ta.vwap    (ta.sma(avline+            ((updngap3)),64  )))
hllev4a     = ta.swma (ta.vwap    (ta.sma(avline+            ((updngap4)/2),64)))
hllev5a     = ta.swma (ta.vwap    (ta.sma(avline+            ((updngap5)/2),64)))
hllev1b     = ta.swma (ta.vwap    (ta.sma(avline-            ((updngap1)),64  )))
hllev2b     = ta.swma (ta.vwap    (ta.sma(avline-            ((updngap2)),64  )))
hllev3b     = ta.swma (ta.vwap    (ta.sma(avline-            ((updngap3)),64  )))
hllev4b     = ta.swma (ta.vwap    (ta.sma(avline-            ((updngap4)/2),64)))
hllev5b     = ta.swma (ta.vwap    (ta.sma(avline-            ((updngap5)/2),64)))
line2a      = ta.swma (line2+(input.float(0.068 ))*ta.highest((  gap3*2.68),64 ))
line2b      = ta.swma (line2-(input.float(0.068 ))*ta.highest((  gap3*2.68),64 ))
line2c      = ta.swma (line2+(input.float(0.2236))*ta.highest((  gap3*2.68),128))
line2d      = ta.swma (line2+(input.float(0.1236))*ta.highest((  gap3*2.68),128))
line1a      = ta.swma (line1-(input.float(0.068 ))*ta.highest((  gap3*2.68),64 ))
line1b      = ta.swma (line1+(input.float(0.068 ))*ta.highest((  gap3*2.68),64 ))
line1c      = ta.swma (line1-(input.float(0.2236))*ta.highest((  gap3*2.68),128))
line1d      = ta.swma (line1-(input.float(0.1236))*ta.highest((  gap3*2.68),128))
//hlav1  = math.avg(hllev1a,hllev1b)
//hlav2  = math.avg(hllev2a,hllev2b)
//hlav3  = math.avg(hllev3a,hllev3b)
//hlav4  = math.avg(hllev4a,hllev4b)
//hlav5  = math.avg(hllev5a,hllev5b)

//Dots inversing what the Candles are doing relative to line1/line2
dot1        = close>avline?line2+gap1:na 
dot2        = close<avline?line1-gap2:na 

//Colors
hlcol       = color.new(input.color(color.aqua),40)
gcol        = color.new(input.color(color.gray),60)

//Plot It!
//p_up1 = plot(p_hl1?up1:na                        ,title='New Up Boundary'         ,linewidth=1,color=hlcol                                                           ,style=plot.style_linebr )
p_up2 = plot(p_hl2?up2:na                        ,title='New Up Boundary'         ,linewidth=1,color=hlcol                                                           ,style=plot.style_linebr )
p_up3 = plot(p_hl3?up3:na                        ,title='New Up Boundary'         ,linewidth=1,color=hlcol                                                           ,style=plot.style_linebr )
p_up4 = plot(p_hl4?up4:na                        ,title='New Up Boundary'         ,linewidth=1,color=hlcol                                                           ,style=plot.style_linebr )
p_up5 = plot(p_hl5?up5:na                        ,title='New Up Boundary'         ,linewidth=1,color=hlcol                                                           ,style=plot.style_linebr )
//p_dn1 = plot(p_hl1?dn1:na                        ,title='New Down Boundary'       ,linewidth=1,color=hlcol                                                           ,style=plot.style_linebr )
p_dn2 = plot(p_hl2?dn2:na                        ,title='New Down Boundary'       ,linewidth=1,color=hlcol                                                           ,style=plot.style_linebr )
p_dn3 = plot(p_hl3?dn3:na                        ,title='New Down Boundary'       ,linewidth=1,color=hlcol                                                           ,style=plot.style_linebr )
p_dn4 = plot(p_hl4?dn4:na                        ,title='New Down Boundary'       ,linewidth=1,color=hlcol                                                           ,style=plot.style_linebr )
p_dn5 = plot(p_hl5?dn5:na                        ,title='New Down Boundary'       ,linewidth=1,color=hlcol                                                           ,style=plot.style_linebr )
l1    = plot(line1                               ,title='Upper Boundary'          ,linewidth=2,color=#ff0000                                                         ,style=plot.style_linebr )
l2    = plot(line2                               ,title='Lower Boundary'          ,linewidth=2,color=#acfb00                                                         ,style=plot.style_linebr )
p_av  = plot(avsma                               ,title='Center Line'             ,linewidth=2,color=color.white                                                     ,style=plot.style_linebr )
p_dot1= plot(dot1                                ,title='Candle Inverse Dots'     ,linewidth=1,color=color.green                                                     ,style=plot.style_circles)
p_dot2= plot(dot2                                ,title='Candle Inverse Dots'     ,linewidth=1,color=color.red                                                       ,style=plot.style_circles)
p_lv1a= plot(hllevels?p_hl1?hllev1a:na:na        ,title='High/Low Levels'         ,linewidth=1,color=gcol                                                            ,style=plot.style_linebr )
p_lv2a= plot(hllevels?p_hl2?hllev2a:na:na        ,title='High/Low Levels'         ,linewidth=1,color=gcol                                                            ,style=plot.style_linebr )
p_lv3a= plot(hllevels?p_hl3?hllev3a:na:na        ,title='High/Low Levels'         ,linewidth=1,color=gcol                                                            ,style=plot.style_linebr )
p_lv4a= plot(hllevels?p_hl4?hllev4a:na:na        ,title='High/Low Levels'         ,linewidth=1,color=gcol                                                            ,style=plot.style_linebr )
p_lv5a= plot(hllevels?p_hl5?hllev5a:na:na        ,title='High/Low Levels'         ,linewidth=1,color=gcol                                                            ,style=plot.style_linebr )
p_lv1b= plot(hllevels?p_hl1?hllev1b:na:na        ,title='High/Low Levels'         ,linewidth=1,color=gcol                                                            ,style=plot.style_linebr )
p_lv2b= plot(hllevels?p_hl2?hllev2b:na:na        ,title='High/Low Levels'         ,linewidth=1,color=gcol                                                            ,style=plot.style_linebr )
p_lv3b= plot(hllevels?p_hl3?hllev3b:na:na        ,title='High/Low Levels'         ,linewidth=1,color=gcol                                                            ,style=plot.style_linebr )
p_lv4b= plot(hllevels?p_hl4?hllev4b:na:na        ,title='High/Low Levels'         ,linewidth=1,color=gcol                                                            ,style=plot.style_linebr )
p_lv5b= plot(hllevels?p_hl5?hllev5b:na:na        ,title='High/Low Levels'         ,linewidth=1,color=gcol                                                            ,style=plot.style_linebr )
p_out1= plot(out1                                ,title='Linear Regression Curves',linewidth=1,color=color.black                                                     ,style=plot.style_linebr )
p_out2= plot(out2                                ,title='Linear Regression Curves',linewidth=1,color=color.black                                                     ,style=plot.style_linebr )
p_out3= plot(out3                                ,title='Linear Regression Curves',linewidth=1,color=color.black                                                     ,style=plot.style_linebr )
p_out4= plot(out4                                ,title='Linear Regression Curves',linewidth=1,color=color.black                                                     ,style=plot.style_linebr )
p_out5= plot(out5                                ,title='Linear Regression Curves',linewidth=2,color=out5>avline?#ff0000:#acfb00,transp=50                           ,style=plot.style_cross  )
l1a   = plot(plotupper?line1a:na                 ,title ='line1a'                 ,linewidth=1,color=gcol                                                            ,style=plot.style_linebr )
l2a   = plot(plotlower?line2a:na                 ,title ='line2a'                 ,linewidth=1,color=gcol                                                            ,style=plot.style_linebr )                                                   
l1b   = plot(plotupper?line1b:na                 ,title ='line1b'                 ,linewidth=1,color=color.black                                                     ,style=plot.style_linebr )
l2b   = plot(plotlower?line2b:na                 ,title ='line2b'                 ,linewidth=1,color=color.black                                                     ,style=plot.style_linebr )
l1c   = plot(plotupper?line1c>line2?line1c:na:na ,title ='line1c '                ,linewidth=1,color=line1c>line2 ? color.maroon  :na                                ,style=plot.style_linebr )
l2c   = plot(plotlower?line2c<line1?line2c:na:na ,title ='line2c '                ,linewidth=1,color=line2c<line1 ? color.teal:na                                    ,style=plot.style_linebr )
l1d   = plot(plotupper?line1d>line2?line1d:na:na ,title ='line1d '                ,linewidth=1,color=line1d < close or  close >close[2]or (line1 - close) > (close - line2)  ? color.maroon  :na)
l2d   = plot(plotlower?line2d<line1?line2d:na:na ,title ='line2d '                ,linewidth=1,color=line2 > close or  close <close[2]or (line1 - close) < (close - line2)   ? color.teal:na    )
fill(l1,l1c,color=color.new(#ff0000,85))
fill(l2,l2c,color=color.new(#acfb00,85))
fill(l1,l1d,color=color.new(#ff0000,95))
fill(l2,l2d,color=color.new(#acfb00,95))
//plot(hllevels?p_hl1?hlav1  :na:na,color=color.new(input.color(color.white),80),style=plot.style_linebr)
//plot(hllevels?p_hl2?hlav2  :na:na,color=color.new(input.color(color.white),80),style=plot.style_linebr)
//plot(hllevels?p_hl3?hlav3  :na:na,color=color.new(input.color(color.white),80),style=plot.style_linebr)
//plot(hllevels?p_hl4?hlav4  :na:na,color=color.new(input.color(color.white),80),style=plot.style_linebr)
//plot(hllevels?p_hl5?hlav5  :na:na,color=color.new(input.color(color.white),80),style=plot.style_linebr)
