MACD divergence: what it means and how to define it for a backtest
MACD divergence is a disagreement between price and MACD: a new price high or low that the indicator does not confirm. It is a hint that momentum is fading, not a reversal signal. To backtest it you must write the swing width, the maximum swing distance, the MACD line compared and the entry confirmation as exact bar rules, then judge the rule on out-of-sample trades.
By Soren Huang · Published · updated · Editorial policy
Contents
- What does MACD divergence mean?
- How do you define a swing high or low objectively?
- Which lookback and confirmation choices turn divergence into a testable rule?
- Is MACD divergence reliable, and why can't you check it by eye?
- What does a backtest of a MACD-family rule on ES daily bars show?
- What are the limits of MACD divergence, and how do you test it out-of-sample?
- Frequently asked questions
- References
What does MACD divergence mean?
MACD divergence is a disagreement between price and the MACD indicator: price makes a new high or low and MACD does not. Bearish (top) divergence is a higher price high with a lower MACD high. Bullish (bottom) divergence is a lower price low with a higher MACD low. Both are hints that momentum is fading, not reversal signals on their own.
The MACD indicator measures the gap between a fast and a slow moving average of price, so it tracks how strongly price is moving, not where price is. Think of a car going uphill: the speedometer still climbs, but the engine note gets weaker. Price is the speedometer, MACD is the engine. When price reaches a new peak and MACD reaches a lower peak, the push behind the move is smaller than it was at the previous peak. Chart readers call this a top divergence (頂背離). The mirror case at a low is a bottom divergence (底背離).
| Bearish (top) divergence | Bullish (bottom) divergence | |
|---|---|---|
| Price | Higher high than the previous swing high | Lower low than the previous swing low |
| MACD at those swings | Lower high than at the previous price high | Higher low than at the previous price low |
| What it hints | Upward momentum is fading | Downward momentum is fading |
| What it does not say | When, or whether, price will turn down | When, or whether, price will turn up |
The same idea applies to any oscillator, which is why RSI divergence (RSI 背離) is read the same way. The problem is the same too: "a new high in price and a lower high in MACD" sounds exact, but it leaves open what counts as a high, how far apart the two highs may be, and what you do once you see it. The guide to which technical analysis tools and indicators can be tested draws this line between a pattern you can describe and a rule you can run. The rest of this article turns divergence into the second kind.
How do you define a swing high or low objectively?
A swing high must be written as a bar rule, and the usual form is: the highest high within N bars on each side. A swing high is a bar whose high is higher than the highs of the N bars before it and the N bars after it. With N equal to 5, the bar must beat five neighbours on the left and five on the right. The same rule with lows gives a swing low.
The part most chart readers skip is the right-hand side. You cannot know a bar is a swing high until N more bars have closed and none of them went higher. So with N equal to 5, the swing is confirmed five bars after it formed. On daily bars that is one trading week of delay. On 15-minute bars it is a little over an hour. This delay is part of the rule, not a detail to hide, because the trade can only be placed after the confirmation bar, never at the swing itself. It is like knowing you stood on a hilltop: you are sure only after you have walked down the other side for a while.
Once swings are defined, the MACD value at each swing is simply the indicator reading on that bar. Divergence then becomes a comparison of two pairs of numbers: the two price highs and the two MACD readings on the same two bars. A backtest that reads the swing on the bar it formed, instead of N bars later, is looking into the future and will show an entry price nobody could have had.
Which lookback and confirmation choices turn divergence into a testable rule?
Four choices decide what a divergence rule actually trades, and each one changes the trade count and the entry timing enough that all four must be fixed before the first test. Here lookback means the maximum number of bars allowed between the two swings being compared. Confirmation means the extra condition that must be true before the rule enters.
| Choice | Typical range | Smaller value | Larger value |
|---|---|---|---|
| Swing width N | 3 to 10 bars each side | More swings, more divergences, more noise, shorter delay | Fewer, larger swings, fewer signals, longer delay |
| Maximum distance between the two swings (lookback) | 20 to 100 bars | Only nearby swings compared; some divergences missed | Swings from different market phases get compared |
| Which MACD line is compared | MACD line, signal line or histogram | The histogram turns first and gives the most divergences; the MACD line turns last and gives the fewest | |
| What confirms entry | Swing confirmation, signal-line cross, or close beyond the last swing | Earlier entry, more false starts | Later entry, fewer trades, less of the move left |
The confirmation choice deserves the most thought because it sets where the trade begins. Entering at swing confirmation means entering N bars after the low. Waiting for the MACD line to cross its signal line usually adds a few more bars. Waiting for a close above the most recent swing high is the same logic as the Donchian channel breakout rules, and it adds the most delay but filters the most failed divergences. None of these is right in general. They are three different rules.
Hypothetical example
Suppose a rule on daily bars with N equal to 3 and a lookback of 60 bars finds 90 bullish divergences in ten years. The same data with N equal to 8 finds 25, because many small dips no longer count as swings. Add a close-above-swing-high confirmation to the second version and 15 of the 25 never trigger, leaving 10 trades. Three settings, three strategies, from one idea.
Write all four values down before you run anything, including the exact confirmation. In AlphaPrime (Valynox), parameter ranges and entry and exit conditions are fixed in strategy generation before a search begins, so the same discipline is forced on every variant the search tries. The same discipline applies well beyond indicators: testing COT positioning as a trading rule starts with the same step of replacing a chart impression with a written condition.
Is MACD divergence reliable, and why can't you check it by eye?
Looking at a chart overstates divergence, because the eye finds the divergences that came before a turn and skips the ones that did not. Only a bar-by-bar rule counts every case, including the failures, and only the full count tells you whether the condition is worth trading.
Three things go wrong when you check by eye. First, you scan for turns and then look back for a divergence, so every turn with a divergence is remembered and every divergence without a turn is forgotten. It is like remembering only the weather forecasts that came true. Second, you see swings that were not yet confirmed at the time; on a finished chart the swing high is obvious, but on the live bar it was not. Third, you adjust the swing size by feel so that the pattern fits. A rule cannot do any of these. It uses one N, one lookback, one MACD line and one confirmation, and it writes down every match.
Hypothetical example
A rule with fixed settings finds 10 bearish divergences on a futures contract over six months. Three are followed by a fall large enough to pay for the trade. Seven are followed by a sideways stretch or by a further rise, and a short entered at confirmation loses. A trader scrolling the same chart afterwards notices the three and calls the pattern reliable. The rule reports 3 of 10 and asks whether the three winners were large enough to cover the seven losers after costs.
This is not only a trading problem. Lo, Mamaysky and Wang (2000) replaced hand-drawn chart patterns with pattern-detection algorithms for exactly this reason: a pattern that is defined by an algorithm can be counted, and a pattern defined by eye cannot. Park and Irwin (2007) reviewed decades of technical-rule studies and found that results depend heavily on how rules were specified and on whether the tests accounted for the many variants tried. Whether divergence helps on your market and bar size is a question for your own count, not for a chart you have already seen.
What does a backtest of a MACD-family rule on ES daily bars show?
A stand-in rule from the MACD family, run on ES daily bars from 2019-01-02 to 2025-06-30, gives the numbers in the box below, and those numbers are flattered by how the candidate was chosen. The run did not test divergence itself. It tested an OSMA rule, which trades on the MACD histogram (the gap between the MACD line and its signal line), as the only entry and exit, with no stop, no target and no filters. It is the nearest standard building block to a divergence rule, not the subject of this article.
Source: AlphaPrime product engine (CPU), run 2026-10-31. ES daily bars, 2019-01-02 to 2025-06-30, long and short, 1 contract, $20 commission and $25 slippage per trade.
| Number | Result |
|---|---|
| Net profit (closed trades) | $122,767.5 |
| Max drawdown | $39,842.5 |
| Trades | 131 |
| Win rate | 50.38% |
| Profit factor | 1.612 |
| Average trade | $937.16 |
| Time in market | 73.52% |
Read the box with its selection in mind. This candidate is the one AlphaPrime kept out of 1,024 variants the search tried, chosen for its return against its drawdown. Picking the best of many after the fact flatters the result: the net profit of $122,767.5 and the profit factor of 1.612 describe the winner of a contest, not what the OSMA rule earns, and not what a reader can expect. Bailey, Borwein, López de Prado and Zhu (2014) show how quickly the best of many backtests separates from what the same rule does on new data as the number of tries grows. Every evaluation in the run included the $20 commission and $25 slippage per trade, so the average trade of $937.16 is after those costs, but after-cost is not the same as after-selection.

Two numbers in the box matter most for a divergence trader. Time in market of 73.52% means this rule holds a position almost three quarters of the time, which is the opposite of a divergence rule that waits for a rare condition. And 131 trades over about six and a half years is roughly 20 trades a year, so a handful of trades can move the average; a divergence rule with stricter confirmation would have even fewer. Both points lead to the testing method in the next section.
What are the limits of MACD divergence, and how do you test it out-of-sample?
Divergence rules trade rarely and their results depend on a few swings, so judge them on out-of-sample trades, check that neighbouring values of N behave alike, and keep one final block of data for a single test. Out-of-sample data is the part of the history that was not used to choose the settings, so a result on it is not inflated by the choosing. The split, and how many trades it needs, are covered in how to split in-sample and out-of-sample data.
The limits come straight from the definition. Divergence compares two swings, so a trending market that makes five higher highs in a row can show divergence at the second high and keep rising for three more. A rule with a long lookback compares swings from different market phases. A rule with a small N trades every wobble. And because every setting changes the trade count, a divergence rule with few trades is easy to fit to the past by accident: with 20 trades, replacing two of them changes the result a lot. Harvey and Liu (2015) make the same point about any test built on few observations and many tries.
- Write the rule before you look. N, the maximum distance between swings, the MACD line compared, and the confirmation, in one sentence each.
- Run it on the in-sample part. Record the trade count and the average trade after commission and slippage.
- Run it unchanged on the out-of-sample part and count completed trades. Fewer than about 30 trades is a hint, not a result; if the count is low, a longer history or a walk-forward design is needed before the rule means anything.
- Rerun neighbouring values of N and double the slippage. If N equal to 5 works and 4 and 6 do not, the setting fits one stretch of history; parameter stability, plateau or lucky spike shows what a healthy neighbourhood looks like.
- Test once on the final untouched block. Read it against the rule you wrote in step 1 and accept or reject.
One more limit is worth naming: divergence is a condition on one indicator and one price series, and it says nothing about who is positioned where. Some traders pair it with a positioning input, and reading the COT report for gold futures as a tested rule shows how such an input is itself turned into a counted condition rather than another chart impression. Whatever you add, it goes through the same five steps.
A test you can run this week: take one market and one bar size you already use, write down N, the lookback, the MACD line and the confirmation, and count how many divergences the rule finds over the last two years. Then count how many were followed by a move large enough to cover one round trip of your commission and slippage. That one ratio, before any optimization, tells you whether the idea deserves the five steps above.
Frequently asked questions
What does MACD divergence mean in simple terms?
MACD divergence means price and the MACD indicator disagree. Price makes a higher high while MACD makes a lower high (bearish or top divergence), or price makes a lower low while MACD makes a higher low (bullish or bottom divergence). It shows that the latest move had less momentum behind it than the one before. It does not by itself say that price will reverse.
Is MACD divergence reliable?
Only a count can answer that for a given market and bar size, and the count depends on how the rule is written: the swing width, the maximum distance between swings, which MACD line is compared and what confirms the entry. Checking by eye on a finished chart overstates reliability, because the eye remembers the divergences that preceded turns and skips the ones that did not. Write the rule, count every match out-of-sample, and compare the winners with the losers after costs.
Does price always reverse after a divergence?
No. A strong trend can show divergence at one high and then make several more higher highs, because momentum can fade for a while and then return. Divergence describes the last two swings only. That is why a tested divergence rule needs a confirmation condition, such as a signal-line cross or a close beyond the last swing, and an exit rule, instead of acting on the divergence alone.
Is RSI divergence the same as MACD divergence?
The idea is the same: price makes a new high or low and the oscillator does not. Only the indicator differs, and the RSI indicator is bounded between 0 and 100 while MACD is not, so the two produce divergences at different times. Both need the same written definitions of swing width, lookback and confirmation before they can be backtested, and both suffer from the same eye-check problem.
References
- Lo, A. W., Mamaysky, H., & Wang, J. (2000). Foundations of Technical Analysis: Computational Algorithms, Statistical Inference, and Empirical Implementation. The Journal of Finance, 55(4), 1705–1765.
- Park, C.-H., & Irwin, S. H. (2007). What Do We Know About the Profitability of Technical Analysis? Journal of Economic Surveys, 21(4), 786–826.
- 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.
- Harvey, C. R., & Liu, Y. (2015). Backtesting. The Journal of Portfolio Management, 42(1), 13–28.

