Smoothing ATR band
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © khlin712

//@version=5
indicator("Smoothing ATR band",shorttitle = 'SAB [khlin712]',overlay=true)


atrLen = input.int(title='Length', defval=20, minval=1)
smoothing = input.string(title='Smoothing', defval='RMA', options=['RMA', 'SMA', 'EMA', 'WMA'])
m = input.float(2, 'Multiplier')
src1 = high
src2 = low
pline = false
col1 = color.blue
col2 = color.teal
col3 = color.red

collong = color.teal
colshort = color.red

ma_function(source, atrLen) =>
    if smoothing == 'RMA'
        ta.rma(source, atrLen)


a = ma_function(ta.tr(true), atrLen) * m
ssl = ma_function(ta.tr(true), atrLen) * m + src1
lsl = src2 - ma_function(ta.tr(true), atrLen) * m

 
ma(source, atrLen, type) =>
    switch type
        "SMA" => ta.sma(source, atrLen)
        "EMA" => ta.ema(source, atrLen)
        "SMMA (RMA)" => ta.rma(source, atrLen)
        "WMA" => ta.wma(source, atrLen)
        "VWMA" => ta.vwma(source, atrLen)

typeMA = input.string(title = "Method", defval = "EMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Smoothing")
smoothingLength = input.int(title = "Length", defval = 8, minval = 1, group="Smoothing")

smoothingLineU = ma(ssl, smoothingLength, typeMA)
au=plot(smoothingLineU, title="Smoothing Line", color=color.new(#bd5be3, 30),linewidth=3)
plotshape(high>smoothingLineU and close<smoothingLineU,title='short',style=shape.labeldown,color=#db5e5e,location=location.abovebar,size=size.small)
smoothingLineD = ma(lsl, smoothingLength, typeMA)
ad=plot(smoothingLineD, title="Smoothing Line", color=color.new(#bd5be3,30),linewidth=3)
plotshape(low<smoothingLineD and close>smoothingLineD,title='long',style=shape.labelup,color=#68d94c,location=location.belowbar,size=size.small)

plot((smoothingLineU+smoothingLineD)/2, title="Base Line", color=color.new(#bd5be3, 30),linewidth=2,offset=1)
//alert

alertup=high>smoothingLineU and close<smoothingLineU==true 
alertdown=low<smoothingLineD and close>smoothingLineD==true

// If you're free tradingview user, you are allowed to have one alert setting.
// Long and Short Opportunities will also trigger the alert 
allert=alertup==true or alertdown==true 
alertcondition(allert,title='SAB_alert(Recommended✅)',message='{{ticker}}, Price:{{close}}, Timeframe:{{interval}}, Trading Opportunity!')
// If setting many alerts is allowed,you can choose these two settings respectively.
alertcondition(alertup,title='SAB Long',message='{{ticker}}, Price:{{close}}, Timeframe:{{interval}}, SAB Long📈')
alertcondition(alertdown,title='SAB Short',message='{{ticker}}, Price:{{close}}, Timeframe:{{interval}}, SAB Short📉')
