Moving Average Cross and RSI
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Uniden202

//Version 15 adds the condition to the define long and short. Added and rsi_ema >= upper  ; rsi_ema <= lower.
//Version 16 changed condition of long/short from rsi_ema to rsi and removed offset from within the plot of the dots to algin with original ema mac cross 
//Version 17 changed condition to add the ta.cross therefore it only signals when all 3 conditons are met and does not repeat
//Version 18 Added shapes to candles for entry/exits

//@version=5
indicator('Moving Average Cross and RSI', overlay=true, precision=0)

//variable
var indicatorshort = 0.0
var indicatorlong = 0.0

// Define variables for RSI
rsi_length = 8
rsi_source = hl2
rsi_ema_length = 3

// Calculate RSI
rsi = ta.rsi(rsi_source, rsi_length)
upper = 64
lower = 30

// Calculate EMA for RSI
rsi_ema = ta.ema(rsi, rsi_ema_length)

// Plot RSI and EMA and Vwap
plot(rsi, style=plot.style_line, color=color.rgb(32, 146, 240), linewidth=2, title='RSI', editable = true)
plot(rsi_ema, style=plot.style_line, color=color.rgb(255, 82, 82), linewidth=2, title='EMA(RSI)', editable = true)

plot(upper, title='Overbought', color=color.rgb(218, 218, 218), linewidth=1, style=plot.style_circles)
plot(lower, title='Overbought', color=color.rgb(218, 218, 218), linewidth=1, style=plot.style_circles)

plot(indicatorshort, 'Short Dot', color=color.rgb(240, 77, 113), style=plot.style_circles, linewidth=3)
plot(indicatorlong, 'Long Dot', color=color.rgb(36, 250, 17, 17), style=plot.style_circles, linewidth=3)

plotshape(indicatorlong, 'Long X', color=color.rgb(36, 250, 17, 17), location = location.abovebar, size=size.small)
plotshape(indicatorshort, 'Short X', color=color.rgb(240, 7, 113), location = location.belowbar, size=size.small)



plot(ta.vwap(hlc3), title = "VWAP", color=color.rgb(255, 255, 255), linewidth = 2)

// Define variables for MAC
ema8 = ta.ema(close, 10)
ema24 = ta.ema(close, 30)
SMMA = ta.sma(hlc3, 21)


// Plot EMAs
plot(ema8, title = "ema8", color=color.rgb(255, 82, 82, 100), editable = true)
plot(ema24, title = "ema24", color=color.rgb(33, 150, 243, 100), editable = true)

// Define condition to long
longcondition = ema8 > ema24 and rsi >= upper and ta.cross(ema8,ema24) and close[1] > ta.vwap(hlc3)
if longcondition
    indicatorlong := 100
else
    indicatorlong := na

// Define Condition to Short
Shortcondition = ema8 < ema24 and rsi <= lower and ta.cross(ema8,ema24) and close[1]  <  ta.vwap(hlc3)
if Shortcondition
    indicatorshort := 1
else
    indicatorshort := na

// Entry and Exit conditions for the trade
long_entry = ema8 > ema24 and ema8[1] <= ema24[1]
short_entry = ema8 < ema24 and ema8[1] >= ema24[1]
closelong = ema8 < ema24 and not long_entry[2]
closeshort = ema8 > ema24 and not short_entry[2]

// Plot the direction of the trade
plot(indicatorshort, title='indicatorshort', color=color.rgb(78, 177, 81, 100))
plot(indicatorlong, title='indicatorlong', color=color.rgb(78, 177, 81, 100))
