Moving Average SAR
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Seungdori_
//@version=5
indicator('Moving Average SAR', shorttitle='MA SAR', overlay = true)

//Inputs//

sensitiveness = 60 * input.int(10, minval=1, title='Sensitiveness')
len = input.int(20, minval=1, title='Length')
MA_type = input.string('RMA', 'MA Type ',options=['SMA', 'EMA', 'HMA', 'WMA', 'DEMA', 'RMA', 'Donchian', 'LSMA','SWMA', 'T3', 'VWAP', 'VWMA'])
src = close
trend_para = input.float(defval = 1, title= 'Trend Strength Parameter', step = 0.01)
AA = input.float(1, title=  'Band Width', step = 0.1)
show_signal = input.bool(defval= false, title = 'Show Signals')
//MA Settings//


/////////////////////
//Region : Function//
/////////////////////
bool bull = false
bool bear = false

FuncT3(_src, _length) =>
    axe1 = ta.ema(_src, _length)
    axe2 = ta.ema(axe1, _length)
    axe3 = ta.ema(axe2, _length)
    axe4 = ta.ema(axe3, _length)
    axe5 = ta.ema(axe4, _length)
    axe6 = ta.ema(axe5, _length)
    ab = 0.7
    ac1 = -ab * ab * ab
    ac2 = 3 * ab * ab + 3 * ab * ab * ab
    ac3 = -6 * ab * ab - 3 * ab - 3 * ab * ab * ab
    ac4 = 1 + 3 * ab + ab * ab * ab + 3 * ab * ab
    T3 = ac1 * axe6 + ac2 * axe5 + ac3 * axe4 + ac4 * axe3
    T3

getMA(ma_type ,src, length) =>
    maPrice = ta.ema(src, length)
    ema = ta.ema(src, length)
    sma = ta.sma(src, length)
    if ma_type == 'SMA'
        maPrice := ta.sma(src, length)
        maPrice
    if ma_type == 'EMA'
        maPrice := ta.ema(src, length)
        maPrice
    if ma_type == 'HMA'
        maPrice := ta.hma(src, length)
        maPrice
    if ma_type == 'WMA'
        maPrice := ta.wma(src, length)
        maPrice
    if ma_type == 'VWMA'
        maPrice := ta.vwma(src, length)
        maPrice
    if ma_type == 'VWAP'
        maPrice := ta.vwap
        maPrice
    if ma_type == 'DEMA'
        e1 = ta.ema(src, length)
        e2 = ta.ema(e1, length)
        maPrice := 2 * e1 - e2
        maPrice
    if ma_type == 'T3'
        maPrice := FuncT3(src, length)
    if ma_type == "SWMA"
        maPrice := ta.swma(src)
    if ma_type == "DEMA"
        maPrice := 2 * ta.ema(src, length) - ta.ema(ta.ema(src, length), length)
    if ma_type == "RMA"
        maPrice := ta.rma(src, length)
    if ma_type == "LSMA"
        maPrice := ta.linreg(src, length, 0)
    if ma_type == "Donchian"
        maPrice := ((math.avg(ta.lowest(length), ta.highest(length))) + (math.avg(ta.lowest(length/2), ta.highest(length/2))))/2

    maPrice

MA =  getMA(MA_type, src, len)


//Main Logic//

interval_to_len = timeframe.multiplier * (timeframe.isdaily ? 1440 : timeframe.isweekly ? 1440 * 7 : timeframe.ismonthly ? 1440 * 30 : 1)

main_len = math.ceil(sensitiveness / interval_to_len)
_highest = math.min(ta.highest(high, main_len) ,close + ta.atr(46)*4)
_lowest = math.max(ta.lowest(low, main_len),close - ta.atr(46)*4)

SR = src
trend = math.sign(nz(src[1]) - nz(SR[1]))

atr = ta.rma(ta.atr(7), 14)

SR := nz(SR[1], SR) + trend * (math.abs(nz(MA, src) - nz(MA[1], MA))) * trend_para
SR := src < SR and trend > 0 ? _highest + atr*AA : src > SR and trend < 0 ? _lowest - atr*AA : SR
trend := math.sign(src - SR)
plot_SR = trend == 1 ? SR - atr*AA : SR + atr*AA

if trend>0 and close > plot_SR
    bull := true

if trend<0 and close < plot_SR
    bear := true


//////////////////
//Region : Plots//
//////////////////

SR_for_fill = plot(plot_SR, color=trend != trend[1] ? na : trend > 0 ? #65D25BFF : #FE6B69FF, title='Moving Average Stop and Reverse')
plot_SR_for_fill = plot(SR,color=color.aqua, display = display.none, editable = false)
fill(SR_for_fill, plot_SR_for_fill, color=trend != trend[1] ? na : trend > 0 ? color.new(#65D25BFF, 60) : color.new(#FE6B69FF, 60))

plotshape(show_signal and (bull and not bull[1]), title= 'Long', style = shape.labelup, location = location.belowbar, color=color.new(#65D25BFF, 20), size = size.tiny, text = 'Long', textcolor = color.white)
plotshape(show_signal and (bear and not bear[1]), title= 'Short', style = shape.labeldown, location = location.abovebar, color=color.new(#FE6B69FF, 20), size = size.tiny, text = 'Short', textcolor = color.white)

//////////////////
//Region : Alert//
//////////////////

alertcondition(bull and not bull[1], title = 'Long Signal')
alertcondition(bear and not bear[1], title = 'Short Signal')
