Data Analytic Investments
Trend & MomentumBeginnerMACD Racers

MACD

Moving Average Convergence Divergence

Tracks the relationship between two EMAs to reveal momentum shifts and trend changes.

4H, Daily, Weekly
Crypto, Forex, Equities
Pine Script v5

What is it?

Developed by Gerald Appel in the late 1970s, MACD subtracts a 26-period EMA from a 12-period EMA to produce the MACD line. A 9-period EMA of the MACD line is then plotted as the 'signal line'. The difference between the MACD line and the signal line is displayed as a histogram. When the MACD line crosses above the signal line, it is a bullish reading; when it crosses below, bearish. The histogram shows the distance between the two lines — expanding bars mean momentum is accelerating; shrinking bars mean it is fading.

Common pitfalls

  • MACD is a lagging indicator — it is based on moving averages of past prices. By the time a crossover fires, a significant portion of the move may already be over.
  • In choppy, sideways markets MACD generates frequent whipsaw crossovers. Always check the broader trend context before acting on a cross.
  • The histogram is not the MACD line — it is the difference between MACD and reading. Many beginners misread histogram bars as the MACD value itself.
  • Default settings (12/26/9) were designed for daily stock charts. On crypto 4H charts, many traders use 6/13/5 for faster response.
  • A MACD crossover below the zero line is a bearish reading even if it looks like a 'bullish cross' — direction relative to zero matters as much as the cross itself.

Indicator Rider — MACD Racers

Two figures move along the MACD line and its moving average — where the two lines meet, a marker appears.

Free code template

Paste directly into TradingView Pine Editor → Add to chart.

Pine Script v5
/indicator("MACD — DAI Template", shorttitle="DAI MACD", overlay=false)

// ── Inputs ──────────────────────────────────────────────────────────────────
fastLen   = input.int(12, "Fast EMA",   minval=1)
slowLen   = input.int(26, "Slow EMA",   minval=1)
sigLen    = input.int(9,  "reading EMA", minval=1)
src       = input.source(close, "Source")

// ── Calculation ─────────────────────────────────────────────────────────────
[macdLine, signalLine, histLine] = ta.macd(src, fastLen, slowLen, sigLen)

// ── Colours ─────────────────────────────────────────────────────────────────
histColor = histLine >= 0 ?
  (histLine > histLine[1] ? #22c55e : color.new(#22c55e, 40)) :
  (histLine < histLine[1] ? #ef4444 : color.new(#ef4444, 40))

// ── Plots ───────────────────────────────────────────────────────────────────
plot(macdLine,   "MACD",   color=#3b82f6, linewidth=2)
plot(signalLine, "reading", color=#f97316, linewidth=1)
plot(histLine,   "Hist",   style=plot.style_histogram, color=histColor, linewidth=3)
hline(0, "Zero", color=color.new(#94a3b8, 50))

// ── Cross readings ────────────────────────────────────────────────────────────
bullCross = ta.crossover(macdLine,  signalLine)
bearCross = ta.crossunder(macdLine, signalLine)
plotshape(bullCross, "Bull Cross", shape.triangleup,   location.bottom, #22c55e, size=size.small)
plotshape(bearCross, "Bear Cross", shape.triangledown, location.top,    #ef4444, size=size.small)

alertcondition(bullCross, "MACD Bull Cross", "MACD crossed above reading")
alertcondition(bearCross, "MACD Bear Cross", "MACD crossed below reading")