MACD on TradingView — Pine Script v5 Source

The built-in ta.macd and a coloured histogram

MACD in Pine Script
MACD in Pine Script

Pine Script has a dedicated ta.macd that hands you all three components in one call, so reproducing the MACD panel from the explainer is quick. We’ll do the built-in version, then the from-scratch one, and add the classic four-colour histogram.

What you’ll need

  • A TradingView account (free tier is fine)
  • The Pine Editor

The built-in version

//@version=5
indicator("AlgoGen MACD", shorttitle="AG MACD", overlay=false)

fastLen   = input.int(12, "Fast length")
slowLen   = input.int(26, "Slow length")
signalLen = input.int(9,  "Signal length")

[macdLine, signalLine, histLine] = ta.macd(close, fastLen, slowLen, signalLen)

// Four-colour histogram: brighter when growing, muted when shrinking.
histUp   = histLine >= 0
growing  = histLine >= histLine[1]
histCol  = histUp ? (growing ? #0ca30c : #a6e0a6) : (growing ? #f0a6a6 : #d03b3b)

plot(histLine,   "Histogram", style=plot.style_columns, color=histCol)
plot(macdLine,   "MACD",   color=#2a78d6, linewidth=2)
plot(signalLine, "Signal", color=#eb6834, linewidth=2)
hline(0, "Zero", color=color.gray)

ta.macd returns the MACD line, the signal line, and the histogram as a tuple — exactly the three series in the output chart. The four-colour trick shades the histogram by whether it’s above/below zero and growing/shrinking, which is a popular way to see momentum acceleration at a glance.

The from-scratch version

Nothing hidden — ta.ema is the same recursive EMA:

//@version=5
indicator("AlgoGen MACD (manual)", overlay=false)
macdLine   = ta.ema(close, 12) - ta.ema(close, 26)
signalLine = ta.ema(macdLine, 9)
plot(macdLine - signalLine, "Histogram", style=plot.style_columns)
plot(macdLine, color=#2a78d6, linewidth=2)
plot(signalLine, color=#eb6834, linewidth=2)

Adding it and getting alerts

  1. Paste into the Pine Editor and Add to chart (it opens in its own pane).
  2. For a signal-cross alert:
alertcondition(ta.crossover(macdLine, signalLine),  "MACD bull cross", "MACD crossed up")
alertcondition(ta.crossunder(macdLine, signalLine), "MACD bear cross", "MACD crossed down")

Watch the tuple order

A classic Pine footgun: ta.macd returns the three series in the order [macdLine, signalLine, histLine]. Swap two of those names by accident and your “MACD” plot is silently the signal line, or your histogram is doubled. Destructure it in that exact order, and sanity-check against the output chart above — the histogram should be zero exactly where the MACD and signal lines touch.

Gotchas

  • Signal smooths the MACD line, not price. ta.macd handles this for you; in the manual version make sure ta.ema’s input is macdLine.
  • Closed-bar signals. Gate crossover alerts on barstate.isconfirmed to avoid intrabar flip-flopping.
  • EMA seeding. Pine’s ta.ema may differ from other platforms by a hair in the first bars due to seeding, then converges — normal, not a bug.

Same indicator elsewhere: Python, MQL5, EasyLanguage, NinjaScript. Then test it in AlgoGen.


This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.

Sources and further reading

  1. Gerald Appel (CMT Association)
  2. Pine Script built-ins (TradingView)

Historical research from the Algogen archive. Not investment advice.

Blog

Have a strategy idea?

Describe it in plain English, preview where your rules fire, then decide whether the evidence is worth a backtest.

Keep me postedHow it works