# A Deep Dive Into The MACD

Education

COINBASE:ETHUSD   Ethereum / U.S. Dollar
1. Introduction

The Moving Average Convergence Divergence ( MACD ) indicator created by Gerald Appel in 1979 (1) is part of the pantheon of technical indicators, being one of the most used and influential ever created. The popularity of the MACD allowed further studies and more varied applications of the indicator, from signal processing in neuroscience (2), prediction of hospitalizations (3)...etc.

In this post, we will highlight extensive details, calculations, and usages of this legendary indicator. If you wanted to go beyond what you learned about the MACD , then this post is for you.

Note that some contents of this post can be complex and might not suit certain readers, feel free to skip the sections of your choice.

2. Details

This oscillator returns 3 time-series, the MACD , obtained from the difference between two exponential moving averages of different periods, a signal line, obtained from the exponential moving average of the MACD , and a histogram obtained from the difference between the MACD and the signal line.

Each MACD component allows evaluating the current market trend direction, momentum, and acceleration. Many traders believe the amount of information the MACD can return is sufficient to be used as a standalone for both trend-following and contrarian trading.

In terms of digital signal processing, the MACD can be classified as an infinite impulse response (IIR) bandpass filter, filtering out both lower and higher frequency components of a signal, thus having the ability to both detrend and smooth. The MACD filter satisfies the conditions for being a discreet time linear time-invariant (DLTI) system, it is linear and time-invariant:

`macd_(a + b) = macd_(a) + macd_(b) -> Additivity`
`K × macd_(x) = macd_(K × x)        -> Homogeneity`
`macd_(x[t+k]) = macd_(x)[t+k]      -> Time Invariance`

3. Calculation

The MACD oscillator is obtained from the difference of two exponential moving averages (ExpMA), one using a faster period (often 12) and one using a slower period (often 26).

`MACD_ = ExpMA(price,fast) - ExpMA(price,slow)`

We can also obtain the MACD from the following difference equation:

`y[t] = (price[t] - price[t-1]) × g + ((1 - a1) + (1 - a2)) × y[t-1] - (1 - a1) × (1 - a2) × y[t-2]`

where a1 is the smoothing constant of the fast ExpMA, a2 the smoothing constant of the slow ExpMA, and g is the gain constant obtained from the difference between the smoothing constant of the two ExpMA's:

```g = a1 - a2
= 2/(fast+1) - 2/(slow+1)```

4. Impulse Response

The impulse response of the MACD is the result obtained by applying the MACD to a unit impulse signal, given by the Kronecker delta function d.

`d[t] = 1 if t = 0, else 0`

The impulse response fully describes the properties of the MACD and can be obtained from the difference between the impulse response of two ema's with periods fast and slow.

The impulse response of an exponential moving average h(ExpMA) over time t with smoothing constant a is given by:

`h(ExpMA)[t] = a × (1 - a)^t`

As such for the impulse response of the MACD h( MACD ) over time t we obtain:

`h(MACD_)[t] = a1 × (1 - a1)^t - a2 × (1 - a2)^t`

Like with an exponential moving average , the impulse response of the MACD does not become steady, instead continuing indefinitely, hence why it is classified as an infinite impulse response filter.

5. Frequency Response

The frequency response of filters allows us to determine how they affect the frequency content of a signal. The frequency response can be directly obtained from the discrete-time Fourier transform (DTFT) of the impulse response, which for the MACD returns:

```H(e^iw) = SUM h[n] × e^-iwn, for n = 0 to ∞
= SUM (a1 × (1 - a1)^n - a2 × (1 - a2)^n) × e^-iwn```

with w = 2 × pi × f. The infinite sum makes its direct computation infeasible.

It is generally more common to evaluate the filter transfer function H(e^iw) obtained from the Z transform given by:

```A([symbol=iw]iw[/symbol]) = b[0] + b[1] × z^-iw + ... + b[p] × z^-iwP
-------------------------------------------------
B([symbol=iw]iw[/symbol]) = a[0] + a[1] × z^-iw + ... + a[q] × z^-iwQ```

With feed-forward coefficient b and feedback coefficients a. This transfer function assumes a filter of the form:

`y[t] = SUM b[p] × x[t-p] - SUM a[q] × y[t-q], for p = 0 to P & for q = 1 to Q`

This is the reverse ordering used by the MACD difference equation previously described, as such the MACD transfer function is given by:

```                     g + -g × z^-iw
----------------------------------------------------------
1 + [(a1-1) + (a2-1)] × z^-iw + [(a1-1) × (a2-1)] × z^-iw2```

The frequency response is then obtained by evaluating the above transfer function for z = e.

5.1 Magnitude Response

The magnitude response describes how a filter attenuates the amplitude of the frequencies composing a signal. It is obtained from the absolute value of the transfer function |H(e^iw)|, that is:

`|H(e^iw)| = sqrt(Real[H(e^iw)]^2 + Imag[H(e^iw)]^2)`

For the MACD we obtain the closed-form solution:

```                              sqrt(g^2 × sin(2 × pi × f)^2 + (g - g × cos(2 × pi × f))^2)
|H(f)| = ----------------------------------------------------------------------------------------------------------
sqrt([-A1 × sin(2 × pi × f) - A2 × sin(4 × pi × f)]^2 + [A1 × cos(2 × pi × f) + A2 × cos(4 × pi × f)+1]^2)```

with A1 = (a1 - 1) + (a2 - 1) and A2 = (a1 - 1) × (a2 - 1).

In the previous figure we can see the magnitude response of the MACD using fast = 12 and slow = 26. This magnitude response is asymmetric, we can see attenuation of lower frequency components, and a poor attenuation of high-frequency components.

The above figure shows various MACD magnitude responses for various configurations of the fast and slow settings. We can see on the left that a fast period closer to the slow period return magnitude responses with fatter tails as well as a decreasing resonant frequency (frequency where the filter returns the least attenuation), on the right, we can see how increasing the slow period returns a lower attenuation of the peak frequency.

6. Usage

The MACD has known a wide variety of usages amongst traders, extending from trend-following to contrarian methodologies.

The most basic usage of the MACD is given by evaluating the sign of the MACD , with a positive sign (fast ExpMa > slow ExpMA) indicating an uptrend and a negative sign (fast ExpMa < slow ExpMA) indicating a downtrend. We can see that this usage does not differ from the one given by a simple MA cross strategy. The user might also suffer from the excessive lag produced by this simplistic approach.

The strength of the indicator can come from the usage of the MACD with the signal line and histogram. A timelier approach would identify an uptrend when the MACD is above its signal line (histogram above 0) and a downtrend when the MACD is under the signal line (histogram under 0). This approach makes better use of the leading characteristic of the MACD oscillator, thus offering more predictive insights. However, an increment in timing does not come at no cost, with the more recurrent of whipsaw trades.

Notice in the image above how the usage of the MACD with the signal line allows for a faster trend detection compared to using the MACD alone . We can also see how this usage of the indicator is more sensitive to shorter-term price variations, inducing potential whipsaw trades. This is caused by the common tendencies that oscillators have to increase the presence of noise in an input series.

It is also possible to use a combination of both usages in order to avoid their disadvantages, for example opening trades based on the sign of the MACD while exiting trades when the MACD crosses the signal line. However, the main disadvantage of using the histogram can appear when the user must optimize indicator settings, with a usage based only on the MACD meaning that two settings would need to be optimized, while usage based on the histogram would mean optimizing three settings, which is computationally more expensive.

6.1 Divergences

Divergences are commonly used with oscillators. A divergence occurs when the price tops/bottoms and MACD tops/bottoms are negatively correlated. This can indicate a trend impulse of lower amplitude, which could highlight a potential reversal.

6.2 Fast > Slow Period MACD

The MACD already possesses some leading characteristics, allowing to anticipate turning points. However, the ability of the MACD to provide signals anticipating future trends mostly depends on the current market conditions, with certain price variations complicating the leading ability of the MACD . The predictive abilities of the MACD can be improved using a fast period higher than the slow period.

Assuming the user uses the histogram of the MACD , cyclical variations within a price trend will generally prove to be problematic if the signal length excessively delays the MACD . Inverting the fast and slow period can help signal early reversal, instead of suffering from the excessive delay introduced by the histogram.

The practice of inverting MACD fast and slow period was proposed by Ehlers (4), we can also see that optimizing MACD settings in mean reverting markets can tend to return fast periods higher than slow periods. We can see that such an approach is directed toward contrarian traders.

7. MACD Using Different Type of Moving Averages

The MACD uses exponential for the calculation of the fast, slow, and signal moving averages by default, however different types of moving averages can be used. The MACD would directly inherit the characteristics of the type of moving average used, thus improving characteristics such as reactivity and smoothness.

For example, certain users prefer using the simple moving average , returning slightly lower reactive MACD with a slightly higher degree of filtering.

Using low lag moving averages would return a very reactive MACD , with a histogram able to anticipate MACD turning points due to the ability of low lag moving averages to over/undershoot the input signal.

Notice in the above chart how the MACD based on the Hull moving average (bottom) is more reactive than a regular MACD (top) with equal settings. Also, notice how the signal line is able to exceed the MACD before the occurrence of its turning point.

However, it can be more interesting to use more than one kind of moving averages for the MACD calculation, using a type of moving average that is suitable for each MACD component. As such it would be more interesting to have a low lag moving average as fast-moving average, and a more classical one as slow and signal moving average.

References

(1) Appel, Gerald. "Technical Analysis Power Tools for Active Investors." Financial Times Prentice Hall. p. 166 (2005)

(2) Durantin, Gautier, et al. "Moving Average Convergence Divergence filter preprocessing for real-time event-related peak activity onset detection: Application to fNIRS signals." 2014 36th Annual International Conference of the IEEE Engineering in Medicine and Biology Society. IEEE, 2014.

(3) Zhang, Jufen, et al. "Predicting hospitalization due to worsening heart failure using daily weight measurement: analysis of the Trans‐European Network‐Home‐Care Management System (TEN‐HMS) study." European journal of heart failure 11.4 (2009): 420-427.

(4) Ehlers , John F. "The MACD Indicator Revisited." (1991).