Opens in a new tab
Skip to content
Find my broker

Understanding MACD: from moving averages to crossings

Moving Average Convergence Divergence (MACD) compares a fast exponential moving average of closing prices with a slow exponential moving average. Its signal line smooths that difference; its histogram shows the gap between the MACD line and the signal line.

What the MACD measures

The examples use a fast EMA of 12 bars, a slow EMA of 26 bars and a signal EMA of 9 bars, applied to close prices. These are the classic defaults, not optimized recommendations.

  • The MACD line is the fast EMA minus the slow EMA.
  • The signal line is an EMA of the MACD line.
  • The histogram is the MACD line minus the signal line.

The calculation used by this download

MACD[i] = EMA(close, FastEma)[i] - EMA(close, SlowEma)[i]
Signal[i] = EMA(MACD, SignalEma)[i]
Histogram[i] = MACD[i] - Signal[i]
alpha = 2 / (N + 1)
EMA[i] = EMA[i-1] + alpha * (x[i] - EMA[i-1])

Seeds and warm-up

Each EMA starts with the simple mean of its first N input values. The fast and slow price EMAs start at their respective seed bars; the signal EMA starts from the first SignalEma available MACD values. Indexing runs from the oldest bar. With sufficient history, the first MACD index is SlowEma − 1 and the first signal and histogram index is SlowEma + SignalEma − 2. Earlier values are empty. With insufficient history, all buffers remain empty and the next call recalculates from scratch.

The seed mean is computed as the first value plus the mean of the differences to it. This is the same simple mean and keeps a run of identical values exact. An EMA with N = 1 is the input itself.

Check the signal-line convention. The MACD supplied with MetaTrader uses a simple moving average for its signal line and draws the MACD line itself as a histogram. This download uses an EMA signal line and a histogram of MACD minus signal. Those displays and values are not interchangeable. The built-in difference indicator is OsMA.

How crossings are defined

A bullish signal-line crossing occurs when the MACD line moves from below its signal line to above it. A bearish crossing is the reverse. Equivalently, the histogram changes sign. A zero-line crossing instead occurs when the MACD line itself changes sign.

Above and below are strict comparisons. An exact zero keeps the previous side: a touch without a sign change is not a crossing. A crossing through an exact zero is reported on the first bar strictly on the new side. The first bar with a side has nothing to cross from.

A crossing describes past bars; it does not forecast prices.

Crossings are reported at the closing bar where the sign change becomes visible, never back-dated. The newest bar may still be forming and does not confirm a crossing. Its MACD, signal and histogram values recalculate on each tick. A closed-bar event is not removed or moved while the closed history is unchanged; a history reload recalculates the indicator from the new data.

Reading the chart examples

These charts use EURUSD hourly bars, with prices and MACD values in USD per EUR. The captured export contains 5,000 closed bars from 2025-12-04 15:00 to 2026-09-24 22:00. This is a historical sample, not a live quote.

Chart times are terminal bar-open times. The measured terminal clock offset at capture was UTC+3; historical daylight-saving changes are not modelled. Weekend and session gaps are compressed, with a faint vertical line at gaps.

An overview of the recent sample

EURUSD hourly price above MACD and signal lines, with a signed histogram and arrows marking confirmed crossings.
Figure 1. The newest 240 hourly bars contain 9 bullish and 8 bearish signal-line crossings. The arrows mark the bars where the crossings became visible after closing; the lower panel separates the MACD line, signal line and histogram. Open full-size chart.

A bullish crossing below zero

EURUSD hourly chart showing a bullish MACD signal-line crossing below the baseline, followed by a MACD baseline crossing.
Figure 2. The bullish signal-line crossing is on the 2026-09-21 11:00 bar, visible at its close (12:00). MACD is -0.000040, while the signal line is -0.000050. The MACD line crosses zero upward 2 bars later. The inset enlarges the separation of the lines. Open full-size chart.

A bearish crossing above zero

EURUSD hourly chart showing a bearish MACD signal-line crossing above the baseline, followed by a MACD baseline crossing.
Figure 3. The bearish signal-line crossing is on the 2026-09-21 17:00 bar, visible at its close (18:00). MACD is 0.000006, while the signal line is 0.000028. The MACD line crosses zero downward 1 bars later. The inset enlarges the separation of the lines. Open full-size chart.

Each crossover example is the most recent qualifying crossing with 40 bars of context on either side and a subsequent zero-line crossing within 30 bars. Price outcome is not used to select it. The windows overlap by 75 bars; they show related events in the same part of the sample.

A dense run of crossings

EURUSD hourly candles in a narrow price range above closely spaced MACD crossing arrows and alternating histogram signs.
Figure 4. The selected 60-bar window contains 12 signal-line crossings. Its high-low range is 0.00428; 98.0% of the 4,907 scored windows have a wider range. The window with the most signal-line crossings is selected, with ties going to the most recent. This describes the sample, not trading performance. Open full-size chart.

Crossings after swing extremes

EURUSD price swing from a marked low to a marked high, with later MACD crossing arrows and horizontal lag annotations.
Figure 5. The selected swing low is 1.17294; the first bullish signal-line crossing follows 6 bars later. The swing high is 1.20816; the first bearish crossing follows 7 bars later. Selection uses the widest high-low range among the scored windows, with ties going to the most recent. The chart extends beyond that window to show the later crossing. Open full-size chart.

Download the indicator

The indicator runs in a separate window, displaying the MACD line, signal line, histogram and signal-line crossing arrows. It does not place trades, send alerts or use DLL imports.

MQL5 source

Native source with editable fast, slow and signal EMA periods.

Download .mq5 source

Compiled MT5 indicator

The recorded compilation result is 0 errors, 0 warnings. Native chart attachment and drawing have not been tested.

Download .ex5 indicator

MQL4 source

Static review only: not compiled or run in MT4.

Download .mq4 source

Configuration

  1. Select the source or compiled file for the intended platform.
  2. Use the platform’s indicator folder and indicator controls to load a custom indicator.
  3. Set FastEma, SlowEma and SignalEma. FastEma and SignalEma must be positive; SlowEma must exceed FastEma.
  4. Check the signal-line convention, available history and closed-bar behavior before interpreting the display.

Limitations

This tool does not detect MACD divergence. It marks crossings and does not forecast prices or trade. Sample crossing counts are not hit rates and were not tested for trading performance. Other implementations can use different signal-line averages, seeds or first bars, changing their early values or signal line.

The sample counts are not hit rates and were not tested for trading performance.

Risk warning: Trading involves risk and you can lose money. MACD should not be used in isolation for trading decisions.

Frequently asked questions

What does the MACD line measure?

It is the fast EMA of closing prices minus the slow EMA of closing prices.

What does the histogram show?

It is the MACD line minus the signal line. It is positive when MACD is above the signal line and negative when MACD is below it.

Which settings do the examples use?

The fast, slow and signal EMA periods are 12, 26 and 9 bars, respectively, applied to close prices.

Is a signal-line crossing the same as a zero-line crossing?

No. A signal-line crossing changes the sign of MACD minus signal. A zero-line crossing changes the sign of the MACD line itself.

Can the newest bar confirm a crossing?

No. The newest bar may still be forming. Its values change on each tick; this download reports crossing events only on closed bars.

Why can the built-in MetaTrader MACD look different?

The built-in indicator uses a simple-average signal line and draws the MACD line as a histogram. This download uses an EMA signal line and draws MACD minus signal as its histogram.

Does the download detect divergence?

No. This tool does not detect MACD divergence.

Further reading

Keep reading

Vdubus Divergence Wave Pattern Generator Tutorial and Trading Guide

Knoxville Divergence Indicator Guide & Strategy

How to Predict Price Movements with Linear Regression Channel?