2 EMA Pullback
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Germangroa

//@version=5
indicator(title='2 EMA Pullback', overlay=true)

//=======  User input  ========
//EMA 1
res1 = input.timeframe(title='EMA 1 Time Frame', defval='60', group="Long EMA")
len1 = input(title='EMA 1 Length', defval=26, group="Long EMA")
//col = input(title='Color', defval=true, group="Long EMA")
smooth1 = input(title='Smooth ?', defval=true, group="Long EMA")

//EMA 2
res2 = input.timeframe(title='EMA 2 Time Frame', defval='60', group="Short EMA")
len2 = input(title='EMA 2 Length', defval=12, group="Short EMA")
//col = input(title='Color', defval=true, group="Short EMA")
smooth2 = input(title='Smooth ?', defval=true, group="Short EMA")



//=======  Calculate EMAs  ========
//EMA 1
ema1 = ta.ema(close, len1)
emaSmooth1 = request.security(syminfo.tickerid, res1, ema1, barmerge.gaps_on, barmerge.lookahead_off)
emaStep1 = request.security(syminfo.tickerid, res1, ema1, barmerge.gaps_off, barmerge.lookahead_off)


//EMA 2
ema2 = ta.ema(close, len2)
emaSmooth2 = request.security(syminfo.tickerid, res2, ema2, barmerge.gaps_on, barmerge.lookahead_off)
emaStep2 = request.security(syminfo.tickerid, res2, ema2, barmerge.gaps_off, barmerge.lookahead_off)


UPtrend = ema2 > ema1
DOWNtrend = ema2 < ema1

//Draw EMA
plot(smooth1 ? emaSmooth1 : emaStep1, color=color.new(color.red, 40), linewidth=4, title='EMA 1', offset=15)
plot(smooth2 ? emaSmooth2 : emaStep2, color=color.new(color.blue, 40), linewidth=2, title='EMA 2', offset=15)



//================ Stoch RSI ==================
smoothK = input.int(3, "K", minval=1)
smoothD = input.int(3, "D", minval=1)
lengthRSI = input.int(14, "RSI Length", minval=1)
lengthStoch = input.int(14, "Stochastic Length", minval=1)
src = input(close, title="RSI Source")
rsi1 = ta.rsi(src, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)


LongStrength = k > d
ShortStrength = k < d


//=========== RSI =====================
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"


Xover = ta.crossover(rsi, 30) and rsi[1] < rsi
Xunder = ta.crossunder(rsi, 70) and rsi[1] > rsi

//========== Plots & Entrys =========

lo = LongStrength and Xover
sh = ShortStrength and Xunder

Long = lo and UPtrend 
Short = sh and DOWNtrend

plotshape(lo ? 1 : na, style=shape.triangleup, color=color.new(color.green, 0),
     location=location.belowbar, title='Bullish Signal', text="L", textcolor=color.new(color.white, 0))
plotshape(sh ? 1 : na, style=shape.triangledown, color=color.new(color.purple, 0),
     location=location.abovebar, title='Bearish Signal',  text='S', textcolor=color.new(color.white, 0))


