MACD EMA (by WJ)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sehweijun
// EMA MACD CODE TAKEN FROM DEFAULT INDICATOR
// I HAVE ONLY MADE SOME ADJUSTMENTS FOR VISUAL AID
// I MADE THIS FOR MY OWN USE BUT HAVE DECIDED TO PUBLISH AND SHARE IN CASE ANYBODY WANTS TO USE IT

//@version=4
study(title="MACD EMA (by WJ)", shorttitle="MACD EMA", overlay=true)

// MACD
macd_fast_length = input(title="Fast Length", group="MACD", type=input.integer, defval=12)
macd_slow_length = input(title="Slow Length", group="MACD", type=input.integer, defval=26)
macd_signal_length = input(title="Signal Smoothing", group="MACD", type=input.integer, minval = 1, maxval = 50, defval = 9)
macd_sma_source = input(title="Oscillator MA Type", group="MACD", type=input.string, defval="EMA", options=["SMA", "EMA"])
macd_sma_signal = input(title="Signal Line MA Type", group="MACD", type=input.string, defval="EMA", options=["SMA", "EMA"])
macd_fast_ma = macd_sma_source == "SMA" ? sma(close, macd_fast_length) : ema(close, macd_fast_length)
macd_slow_ma = macd_sma_source == "SMA" ? sma(close, macd_slow_length) : ema(close, macd_slow_length)
macd = macd_fast_ma - macd_slow_ma
macd_signal = macd_sma_signal == "SMA" ? sma(macd, macd_signal_length) : ema(macd, macd_signal_length)
macd_hist = macd - macd_signal

// EMA
emaLength1 = input(200, title="EMA", group="EMA", minval=1)
ema1 = ema(close, emaLength1)

plot(ema1, title="EMA", color=#2962ff, transp=0, linewidth=2, style=plot.style_circles)

// Signal Candle
barColourToggle = input(title="Bar Colour [ON/OFF]", group="Signal Candle", type=input.bool, defval=true)
backgroundColourToggle = input(title="Background Colour [ON/OFF]", group="Signal Candle", type=input.bool, defval=true)

buySignal = close[0] > ema1 and macd < 0 and crossover(macd, macd_signal)
sellSignal = close[0] < ema1 and macd > 0 and crossunder(macd, macd_signal)

barcolor( color=buySignal and barColourToggle ? #00ff0a : sellSignal and barColourToggle ? #FF0000 : na, title="Signal Candle Bar Colour" )
bgcolor( color=buySignal and backgroundColourToggle ? #00ff0a : sellSignal and backgroundColourToggle ? #FF0000 : na, transp=85, title="Signal Candle Background Colour" )

alertcondition( buySignal or sellSignal, title="MACD Signal", message="MACD Signal!")
alertcondition( buySignal, title="MACD Signal Buy", message="MACD Signal Buy!")
alertcondition( sellSignal, title="MACD Signal Sell", message="MACD Signal Sell!")