VALYNOXSTRATEGY RESEARCH TECHNOLOGY
AlphaPrimeAlphaPrime
TRADING RESEARCH CLUB / Indicators and signals

MACD indicator explained: calculation, signals and settings

MACD is the gap between a 12-period and a 26-period exponential moving average, with a 9-period signal line and a histogram on top. Its crossovers, zero line and divergences only become useful once you write them as exact rules and test them with trading costs.

By · Published · updated · Editorial policy

Contents
  1. What is the MACD indicator?
  2. How is MACD calculated?
  3. How do you read the MACD lines and histogram?
  4. What do the MACD settings 12, 26, 9 change?
  5. Why does MACD give late and false signals?
  6. How do you turn MACD into a rule you can backtest?
  7. How do you test many MACD settings without fooling yourself?
  8. Frequently asked questions
  9. References

What is the MACD indicator?

MACD (moving average convergence divergence) is a momentum indicator that subtracts a 26-period exponential moving average from a 12-period one. A 9-period average of that difference forms the signal line, and the gap between the two lines is the histogram. Traders read line crossovers, zero-line crossings and divergence from price.

Picture two walkers following the price, one fast and one slow. When the price starts to climb, the fast walker reacts first and pulls ahead. The MACD line is the distance between the two walkers. The name comes from this: the two averages move apart (diverge) when a move gains strength and move together (converge) when it fades.

The histogram adds a second layer. A car's speedometer shows your speed. The push you feel in your seat shows whether you are still speeding up. The MACD line is the speed, and the histogram is that push.

The parts of a MACD chart: price with a 12-period and a 26-period EMA on top; below, the MACD line, its dashed signal line, the histogram bars and the zero line. An arrow shows that the MACD line is the gap between the two EMAs. MACD is the gap between EMAs MACD line = EMA(12) − EMA(26) Price EMA 12 EMA 26 MACD Signal Zero line Histogram
The parts of a MACD chart: the two EMAs on price, and below them the MACD line, signal line, histogram and zero line.

How is MACD calculated?

MACD takes three steps of averaging, all based on the closing price of each bar. An exponential moving average (EMA) is an average of recent closing prices that gives the newest prices the most weight and lets older prices fade out gradually. Think of how you judge the weather: yesterday counts more than last month, but last month is not forgotten. If you want the background on average types, see the moving averages indicator page.

  1. Compute the 12-period EMA of the close.
  2. Compute the 26-period EMA of the close.
  3. Subtract the slow EMA from the fast EMA. This is the MACD line.
  4. Compute a 9-period EMA of the MACD line itself. This is the signal line.
  5. Subtract the signal line from the MACD line and draw the result as bars above or below zero. This is the histogram.

The only formula you need is step 3:

MACD line = EMA(12) − EMA(26)

EMA(12) is the 12-period exponential moving average of the close, and EMA(26) is the 26-period one. A period is one bar. On a 5-minute chart, 12 periods means the last 12 five-minute bars. On a daily chart, it means 12 trading days.

The signal line is a smoothed copy of the MACD line that trails it slightly. The histogram is the distance between the MACD line and the signal line. When the MACD line is above the signal line, the bars are above zero.

If you need a version that compares across markets, the percentage price oscillator divides the gap between the two averages by the slower average. It shows the same shape but in percent.

How do you read the MACD lines and histogram?

There are three common readings: signal-line crossovers, the position relative to zero, and divergence from price. Each one has a plain meaning and an exact version you can test.

ReadingWhat it meansExact version for a test
Bullish crossoverShort-term momentum is turning up compared with its own recent averageMACD line above signal line at this bar's close, below it at the previous close
Bearish crossoverShort-term momentum is turning downMACD line below signal line at this close, above it at the previous close
MACD above zeroThe 12-period EMA is above the 26-period EMA, an upward biasMACD line greater than 0 at the bar close
Zero-line crossThe fast EMA has crossed the slow EMAMACD line changes sign from one close to the next
Shrinking histogramThe gap between MACD and signal line is narrowing; momentum is slowingHistogram smaller in size than on the previous bar, for a set number of bars
DivergencePrice and momentum disagreeNeeds a defined swing high or low, such as the highest high of the last N bars

The histogram is useful because it shrinks before the crossover happens. When the bars get smaller, the MACD line is bending toward the signal line. That gives an earlier hint of a turn, but it also gives more false hints.

A divergence is a situation where price makes a new high or low but MACD does not. In a bearish divergence, price makes a higher high while MACD makes a lower high. In a bullish divergence, price makes a lower low while MACD makes a higher low. Divergences are popular on charts but harder to test. You must decide what counts as a swing point, and that choice adds parameters and delay. A divergence can also last through several more highs before price turns.

Bearish divergence: price makes two highs with the second one higher, joined by a rising line, while the MACD line makes two matching peaks with the second one lower, joined by a falling line Price rises, MACD fades Bearish divergence, illustration Price MACD Higher high Lower high Price climbs, momentum fades. So: define swing points first.
Bearish divergence: price makes a higher high while the MACD line makes a lower high.

What do the MACD settings 12, 26, 9 change?

Shorter periods make MACD react sooner and signal more often. Longer periods make it smoother, slower and quieter. The default 12, 26, 9 is a convention, not a tested optimum for your market.

Setting styleExample valuesWhat changes
Faster6, 13, 5Earlier crossovers, more signals, more false signals, more trades that each pay costs
Default12, 26, 9The standard most charts show
Slower24, 52, 18Fewer signals, smoother lines, later entries and later exits

Bar size works like a period setting. With 12, 26, 9 on 5-minute bars, the slow average covers about two hours of trading (26 × 5 minutes). On daily bars, it covers about five weeks. Doubling the bar length and halving the periods gives a similar picture. Pick the bar size that matches how long you plan to hold a trade, then adjust the periods.

The signal period mostly controls how many crossovers you get. A shorter signal line hugs the MACD line and crosses it often. A longer one crosses less often and later.

Most charting platforms, including free web charts and broker charts, plot MACD with 12, 26, 9 as the default. Check two settings before you compare charts: that both averages are exponential (some platforms let you switch to simple averages) and that the source price is the close. Different choices give different values and different crossover bars.

The same price with two MACD settings: the fast 6, 13, 5 MACD crosses its signal line seven times, the slow 24, 52, 18 MACD crosses only twice and later Faster MACD, more crossings Same price, two settings Price 6, 13, 5 24, 52, 18 7 crosses 2 crosses More crossings mean more costs. So: test each setting with costs.
The same price with fast and slow MACD settings: faster settings cross more often, slower settings cross less often and later.

Why does MACD give late and false signals?

MACD is built from moving averages, so it is always late, and in sideways markets it crosses back and forth. These two weaknesses explain most poor MACD results.

Lag is the delay between a change in price and the indicator showing it. An EMA lags price by roughly half its period. The 12-period EMA trails by about 6 bars and the 26-period EMA by about 12 bars. The signal line adds its own delay on top. A signal-line crossover therefore comes several bars after the turn, and a zero-line cross comes later still. On a short, sharp move, much of the move can be over before the signal appears.

A whipsaw is a signal that reverses soon after it appears, often at a small loss. In a range, the 12-period and 26-period EMAs stay close together. The MACD line hovers near zero and crosses the signal line again and again. Each cross is a trade, and each trade pays commission and slippage.

Whipsaws: while price moves sideways in a range, MACD crosses its signal line six times, each marked with an x; when a steady uptrend starts, it crosses once, marked with a circle Ranges cause whipsaws Range vs trend, illustration Range Trend Price MACD 6 crosses 1 cross In a range, each cross pays costs. So: filter out quiet ranges.
Whipsaws: in the sideways range, MACD crosses six times; in the trend, it crosses once.

Traders reduce whipsaws in three common ways:

  • Trade with the zero line. Take bullish crossovers only when MACD is above zero, bearish ones only below zero.
  • Add a trend-strength filter. For example, take signals only when the ADX indicator is above a chosen level, which skips many quiet ranges.
  • Require a minimum gap. Ignore crossovers where the histogram stays tiny, measured relative to recent volatility rather than in fixed points.

Each filter removes some false signals and also some good ones. Each adds a parameter that you will be tempted to tune. That is why the next two steps matter.

How do you turn MACD into a rule you can backtest?

A MACD idea becomes testable once every decision is written down so exactly that two people would get the same trades. Use this order:

  1. Fix the market, bar size and session. On futures, day-only bars and full-session bars give different EMA values, so choose before you test.
  2. Define the signal at the bar close. For example: "MACD line above the signal line at this close and below it at the previous close."
  3. Execute on the next bar. A signal known at the close cannot be filled at that same close. Fill at the next bar's open.
  4. Define the exit. Opposite crossover, a stop, a time limit or the end of the session. Write it as precisely as the entry.
  5. Skip the warm-up. EMAs need history before their values settle. Start counting trades only after at least about 100 bars with 12, 26, 9.
  6. Add commission and slippage to every trade. Slippage is the difference between the price your test assumes and the price you would actually get.
  7. Keep data the rule has never seen. Hold back a later period for out-of-sample testing.

Hypothetical example

A long-only rule on ES (E-mini S&P 500) 15-minute bars buys on a bullish crossover and sells on the next bearish crossover. The backtest shows 400 trades and a gross result of $12,000, or $30 per trade.

Costs per round trip: $5 commission and fees, plus one tick of slippage on entry and one on exit. One ES tick is $12.50, so slippage is $25. Total cost: $30 per trade. The net result is $0 per trade. The gross backtest looked fine; after costs there is nothing left.

Now add the zero-line filter. Suppose it leaves 150 trades with a gross result of $9,000, or $60 per trade. After the same $30 cost, the net is $30 per trade. The point is the arithmetic: fewer, larger trades survive costs more easily than many small ones.

The guide on trading costs in backtests shows how to estimate commission and slippage for your own contract. In AlphaPrime (Valynox), Strategy generation turns research ideas into testable trading rules with entry and exit conditions, parameter ranges and search constraints. AlphaPrime's backtesting then evaluates the candidates in batches with fees and slippage, next-bar execution and trading sessions included.

How do you test many MACD settings without fooling yourself?

Testing many settings is fine; trusting only the best one is the mistake. The more combinations you try, the more likely the top result owes part of its shine to luck.

Hypothetical example

You test fast periods 6 to 15 (10 values), slow periods 20 to 40 in steps of 2 (11 values) and signal periods 5 to 12 (8 values). That is 10 × 11 × 8 = 880 combinations. The best of 880 will look better than its true quality, simply because it was selected as the best.

Sullivan, Timmermann and White (1999) showed that when many technical trading rules are tested on the same data, the best one can look significant by chance, and they used a bootstrap test to correct for this. Bailey and co-authors (2014) showed that with enough trials, a strong in-sample result can appear even when no rule has an edge. Use these checks:

  • Look at the neighbours. If 12, 26, 9 is good, then 11, 26, 9 and 13, 28, 9 should be reasonable too. A lone peak surrounded by poor settings is fitted noise.
  • Judge the out-of-sample period. Choose settings on the earlier data, then run them unchanged on the held-back data.
  • Repeat the split over time. Walk-forward analysis re-chooses the settings on each window and tests them on the next one, so every result comes from data the choice did not see.
  • Count the trades. A setting with 20 trades tells you little, however good it looks.

What this means if you use AlphaPrime: treat MACD as an idea to write as exact entry and exit conditions with realistic costs, then judge it by out-of-sample results and neighbouring settings rather than by the single best backtest.

Frequently asked questions

What are the best MACD settings?

No setting is best for every market and bar size. 12, 26, 9 is the common default and a sensible starting point. To choose your own, test a range of settings on your market and check that neighbouring settings and out-of-sample results also hold up.

Is MACD a leading or a lagging indicator?

MACD is a lagging indicator because it is built from moving averages of past prices. Its histogram turns before the signal-line crossover, so it reacts earlier than the lines. It still reacts only after price has already moved.

Can MACD be used for day trading futures?

Yes. MACD works on any bar size, and the periods count bars, so 12, 26, 9 on 5-minute bars reacts within hours. Intraday rules usually trade more often, so commission and slippage take a larger share of each trade. Decide whether to use day-session or full-session bars before testing.

Where can I see a free MACD chart?

Most charting platforms and broker charts include MACD at no cost, usually with 12, 26, 9 as the default. Check that both averages are exponential and that the indicator uses the closing price. Different settings produce different values and crossover bars.

What is the difference between the MACD line and the MACD histogram?

The MACD line is the 12-period EMA minus the 26-period EMA. The histogram is the MACD line minus its 9-period signal line. The line shows momentum; the histogram shows whether that momentum is growing or fading.

References

  • Sullivan, R., Timmermann, A., & White, H. (1999). Data-Snooping, Technical Trading Rule Performance, and the Bootstrap. The Journal of Finance, 54(5), 1647–1691.
  • Bailey, D. H., Borwein, J., López de Prado, M., & Zhu, Q. J. (2014). Pseudo-Mathematics and Financial Charlatanism: The Effects of Backtest Overfitting on Out-of-Sample Performance. Notices of the American Mathematical Society, 61(5), 458–471.

What AlphaPrime does here

Strategy generation turns research ideas into testable trading rules with entry and exit conditions and parameter ranges. AlphaPrime's backtesting evaluates them in batches with fees, slippage, next-bar execution and trading sessions.

See plans and pricing