EMA Cross Cloud
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © syntaxgeek

//@version=5
indicator(title="EMA Cross Cloud", shorttitle='EXC', overlay=true)

i_emaFastLength = input.int(title='EMA Fast Length', defval=8)
i_emaSlowLength = input.int(title='EMA Slow Length', defval=21)
i_emaMarketLength = input.int(title='EMA Market Length', defval=200)
i_emaTrailingStopLength = input.int(title='EMA Trailing Stop', defval=34)

v_emaMarket = ta.ema(close, i_emaMarketLength)
v_emaTS = ta.ema(close, i_emaTrailingStopLength)
v_emaFast = ta.ema(close, i_emaFastLength)
v_emaSlow = ta.ema(close, i_emaSlowLength)

v_uptrend = ta.crossover(v_emaFast, v_emaSlow)
v_downtrend = ta.crossunder(v_emaFast, v_emaSlow)
v_uptrendStopped = v_uptrend and low < v_emaMarket
v_downtrendStopped = v_downtrend and high > v_emaMarket

v_neutralColor = color.new(color.gray, 50)
v_trendColor = v_emaFast > v_emaSlow ? close < v_emaMarket ? v_neutralColor : color.rgb(102, 255, 0, 50) : close > v_emaMarket ? v_neutralColor : color.rgb(255, 0, 0, 50)

p_fast = plot(v_emaFast, title='Fast', color=color.new(color.green, 100))
p_slow = plot(v_emaSlow, title='Slow', color=color.new(color.red, 100))
p_market = plot(v_emaMarket, title='Market', color=color.white, linewidth=3)
p_ts = plot(v_emaTS, title='Stop', color=color.orange, style=plot.style_cross)

fill(p_fast, p_slow, color=v_trendColor)

plotchar(v_uptrend ? close : na, char='⏫', title='Uptrend')
plotchar(v_downtrend ? close : na, char='⏬', title='Downtrend')

alertcondition(v_uptrend, "EMA Cross Uptrend", "EMA Cross Uptrend {{ticker}} @ {{close}}")
alertcondition(v_downtrend, "EMA Cross Downtrend", "EMA Cross Downtrend {{ticker}} @ {{close}}")
alertcondition(v_uptrend or v_downtrend, "EMA Cross Trend Changed", "EMA Cross Trend Change {{ticker}} @ {{close}}")
alertcondition(v_uptrendStopped, "EMA Cross Uptrend Stopped", "EMA Cross Uptrend Stopped {{ticker}} @ {{low}}")
alertcondition(v_downtrendStopped, "EMA Cross Downtrend Stopped", "EMA Cross Downtrend Stopped {{ticker}} @ {{high}}")