MON–FRI 9:00–16:00 · SAT/SUN CLOSED | 카톡 tujac | 213-599-7359 LA · ORANGE COUNTY · ONLINE WORLDWIDE
TUJAC TRADING EDUCATION

MACD (Moving Average Convergence Divergence)

2025-10-09
//@version=6
indicator(title="TJ, MACD (Moving Average Convergence Divergence)", 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, 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)
// 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

alertcondition(hist[1] >= 0 and hist < 0, title = 'Rising to falling', message = 'The MACD histogram switched from a rising to falling state')
alertcondition(hist[1] <= 0 and hist > 0, title = 'Falling to rising', message = 'The MACD histogram switched from a falling to rising state')

hline(0, "Zero Line", color = color.new(#787B86, 50))
plot(hist, title = "Histogram", style = plot.style_columns, color = (hist >= 0 ? (hist[1] < hist ? color.new(#ffffff, 0) : color.new(#ffffff, 50)) : (hist[1] < hist ? color.new(#2962ff, 50) : color.new(#2962ff, 0))))
plot(macd,   title = "MACD",   color = #2962FF, display=display.none)
plot(signal, title = "Signal", color = #FF6D00, display=display.none)