Stoch/RSI with EMA50 Cross & HHLL
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Demha_MCX

//@version=5
indicator("Oscillators", "RSI+SRSI+HHLL+EMA50Crosover", false)

//////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////Plotting RSI//////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

rsi = ta.rsi(close,14)
plot(rsi, color=color.white, title="RSI",linewidth=2)

/////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////Plotting SRSI/////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

// Input data to configure on chart
smoothK = input.int (defval=3, title = "Stochastic %K", minval=1,maxval=100, step=1)
smoothD = input.int (defval=3, title = "Stochastic %D", minval=1,maxval=100, step=1)
lengthRSI = input.int (defval=14, title = "RSI Length", minval=1, maxval=100,step=1)
lengthStoch = input.int (defval=14, title = "Stochastic Length",maxval=100, minval=1, step=1)
src4 = input(close, title="RSI Source")

// Calculate indicator
rsi1 = ta.rsi(src4, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)

// Drawing - plot on chart
plot(k, color=color.blue,title = "%k")
plot(d, color=color.red,title = "%d")
h0 = hline(80, linestyle=hline.style_dotted,title = "Oscillator Upper Band")
h1 = hline(20, linestyle=hline.style_dotted,title = "Oscillator Lower Band")
fill(h0, h1, color.new(color.purple, 80),title = "Oscillator Background")

/////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////Plotting Crossover of EMA50 with CMP//////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

ema50CrossOver = ta.crossover(close,ta.ema(close,50))
ema50CrossUnder = ta.crossunder(close,ta.ema(close,50))

bgcolor(title="ema50_CrossOver",color= ema50CrossOver ? color.new(color.green,80) : na)
bgcolor(title="ema50_CrossUnder",color=ema50CrossUnder ? color.new(color.red,80)  : na)

/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////Plotting HH & LL////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

//  ||---   Fractal Recognition:
filterBW = input(false, title="filter Bill Williams Fractals:")


//  ||---   Fractal Recognition Functions:  ---------------------------------------------------------------||
isRegularFractal(mode) =>
    ret = mode == 1 ? high[4] < high[3] and high[3] < high[2] and high[2] > high[1] and high[1] > high[0] : mode == -1 ? low[4] > low[3] and low[3] > low[2] and low[2] < low[1] and low[1] < low[0] : false

isBWFractal(mode) =>
    ret = mode == 1 ? high[4] < high[2] and high[3] <= high[2] and high[2] >= high[1] and high[2] > high[0] : mode == -1 ? low[4] > low[2] and low[3] >= low[2] and low[2] <= low[1] and low[2] < low[0] : false

filteredtopf = filterBW ? isRegularFractal(1) : isBWFractal(1)
filteredbotf = filterBW ? isRegularFractal(-1) : isBWFractal(-1)

ShowHHLL = input(true)

higherhigh = filteredtopf == false ? false : (ta.valuewhen(filteredtopf == true, high[2], 1) < ta.valuewhen(filteredtopf == true, high[2], 0) and ta.valuewhen(filteredtopf == true, high[2], 2) < ta.valuewhen(filteredtopf == true, high[2], 0))
lowerhigh = filteredtopf == false ? false : (ta.valuewhen(filteredtopf == true, high[2], 1) > ta.valuewhen(filteredtopf == true, high[2], 0) and ta.valuewhen(filteredtopf == true, high[2], 2) > ta.valuewhen(filteredtopf == true, high[2], 0))
higherlow = filteredbotf == false ? false : (ta.valuewhen(filteredbotf == true, low[2], 1) < ta.valuewhen(filteredbotf == true, low[2], 0) and ta.valuewhen(filteredbotf == true, low[2], 2) < ta.valuewhen(filteredbotf == true, low[2], 0)   )
lowerlow = filteredbotf == false ? false : (ta.valuewhen(filteredbotf == true, low[2], 1) > ta.valuewhen(filteredbotf == true, low[2], 0) and ta.valuewhen(filteredbotf == true, low[2], 2) > ta.valuewhen(filteredbotf == true, low[2], 0))

plotshape(ShowHHLL ? higherhigh : na, title='Higher High', style=shape.square, location=location.top, color=color.green, text="[HH]",textcolor=color.green, offset=-2)
plotshape(ShowHHLL ? higherlow : na, title='Higher Low', style=shape.circle, location=location.top, color=color.yellow, text="[HL]",textcolor=color.yellow, offset=-2)
plotshape(ShowHHLL ? lowerhigh : na, title='Lower High', style=shape.circle, location=location.bottom, color=color.yellow, text="[LH]",textcolor=color.yellow, offset=-2)
plotshape(ShowHHLL ? lowerlow : na, title='Lower Low', style=shape.square, location=location.bottom, color=color.red, text="[LL]",textcolor=color.red, offset=-2)

alertcondition(higherhigh, title='Higher High', message='{{ticker}} - Higher High on the {{interval}}')
alertcondition(lowerlow, title='Lower Low', message='{{ticker}} - Lower Low on the {{interval}}')

/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////END OF FILE/////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
