//@version=6
indicator("MACD_RD", timeframe="", timeframe_gaps=true)
// MACD_RD - Moving Average Convergence Divergence (Ryan DeBraal)
// This indicator plots a standard MACD along with a color-adaptive histogram and
// integrated momentum-shift alerts. It preserves the normal MACD structure while
// improving visual clarity and signal recognition.
//
// FEATURES
// -----------------------------------------------------------------------------
// • Standard MACD Calculation
// - Fast MA (12 by default)
// - Slow MA (26)
// - Signal line (9)
// - Choice between SMA/EMA for both MACD and Signal smoothing
//
// • Color-Changing Histogram
// - Green shades for positive momentum
// - Red shades for negative momentum
// - Lighter/darker tones depending on whether momentum is increasing or fading
// - 50% opacity for improved readability
//
// • Crossover-Based MACD Line Coloring
// - MACD line turns green on bullish cross (MACD > Signal)
// - MACD line turns red on bearish cross (MACD < Signal)
// - Default blue when no crossover occurs
//
// • Momentum-Shift Alerts
// - Alerts when histogram flips direction
//
// PURPOSE
// -----------------------------------------------------------------------------
// This MACD version emphasizes momentum shifts and trend transitions by
// highlighting subtle histogram changes and providing clean crossover visuals.
// Ideal for:
// • Identifying early momentum reversals
// • Filtering breakout/trend setups
// • Confirming trend continuation vs exhaustion
// Inputs
lineThickness = input.int(2)
fast_length = input.int(title="Fast Length", defval=12)
slow_length = input.int(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing", minval=1, maxval=50, defval=9, display=display.data_window)
sma_source = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA","EMA"], display=display.data_window)
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA","EMA"], display=display.data_window)
// MACD Calculations
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
// Histogram reversal alerts
alertcondition(hist[1] >= 0 and hist < 0, title="Rising to falling", message="MACD histogram switched from rising to falling")
alertcondition(hist[1] <= 0 and hist > 0, title="Falling to rising", message="MACD histogram switched from falling to rising")
// Zero line
hline(0, "Zero Line", color=color.new(#787B86, 50))
// Histogram with opacity 50%
plot(hist, title="Histogram", style=plot.style_columns,
color=color.new(hist >= 0 ? (hist[1] < hist ? #26A69A : #B2DFDB)
: (hist[1] < hist ? #FFCDD2 : #FF5252), 80))
// MACD crossover logic
bullCross = ta.crossover(macd, signal)
bearCross = ta.crossunder(macd, signal)
// MACD line color on crossovers
macdColor = bullCross ? color.new(color.white, 0) : bearCross ? color.new(color.white, 0) : color.new(color.orange, 0)
// Plot MACD and Signal
plot(macd, title="MACD", color=macdColor, linewidth=lineThickness)
plot(signal, title="Signal", color=color.new(color.gray, 0), linewidth=lineThickness)