The MACD looks intimidating at first — three elements, two numbers, a histogram — but the core idea is simple: it measures whether the trend is speeding up or slowing down, by comparing two moving averages. After this guide you will understand every 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 is a trend indicator and a momentum indicator at the same time. 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 is positive (bullish momentum).
- Signal line: a 9-period EMA of the MACD line itself. It works as the 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: they are Appel's default. 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 is value in using what the whole market watches.
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 is the basis of the strategy you can automate.
2. Zero line crossover
When the MACD line crosses above zero, the fast average has passed the slow one — confirmation of an uptrend. Below zero, a downtrend. Slower, but more reliable than the line crossover.
3. Histogram
The histogram grows when momentum speeds up and shrinks when it slows down. 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, the MACD makes a lower high → weakening, possible reversal.
The MACD's weakness: it is built on moving averages, so it lags. In a ranging market it produces plenty of false crossovers (whipsaw). Like the RSI, the MACD shines in a trend and suffers in consolidation. Do not trade MACD crossovers in a ranging market without a filter.
03Coding the MACD
//@version=5 strategy("MACD Cruzamento", overlay=false) rapida = input.int(12, "EMA Rápida") lenta = input.int(26, "EMA Lenta") sinal = input.int(9, "Linha de Sinal") [macdLine, signalLine, hist] = ta.macd(close, rapida, lenta, sinal) plot(macdLine, "MACD", color=color.blue) plot(signalLine, "Sinal", color=color.orange) plot(hist, "Histograma", style=plot.style_histogram, color=hist >= 0 ? color.green : color.red) // strategy: line crossover if ta.crossover(macdLine, signalLine) strategy.entry("Compra", strategy.long) if ta.crossunder(macdLine, signalLine) strategy.close("Compra")
import pandas as pd def calcular_macd(precos, rapida=12, lenta=26, sinal=9): ema_rapida = precos.ewm(span=rapida, adjust=False).mean() ema_lenta = precos.ewm(span=lenta, adjust=False).mean() macd_line = ema_rapida - ema_lenta signal_line = macd_line.ewm(span=sinal, adjust=False).mean() histograma = macd_line - signal_line return macd_line, signal_line, histograma macd, sinal_l, hist = calcular_macd(df["close"]) # detects a bullish crossover on the last candle cruzou_cima = (macd.iloc[-2] < sinal_l.iloc[-2]) and \ (macd.iloc[-1] > sinal_l.iloc[-1]) if cruzou_cima: print("MACD cruzou pra cima — sinal de compra")
Shortcut: in Python, pandas-ta already has the MACD built in: df.ta.macd(fast=12, slow=26, signal=9). The manual calculation above is there so you understand the mechanics.
Combine the MACD with other indicators
See how the RSI complements the MACD to filter out false signals.
04Improving the strategy
A pure MACD crossover produces a lot of false signals. Filters that help a lot:
- Trend filter: only buy if price is above the 200 moving average and the MACD has 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 indicator on its own is enough. The MACD gains a lot when it confirms (or is confirmed by) trend and momentum from other sources. Combining them cuts down false positives.
05Frequently asked questions
What is the MACD?
It is 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 speeding up or slowing down.
What are the default parameters?
12, 26, 9: a 12-period fast average, a 26-period slow one and a 9-period signal. Defined by Gerald Appel and widely used — there is value in using what the whole market watches.
How do I 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 is not "or" — they are complementary. The MACD is more focused on trend/momentum from moving averages; the RSI on overbought/oversold. Many strategies use both: the MACD for direction, the RSI for timing/filtering.
Does the MACD work for day trading?
Yes, but adjust the periods to 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 faster indicators.