SAR+RSI+EMAs Signals
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AliSignals

//@version=5
indicator('SAR+RSI+EMAs AliSignals', overlay=true)
fast = input(7)
slow = input(21)
rsiL = input(14)
boolVariable = input(title='use high&low instead of close for fast EMA', defval=false)
filter = input(title='use MATD as a filter', defval=true)
RSIOL = input.float(defval = 30, minval=0.1 ,maxval=99.9, title= " RSI oversold level")
sar = ta.sar(0.02, 0.02, 0.2)
// make EMAfast an easier one.
srch = if boolVariable == true
    high
else
    close

srcl = if boolVariable == true
    low
else
    close


emashorthigh = ta.ema(srch, fast)
emashortlow = ta.ema(srcl, fast)
emashort = ta.ema(close, fast)

emalong = ta.ema(close, slow)

rsi = ta.rsi(close, rsiL)


buy1 = if ta.crossover(rsi, 50) or ta.crossover(rsi, RSIOL)
    1
else
    na




sell1 = if ta.crossunder(rsi, 50) or ta.crossunder(rsi, 100-RSIOL)
    1
else
    na


buy = if buy1 == 1 and emashorthigh > emalong and close > emashort and close > sar
    1
else
    na


sell = if sell1 == 1 and emashortlow < emalong and close < emashort and close < sar
    1
else
    na

// adding MATD filter
//declaring function to use different types of MA
maf(source, length, type) =>
    type == 'SMA' ? ta.sma(source, length) : type == 'EMA' ? ta.ema(source, length) : type == 'SMMA (RMA)' ? ta.rma(source, length) : type == 'WMA' ? ta.wma(source, length) : type == 'VWMA' ? ta.vwma(source, length) : na
// inputs
Malength = input(100, title = "MA Length")
src = input(close, title = "MA Source")
type = input.string(     'WMA', title='MA type', inline='MA Type', options=['SMA', 'EMA', 'SMMA (RMA)', 'WMA', 'VWMA', 'linreg'])
Madistancelength = input(20, title = "Distance Length")
srcdistance = input(hl2, title = "Distance Source")
smoothing = input(20, title = "smooothing")




// running fuction 
ma = maf(src, Malength, type)




// calculating distance
distance = srcdistance - ma

// Average of distances
smadistance = ta.sma(distance, Madistancelength)

// MATD and smoothing of it
MATD = srcdistance - smadistance
Smoothed = ta.sma(MATD, smoothing)



max = math.max(Smoothed,ma)
min = math.min(Smoothed,ma)

B = if close > max and buy == 1
    1
else
    na

S = if close < min and sell == 1
    1
else
    na





b = if filter == true
    B
else
    buy


s = if filter == true
    S
else
    sell




plotshape(b, title='Buy', style=shape.labelup, location=location.belowbar, size=size.normal, text='Buy', textcolor=color.new(color.white, 0), color=color.new(color.green, 0))
plotshape(s, title='Sell', style=shape.labeldown, location=location.abovebar, size=size.normal, text='Sell', textcolor=color.new(color.white, 0), color=color.new(color.red, 0))
plot(sar, title= "sar")
plot(emalong, title= "emalong",color=color.new(color.red, 0))














