Moving Average Convergence Divergence with Rate of Change
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Bhangerang

//@version=5
indicator(title="Moving Average Convergence Divergence with Rate of Change", shorttitle="MACD with Rate of Change", timeframe="", timeframe_gaps=true)


// 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"])


// 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


bool rate_increasing = math.abs(hist[1]-hist[2]) < math.abs(hist-hist[1]) ? true : false

// 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_rate_increase = input(#00332a, "Above   Grow Rate Increase", group="Histogram", inline="Above and Increasing")
col_fall_above_rate_increase = input(#B2DFDB, "Fall Rate Increase", group="Histogram", inline="Above and Increasing")
col_grow_below_rate_increase = input(#FFCDD2, "Below Grow Rate Increase", group="Histogram", inline="Below and Increasing")
col_fall_below_rate_increase = input(#801922, "Fall Rate Increase", group="Histogram", inline="Below and Increasing")

col_grow_above_rate_decrease = input(#056656, "Above   Grow Rate Decrease", group="Histogram", inline="Above and Decreasing")
col_fall_above_rate_decrease = input(#42bda8, "Fall Rate Decrease", group="Histogram", inline="Above and Decreasing")
col_grow_below_rate_decrease = input(#f77c80, "Below Grow Rate Decrease", group="Histogram", inline="Below and Decreasing")
col_fall_below_rate_decrease = input(#f23645, "Fall Rate Decrease", group="Histogram", inline="Below and Decreasing")


plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? (rate_increasing ? col_grow_above_rate_increase : col_grow_above_rate_decrease) : (rate_increasing ? col_fall_above_rate_increase : col_fall_above_rate_decrease)) : (hist[1] < hist ? (rate_increasing ? col_grow_below_rate_increase : col_grow_below_rate_decrease) : (rate_increasing ? col_fall_below_rate_increase : col_fall_below_rate_decrease))))
plot(macd, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)
