⬡ INDICATOR · WITH CODE · 12 MIN READ

MACD: the indicator and the automatable strategy.

One of the most powerful indicators — and one of the most misread. Let's break down the three parts (line, signal, histogram), the crossovers, divergences, and the ready-to-use bot code.

By the RoboTraderIA Team· updated May 2026· intermediate level

The MACD looks intimidating at first — three elements, two numbers, a histogram — but the core idea is simple: it measures whether the trend is accelerating or decelerating, by comparing two moving averages. After this guide, you'll understand each part and have the code to automate the classic strategy.

01What the MACD is

MACD stands for Moving Average Convergence Divergence, created by Gerald Appel in the late 1970s. It's both a trend and a momentum indicator. It has three components:

  • MACD line: the difference between the 12-period exponential moving average (EMA) and the 26-period one. When the fast one is above the slow one, it's positive (bullish momentum).
  • Signal line: a 9-period EMA of the MACD line itself. It acts as a trigger.
  • Histogram: the difference between the MACD line and the signal line. It shows the strength of the move visually.

The numbers 12, 26, 9: these are Appel's defaults. 12 = fast average, 26 = slow average, 9 = signal line. You can adjust them, but these values are so widespread that many traders react to them — there's value in using what the whole market watches.

zero line histogram turns positive MACD line signal line cross ↑ = buy
The MACD line (blue) crosses the signal line (gold) and the histogram turns positive — the classic buy signal.

02The MACD signals

1. Line crossover (the classic)

The most-used signal: when the MACD line crosses above the signal line → buy. When it crosses below → sell. It's the basis of the automatable strategy.

2. Zero-line crossover

When the MACD line crosses above zero, the fast average has overtaken the slow one — confirmation of an uptrend. Below, a downtrend. Slower, but more reliable than the line crossover.

3. Histogram

The histogram grows when momentum accelerates and shrinks when it decelerates. A shrinking histogram (even while still positive) warns that the rally is losing strength — it anticipates the crossover.

4. Divergences

As with the RSI, divergence between price and MACD is powerful: price makes a higher high, MACD makes a lower high → weakening, possible reversal.

The MACD's weakness: it's based on averages, so it lags. In a ranging market it generates many false crossovers ("whipsaw"). Like the RSI, the MACD shines in a trend and suffers in consolidation. Don't trade MACD crossovers in a ranging market without a filter.

03Coding the MACD

Pine Script (TradingView)
//@version=5
strategy("MACD Crossover", overlay=false)

fast = input.int(12, "Fast EMA")
slow = input.int(26, "Slow EMA")
sig  = input.int(9,  "Signal Line")

[macdLine, signalLine, hist] = ta.macd(close, fast, slow, sig)

plot(macdLine, "MACD", color=color.blue)
plot(signalLine, "Signal", color=color.orange)
plot(hist, "Histogram", style=plot.style_histogram,
     color=hist >= 0 ? color.green : color.red)

// strategy: line crossover
if ta.crossover(macdLine, signalLine)
    strategy.entry("Buy", strategy.long)
if ta.crossunder(macdLine, signalLine)
    strategy.close("Buy")
Python (with pandas)
import pandas as pd

def compute_macd(prices, fast=12, slow=26, signal=9):
    ema_fast = prices.ewm(span=fast, adjust=False).mean()
    ema_slow = prices.ewm(span=slow, adjust=False).mean()
    macd_line   = ema_fast - ema_slow
    signal_line = macd_line.ewm(span=signal, adjust=False).mean()
    histogram   = macd_line - signal_line
    return macd_line, signal_line, histogram

macd, signal_l, hist = compute_macd(df["close"])

# detect a bullish crossover on the last candle
crossed_up = (macd.iloc[-2] < signal_l.iloc[-2]) and \
             (macd.iloc[-1] > signal_l.iloc[-1])
if crossed_up:
    print("MACD crossed up — buy signal")

Shortcut: in Python, pandas-ta has MACD built in: df.ta.macd(fast=12, slow=26, signal=9). The manual calculation above is for understanding the mechanics.

Combine MACD with other indicators

See how the RSI complements the MACD to filter out false signals.

See the RSI guide →

04Improving the strategy

A pure MACD crossover generates many false signals. Filters that improve it a lot:

  • Trend filter: only buy if price is above the 200 moving average and MACD crossed up.
  • Zero-line filter: only validate the crossover if it happens above zero (already in an uptrend).
  • RSI confirmation: MACD crossover + RSI above 50 = a stronger signal.

Principle: no single indicator is enough. The MACD gains a lot when it confirms (or is confirmed by) trend and momentum from other sources. Combination reduces false positives.

05Frequently asked questions

What is the MACD?

It's a momentum and trend indicator that shows the relationship between two exponential moving averages (12 and 26), with a signal line (9) and a histogram. It measures whether the trend is accelerating or decelerating.

What are the default parameters?

12, 26, 9: a 12-period fast average, a 26-period slow one, a 9-period signal. Defined by Gerald Appel and widely used — there's value in using what the whole market watches.

How do you generate a buy signal?

The classic one is when the MACD line crosses above the signal line (buy) or below it (sell). The zero-line crossover and the histogram complement it. Always filter with the trend to avoid false signals.

MACD or RSI?

It's not "either/or" — they're complementary. MACD is more focused on the trend/momentum of averages; RSI on overbought/oversold. Many strategies use both: MACD for direction, RSI for timing/filtering.

Does MACD work in day trading?

Yes, but adjust the periods for the short timeframe and watch out for the inherent lag. In very fast day trading (scalping), the MACD's lag can be a problem — combine it with more responsive indicators.