This blog post is for short-term players looking for counter-trend opportunities. This blog post explains why we observe divergences between the MACD indicator and stock prices. I also describe the scan code for automatically detecting divergences using the StockCharts scan engine on daily and weekly data. Short-term and swing traders can now automate the search for MACD divergences via this post. Longer-term players will gain insight into how the MACD works.

**Introduction**

Is there something fundamentally significant about a divergence between price and MACD, when prices make new highs but MACD declines (or vice versa)? The MACD indicator is simply the difference between two exponential moving averages, with the default values of 12 and 26 periods. We visually observe divergences because the slower moving exponential moving average is "catching up" to the faster moving average as prices begin to rise (or fall) more slowly, i.e., the high-low range in prices becomes smaller when measured over a fixed time interval. So a divergence is merely an artifact of how we calculate and plot these indicators. The divergence disappears if we change the lengths of the moving averages used to calculate the MACD indicator, or change the underlying time period, from say daily to weekly data. Such a divergence may precede price declines (or reversals), but not every divergence leads to price declines (or reversals). Confused? Well, read on.

**What is the MACD indicator?**

The MACD (moving average convergence divergence) oscillator is simply the difference between two exponential moving averages (EMAs). The default values are 12 and 26 periods. Since there are 52-weeks in a year, half a year is 26-weeks and one quarter is half of 26, rounded down to 12 weeks. So, this may explain the choice of 12 and 26 periods. In addition, a 9-period exponential average of the difference is plotted as a signal line.

Greg Morris goes into a detailed analysis of MACD and suggests that the second average should be 6x (6 times) the length of the first average and the signal line should be 3x (3 times) the length of the shortest EMA. He recommends 7 days as the value for the first average on daily data.

**Simulation with Sinusoidal Prices**

Let us use a sine curve to simulate sinusoidal prices (see Chart 1). I used a sine curve to guarantee that the simulated prices will top and turn lower. The blue line in Chart 1 is using a sine function to create artificial price series starting at $100. I then calculated the 12- and 26- period EMAs, and plotted their difference below the sinusoidal price curve. This difference is the MACD indicator without the signal line. Note the divergence: the difference in EMAs peaks and starts declining before prices actually top and turn lower.

*Chart 1: I simulated closing prices using a sine curve (blue line) to guarantee that prices will top and turn lower. The purple bars are the MACD indicator with default values, or the difference between 12 and 26 period EMAs without the signal line. Observe that the MACD has turned down before prices begin to rollover. We have a divergence because prices are rising even as the MACD is falling. So the MACD is predicting price declines before the sine curve tops and turns down.*

I illustrate this analysis in greater detail in Chart 2. When prices start to accelerate higher, the shorter EMA (red line) rises faster than the slower EMA (green line) and the gap or difference between the two EMAs (or MACD) increases. As the price begins to rise more slowly near the top, the red line slows down, but the green line begins to catch up to the red line. In other words, as prices rise more slowly, the slower EMA catches up to the faster EMA and the difference (or MACD) declines. So for a sine curve, the MACD turns down before price tops. Hence, we can plot a divergence, as shown in Chart 3.

*Chart 2: The same data as in Chart 1, showing both the 12- and 26- period EMAs. Note how the shorter EMA (red line) accelerates away from the slower EMA (green line) and the MACD rises towards the start of the sine wave. Near the top of the sine wave, as the prices rise more slowly, the shorter EMA (red line) starts rising more slowly, and the slower EMA (green line) starts to catch up with the red line, thus reducing the distance between them. In other words, the MACD turns down because the gap between the two EMAs shrinks.*

*Chart 3: The divergence occurs because the prices are rising more slowly, and the slower EMA catches up to the faster EMA. *

**Divergence in the QQQ**

In Chart 4 we look at a real world divergence in the QQQ ETF. At the most recent high in late November, the MACD (upper panel) had made a high and turned down before prices made a new high. Thus, we had a divergence between prices and the MACD. In the seven days leading up to the price high, the QQQ ETF rose 3.36 points. In the seven days leading up to the high in the MACD, the QQQ ETF rose 3.55 points. Thus, the new high was made with a narrower price range leading up to the new high: the faster EMA was slowing down, allowing the slower EMA to catch up, leading up to the divergence, just as we showed in the simulated sinusoidal prices above. Another way to look at the divergence is to observe that the faster EMA is pinching the slower EMA as prices rise more slowly (see Chart 5).

*Chart 4: We see a divergence on the QQQ daily, because the 7-day price range to the Nov 28 high was 3.36 points, but the 7-day price range to the Nov 08 high (which is the MACD high) was 3.55 points.*

*Chart 5: The divergence occurs because at the MACD high the difference was 1.98, but as prices continue to rise at a slower pace, the slower EMA (red line) catches up to the faster EMA (blue line), and the difference drops to 1.80. *

**Divergence Disappears if Time Period Changes or MACD Input Values Are Changed**

The divergence visible in Charts 4 and 5 disappears, if for example, we change the MACD input values to 12, 72, and 36 following the Greg Morris formula (see Chart 6). Thus, the observed "divergence" is merely an accident of the chosen variable values, and not something intrinsic to market action (or QQQ price behavior).

*Chart 6: The divergence observed using MACD values of 12,26,9 disappears on the QQQ daily chart when MACD values are changed to 12, 72, and 36 as shown above. Thus, the observed MACD divergence is an accident of the chosen variable values and not something intrinsic to the data.*

In Chart 7 we show a weekly chart of the QQQ ETF, which now shows a divergence between the MACD and prices at a different point in time. However, prices continued to move higher after this divergence. Observe also there is no divergence visible in early November on the weekly chart that is visible on the daily data. Thus, the divergence in Charts 4 and 5 disappears if we change the time period from daily to weekly data.

*Chart 7: Note that the divergence visible in November in Chart 4 and 5 disappears when the time periodicity changes to weekly data. But a new divergence is visible, and this time prices continued higher after the divergence.*

*Chart 8: The divergence visible in Chart 7 on weekly data disappears when MACD variable values are changed to 12, 72, and 36 following Greg Morris. Thus, divergences are an accidental result of choosing a particular set of values for calculating the MACD indicator.*

**Scan Code for Automatic Detection of MACD Divergences**

I wrote two versions of the scan code, for daily and weekly data.

======================================================

// Tushar Chande's Code for Detecting MACD Divergence on Daily Data: Dec/2017

// Uses Daily Data[type is stock]

// Narrow the universe... can use Exchange is NYSE etc.and [Group is SP500]

// high was greater than high 26 weeks agoand [Daily High > 26 days ago high]

// Daily MACD value is lower than value 10 days agoand [Daily MACD Line(12,26,9) < 10 days ago Daily MACD Line (12, 26,9)]

// Have uptrend: 13 day EMA > 26 day EMAand [Daily EMA(13,close) > Daily EMA(26,close)]

// and still near recent price highsand [Daily high > 2 days ago max(250,high)]

=======================================================

// Tushar Chande's Code For Detecting MACD Divergence on Weekly Data: Dec/2017

// Uses Weekly Data[type is stock]

// Narrow the universe... can use Exchange is NYSE etc.and [Group is SP500]

// high was greater than high 26 weeks agoand [weekly High > 26 weeks ago high]

// Weekly MACD value is lower than value 10 weeks agoand [weekly MACD Line(12,26,9) < 10 weeks ago weekly MACD Line (12, 26,9)]

// have uptrend: 13 week EMA > 26 week EMAand [weekly EMA(13,close) > weekly EMA(26,close)]

// and still near recent price highsand [weekly high > 2 weeks ago max(250,high)]

**Sample Scan Results**

Here are results for the daily scan with a chart below for ACN, the first item on the list. In chart are the results from daily data, and in Chart 10 I show that a divergence is clearly visible: thus, the scan seems to be working correctly. Chart 11 shows the results using weekly data. The first stock in that table is APTV and it's weekly chart, shown in Chart 12, shows a MACD divergence as we should expect. Thus, the code seems to be working as expected on both, daily and weekly data.

*Chart 9: Sample scan results for MACD divergence on daily data*

*Chart 10: Divergence on the Accenture (ACN) daily chart via the scan. ACN was at the top of the list in Chart 9.*

*Chart 11: The first few lines of the results for a scan for MACD divergence using weekly data.*

*Chart 12: Divergence on the Aptive (APTV) weekly chart suggests that the scan code is working as expected. (APTV is the first stock in the table in Chart 11.)*

**Summary**

The divergence on the MACD charts occurs when prices rise more slowly (or decline more slowly), so that the high-low range over a fixed time period (say 12 periods) is smaller. This allows the slower EMA to catch up to the faster EMA, giving us a divergence. Of course, the prices can continue to move in the prior direction, so that the MACD divergence does not guarantee a price reversal. The observed divergence is a random occurrence due to the input values selected for the MACD calculations. The divergence disappears if the MACD input values are changed or the underlying periodicity of the data is changed (say from daily to weekly).

Thus, the observed MACD divergence is simply an accidental effect of the selected input values and lacks any fundamental significance.

I hope you have enjoyed my insight into a very popular indicator, and will subscribe to myblog using the very easy link available below.

**Additional Reading**

Greg Morris: "Filtering the Noise II" (in depth discussion of MACD)

Tushar Chande: "Back to Basics: Why do RSI Divergences Occur?" (a close cousin of this discussion)