Color Changing MACD
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FX365_Thailand

//@version=5

//Release note
//v24.0 Released


indicator(title="Color Changing MACD", shorttitle="Color MACD")

//User inputs
fast_length = input(title="Fast Length", defval=12,group="Common")
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="SMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="SMA", options=["SMA", "EMA"])
i_signal = input.bool(true,title="Show GC/DC Signals?")


// Plot colors
col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(color.new(color.green,20), "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(color.new(color.green,80), "Fall", group="Histogram", inline="Above")
col_grow_below = input(color.new(color.red,80), "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(color.new(color.red,20), "Fall", group="Histogram", inline="Below")


//Logic

//MACD calculation
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal


//Determine colors
color_macd = (macd > signal) and (hist > hist[1]) ? color.green : (macd > signal) and (hist < hist[1]) ? #9c27b0 : (macd < signal) and (hist < hist[1]) ? color.red : (macd < signal) and (hist > hist[1]) ? #9c27b0 : na


//Plot
// a=plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
color_bull = color.from_gradient(value=hist, bottom_value = 0,top_value = 0.3 , top_color = color.new(color.green,80), bottom_color = color.new(color.green,20) )
color_bear = color.from_gradient(value=hist, top_value = 0, bottom_value = -100, top_color = color.new(color.red,20), bottom_color = color.new(color.red,80) )

a=plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
b=plot(macd, title="MACD", color=color_macd,display=display.none)
c=plot(signal, title="Signal", color=color_macd,display=display.none)

//Fill
//Condition
bull = macd > signal
bear = macd < signal

color_top = (macd > signal) and (hist > hist[1]) ? color.new(#e1bee7,80) : (macd > signal) and (hist < hist[1]) ? color.new(#9c27b0,80) : (macd < signal) and (hist < hist[1]) ? color.new(color.red,80) : (macd < signal) and (hist > hist[1]) ? color.new(#e1bee7,80) : na
color_bottom = (macd > signal) and (hist > hist[1]) ? color.new(#0ef30e,0) : (macd > signal) and (hist < hist[1]) ? color.new(#9c27b0,0) : (macd < signal) and (hist < hist[1]) ? color.new(color.red,0) : (macd < signal) and (hist > hist[1]) ? color.new(#9c27b0,0) : na

fill(b,c, top_value=macd, bottom_value = signal, top_color = color_top, bottom_color = color_bottom)

//signals  
gc = ta.crossover(macd, signal)
dc = ta.crossunder(macd, signal)

//Plot signals 
plotshape(i_signal ? gc : na,style= shape.triangleup, color=color.new(color.green,0),title="Golden Cross",location=location.bottom,size=size.tiny)
plotshape(i_signal ? dc : na,style= shape.triangledown, color=color.new(color.red,0),title="Death Cross",location=location.top,size=size.tiny)

//alert conditions
alertcondition(gc, title="Golden Cross", message="Golden Cross Occurred")
alertcondition(dc, title="Death Cross", message="Death Cross Occurred")
