// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains
//@version=5
indicator(title="Custom MACD", shorttitle="Custom MACD", overlay = false)
// @keywords: custom-macd, macd-line, signal-line, histogram, zero-line, sma-ema-toggle, oscillator-ma-type, signal-smoothing, smma-trend-filter, background-regime, bullish-alert, bearish-alert, fast-length, slow-length, configurable-source, histogram-growth, trend-background, moving-average-filter, macd-alerts, oscillator-pane
// Getting inputs
fast_length = input(title="Fast Length", defval=12)
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="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
// 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(#26A69A, "Above Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
// Calculating
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
hline(0, "Zero Line", color=color.new(#787B86, 50))
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)))
plot(macd, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)
// ma
ma_length = input.int(150, title = 'MA Length', group = 'MA Settings', inline = '1')
var float smma = 0.0
smma := na(smma[1]) ? ta.sma(close, ma_length) : (smma[1] * (ma_length - 1) + close) / ma_length
bgcolor(hl2 > smma ? color.new(#26A69A,80) : color.new(#FF5252,80))
alertcondition(hl2 > smma and hist > hist[1] and hist <= 0 , 'Bullish MACD Alert', 'Bullish MACD')
alertcondition(hl2 < smma and hist < hist[1] and hist >= 0, 'Bearish MACD Alert', 'Bearish MACD')
// barcolor(hl2 > ma_value and hist > hist[1] ? color.yellow : na)