Skip to content
GetProfitable
Search
Momentum

MACD Histogram

The MACD line, its signal line and the histogram of the gap between them, coloured by side so the widening and narrowing of momentum reads before the crossover arrives.

indicator
//@version=6
indicator("MACD Histogram", "MACD Hist")

fastLen = input.int(12, "Fast length", minval = 1, group = "Settings")
slowLen = input.int(26, "Slow length", minval = 1, group = "Settings")
signalLen = input.int(9, "Signal length", minval = 1, group = "Settings")
bull = input.color(#26A69A, "Above zero", group = "Style")
bear = input.color(#EF5350, "Below zero", group = "Style")
lineCol = input.color(#787B86, "Lines", group = "Style")

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

plot(hist, "Histogram", style = plot.style_columns, color = hist >= 0 ? color.new(bull, 35) : color.new(bear, 35))
plot(macdLine, "MACD", color = lineCol, linewidth = 2)
plot(signalLine, "Signal", color = color.new(lineCol, 45))
hline(0, "Zero", color = color.new(lineCol, 60))

crossUp = ta.crossover(macdLine, signalLine)
crossDown = ta.crossunder(macdLine, signalLine)
plotshape(crossUp, "Cross up", location = location.bottom, style = shape.triangleup, color = bull, size = size.tiny)
plotshape(crossDown, "Cross down", location = location.top, style = shape.triangledown, color = bear, size = size.tiny)
alertcondition(crossUp, "MACD crossed up", "The MACD line crossed above its signal line")
alertcondition(crossDown, "MACD crossed down", "The MACD line crossed below its signal line")
This runs in TradingView, not here
Pine Script only executes inside TradingView. Paste the source into the Pine Editor and add it to a chart to see it plotted.

What it will not do

  • Two moving averages subtracted from each other still lag. The histogram turns after the move in price has begun, not before it.
  • Crossovers near the zero line come in clusters during a range, because the two averages are almost on top of each other there.
  • The histogram is unbounded and its scale depends on the instrument, so a reading of 3 means nothing on its own and cannot be compared across symbols.

Written from this description

Plot MACD with configurable fast, slow and signal lengths. Draw the histogram as columns coloured green above zero and red below, plot both lines over it, and mark where the MACD line crosses its signal.

Educational only, not financial advice. The maths is simple arithmetic on the numbers you enter; it knows nothing about your broker, fees, slippage or the market. Something wrong with it? Say so in Site Feedback.