The Moving Average Convergence-Divergence (MACD) is a popular and versatile indicator that appears in a number of trading systems. In it’s most basic form, we have the difference between two exponential moving averages (EMA), one fast and the other slow. The MACD is the difference between these two EMAs. From this simple beginning a host of other indicators such as signal lines and MACD bars are built. We’ll show you how to implement each of these, but refer back to this article for a thorough explanation.

In this post, we’re going to implement and backtest three versions of the MACD in Python to illustrate how to trade it and discuss areas for improvement.

Of course, if you want to skip all the code, you can try our new platform for free here to test these ideas.

**Mean-Reverting MACD**

A good place to get started with the MACD is to see when the signal diverges, i.e. when the MACD moves far away from 0. A mean-reverting strategy can quickly be built on top of this.

Our strategy is going to use standard parameters for the MACD. Our fast EMA will look over the past 12 days, and the slow EMA will run over 26 days. The model is going to buy our stock when the price breaks below a certain threshold, and will sell when the MACD converges back to 0. If the MACD runs high, we’ll short the stock and sell when it gets back to 0. We’re simply trying to jump on large, outlier movements in the price with the hope that the price will move back towards the longer EMA.

To get going, fire up Python and import the following packages.

```
import numpy as np
import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
```

To start, we need to calculate the MACD. As stated above, the MACD is built on top of the EMA, so we’re going to write a few functions to calculate the EMA, one to plug in the values, the other that will initialize it and apply it to our data frame. From there, we can write our MACD function that will take these parameters, calculate the EMAs, our MACD, and fill in our data frame.

The code for the following functions was taken from this previous post on the MACD and will go through all of the details on the code and calculations.

```
def _calcEMA(P, last_ema, N):
return (P - last_ema) * (2 / (N + 1)) + last_ema
def calcEMA(data: pd.DataFrame, N: int, key: str = 'Close'):
# Initialize series
data['SMA_' + str(N)] = data[key].rolling(N).mean()
ema = np.zeros(len(data)) + np.nan
for i, _row in enumerate(data.iterrows()):
row = _row[1]
if np.isnan(ema[i-1]):
ema[i] = row['SMA_' + str(N)]
else:
ema[i] = _calcEMA(row[key], ema[i-1], N)
data['EMA_' + str(N)] = ema.copy()
return data
def calcMACD(data: pd.DataFrame, N_fast: int, N_slow: int):
assert N_fast < N_slow,
("Fast EMA must be less than slow EMA parameter.")
# Add short term EMA
data = calcEMA(data, N_fast)
# Add long term EMA
data = calcEMA(data, N_slow)
# Subtract values to get MACD
data['MACD'] = data[f'EMA_{N_fast}'] - data[f'EMA_{N_slow}']
# Drop extra columns
data.drop(['Open', 'High', 'Low', 'Volume',
'Dividends', 'Stock Splits'], axis=1, inplace=True)
return data
```

Now with the MACD in place, we’re going to write two more functions. One is going to be our strategy function, and the other will calculate all of our returns. We’re breaking this second one into its own function because we can re-use the code later when we write other MACD strategies.

The **MACDReversionStrategy** is where our strategy is implemented (if you didn't guess that by the name). This is a simple, vectorized implementation that is just going to look for our level value, and trade if the MACD moves outside of its bounds.

```
def calcReturns(df):
df['returns'] = df['Close'] / df['Close'].shift(1)
df['log_returns'] = np.log(df['returns'])
df['strat_returns'] = df['position'].shift(1) * df['returns']
df['strat_log_returns'] = df['position'].shift(1) * \
df['log_returns']
df['cum_returns'] = np.exp(df['log_returns'].cumsum()) - 1
df['strat_cum_returns'] = np.exp(
df['strat_log_returns'].cumsum()) - 1
return df
def MACDReversionStrategy(data, N_fast=12, N_slow=26,
level=1, shorts=True):
df = calcMACD(data, N_fast, N_slow)
# Drop extra columns
df.drop(['Open', 'High', 'Low', 'Volume',
'Dividends', 'Stock Splits'], axis=1, inplace=True)
df['position'] = np.nan
df['position'] = np.where(df['MACD']<level, 1, df['position'])
if shorts:
df['position'] = np.where(df['MACD']>level, -1, df['position'])
df['position'] = np.where(df['MACD'].shift(1)/df['MACD']<0,
0, df['position'])
df['position'] = df['position'].ffill().fillna(0)
return calcReturns(df)
```

The next step is to get data. We’ll rely on the **yfinance** package to get some free data from Yahoo! Finance. For backtests, more data is better, so we're going to grab an older company so we can see how this performs over a long, multi-decade time horizon, so let's choose 3M (ticker: MMM) and see how this strategy works.

```
ticker = 'MMM'
start = '2000-01-01'
end = '2020-12-31'
yfObj = yf.Ticker(ticker)
df = yfObj.history(start=start, end=end)
N_fast = 12
N_slow = 26
df_reversion = MACDReversionStrategy(df.copy(),
N_fast=N_fast, N_slow=N_slow, level=1)
fig, ax = plt.subplots(2, figsize=(12, 8), sharex=True)
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
ax[0].plot(df_reversion['strat_cum_returns']*100,
label='MACD Reversion')
ax[0].plot(df_reversion['cum_returns']*100, label=f'{ticker}')
ax[0].set_ylabel('Returns (%)')
ax[0].set_title(
'Cumulative Returns for MACD Reversion Strategy' +
f'and Buy and Hold for {ticker}')
ax[0].legend()
ax[1].plot(df_reversion['MACD'])
ax[1].axhline(level, label='Short Level', color=colors[1],
linestyle='--')
ax[1].axhline(-level, label='Long Level', color=colors[1],
linestyle='--')
ax[1].axhline(0, label='Neutral', color='k', linestyle='--')
ax[1].set_xlabel('Date')
ax[1].set_ylabel('MACD')
ax[1].set_title(f'{N_fast}/{N_slow} MACD for {ticker}')
plt.tight_layout()
plt.show()
```

Our MACD mean reversion strategy outpaces the underlying stock from 2000–2016, before the strategy begins giving back some value and then underperforms for a few years. Starting in 2018 and continuing through the end of 2020, however, the strategy simply takes off!

One thing to notice about this model, is that the range of values for the MACD increases over time. It’s likely the case that the signal value we chose (-1 and 1 for simplicity) aren’t optimal and an adaptive value may prove better. The drawback of this is that we now have another parameter we need to fit, which can lead to overfitting our results.

**MACD with Signal Line**

This next method relies on the **MACD signal line**. This is just the EMA of the MACD itself. Because of this, we can re-use a lot of our code from the previous functions to add the signal line to our data.

```
def calcMACDSignal(data: pd.DataFrame, N_fast: int, N_slow: int,
N_sl: int = 9):
data = calcMACD(data, N_fast=N_fast, N_slow=N_slow)
data = calcEMA(data, N_sl, key='MACD')
# Rename columns
data.rename(
columns={f'SMA_{N_sl}': f'SMA_MACD_{N_sl}',
f'EMA_{N_sl}': f'SignalLine_{N_sl}'}, inplace=True)
return data
```

A common way to trade the MACD with the signal line, is to buy when the MACD is above the signal line, and short or sell if the lines cross again.

```
def MACDSignalStrategy(data, N_fast=12, N_slow=26, N_sl=9,
shorts=False):
df = calcMACDSignal(data, N_fast, N_slow, N_sl)
df['position'] = np.nan
df['position'] = np.where(df['MACD']>df[f'SignalLine_{N_sl}'], 1,
df['position'])
if shorts:
df['position'] = np.where(df['MACD']<df[f'SignalLine_{N_sl}'],
-1, df['position'])
else:
df['position'] = np.where(df['MACD']<df[f'SignalLine_{N_sl}'],
0, df['position'])
df['position'] = df['position'].ffill().fillna(0)
return calcReturns(df)
```

For the signal line, it’s fairly typical to look at the 9-day EMA, so that’s what we’ll use here. Again, no fiddling with the settings or optimization, we’re just taking a first pass at the backtest.

```
N_sl = 9
df_signal = MACDSignalStrategy(df.copy(), N_fast=N_fast,
N_slow=N_slow, N_sl=N_sl)
fig, ax = plt.subplots(2, figsize=(12, 8), sharex=True)
ax[0].plot(df_signal['strat_cum_returns']*100,
label='MACD Signal Strategy')
ax[0].plot(df_signal['cum_returns']*100, label=f'{ticker}')
ax[0].set_ylabel('Returns (%)')
ax[0].set_title(
f'Cumulative Returns for MACD Signal Strategy and' +
f'Buy and Hold for {ticker}')
ax[0].legend()
ax[1].plot(df_signal['MACD'], label='MACD')
ax[1].plot(df_signal[f'SignalLine_{N_sl}'],
label='Signal Line', linestyle=':')
ax[1].set_xlabel('Date')
ax[1].set_ylabel('MACD')
ax[1].set_title(f'{N_fast}/{N_slow}/{N_sl} MACD for {ticker}')
ax[1].legend()
plt.tight_layout()
plt.show()
```

Here, our results weren’t nearly as great compared to the mean reversion model (the interplay between the signal line and the MACD is hard to discern at this time scale, zooming in will show its many crosses). This strategy does, however, avoid large or long drawdowns. It seems to be slow and steady, but does fail to win the race via a long-only strategy.

In a more complex strategy where you are managing a portfolio of strategies, an equity curve like we’re showing for the MACD signal strategy could be very valuable. The steady returns could provide a safe haven to allocate capital to if you’re high-return strategies suddenly underperform due to a shift in the market regime.

If you’re looking for all out returns, changing up some of the parameters could be a good way to go beyond the standard values we used here.

**MACD Momentum**

Another technique that is frequently used is to take the difference between the MACD and the signal line. This is then plotted as vertical bars and called **MACD bar** or **MACD histogram** plots. If the bars grow over time, then we have increasing momentum and go long, if they decrease, then we have slowing momentum and a possible reversal.

Calculating this is rather straightforward, we just take our MACD and subtract off the signal line. Again, we can re-use a lot of our previous code to keep things short and sweet.

```
def calcMACDBars(data: pd.DataFrame, N_fast: int, N_slow: int,
N_sl: int = 9):
data = calcMACDSignal(data, N_fast=N_fast, N_slow=N_slow,
N_sl=N_sl)
data['MACD_bars'] = data['MACD'] - data[f'SignalLine_{N_sl}']
return data
```

The histogram is positive when the MACD is above the signal line, and negative when it falls below.

This is an indicator of an indicator and is a few steps removed from the actual price. Regardless, there are a variety of ways to trade using it. One way is to look for increasing or decreasing momentum. If consecutive, positive bars grow, it means that we have a bullish signal for increasing momentum as the MACD moves away from the signal line. If they’re moving closer to one another, then we have a bearish signal and can short it.

We can also look for crossovers, however this is the same as the signal line strategy we implemented above.

You’ll also see things such as peak-trough divergences or slant divergences to generate signals. These look for consecutive hills in the MACD histogram chart and are usually picked up via visual inspection.

We’re going to start with a momentum strategy that looks at the growth of consecutive bars. We’ll use the standard, 12/26/9 format for the MACD signal line parameters, but we’ll see if we can pick up on three consecutive days of growth (positive or negative) in the bars for buy/short signals.

```
def MACDMomentumStrategy(data, N_fast=12, N_slow=26, N_sl=9,
N_mom=3, shorts=False):
df = calcMACDBars(data, N_fast=N_fast, N_slow=N_slow, N_sl=N_sl)
df['growth'] = np.sign(df['MACD_bars'].diff(1))
df['momentum'] = df['growth'].rolling(N_mom).sum()
if shorts:
df['position'] = df['momentum'].map(
lambda x: np.sign(x) * 1 if np.abs(x) == N_mom else 0)
else:
df['position'] = df['momentum'].map(
lambda x: 1 if x == N_mom else 0)
df['position'] = df['position'].ffill().fillna(0)
return calcReturns(df)
```

Running and plotting this new bot strategy:

```
N_mom = 3
df_mom = MACDMomentumStrategy(df.copy(), N_fast=N_fast,
N_slow=N_slow,N_sl=N_sl, N_mom=N_mom)
fig, ax = plt.subplots(3, figsize=(12, 8), sharex=True)
ax[0].plot(df_mom['strat_cum_returns']*100, label='MACD Momentum')
ax[0].plot(df_mom['cum_returns']*100, label=f'{ticker}')
ax[0].set_ylabel('Returns (%)')
ax[0].set_title(f'Cumulative Returns for MACD Signal Strategy and Buy and Hold for {ticker}')
ax[0].legend()
ax[1].plot(df_mom['MACD'], label='MACD')
ax[1].plot(df_mom[f'SignalLine_{N_sl}'], label='Signal Line',
linestyle=':')
ax[1].set_title(f'{N_fast}/{N_slow}/{N_sl} MACD for {ticker}')
ax[1].legend()
ax[2].bar(df_mom.index, df_mom['MACD_bars'], label='MACD Bars',
color=colors[4])
ax[2].set_xlabel('Date')
ax[2].set_title(f'MACD Bars for {ticker}')
plt.tight_layout()
plt.show()
```

This one doesn’t look so great. 20 years and losing 15% is not a strategy I want to be invested in. If we look a little deeper into this one, we can see that we jump in and out of a lot of positions very quickly. Usually we have some momentum, but because the momentum signal we’re looking at can change after a day or two, we aren’t in a position long enough to ride a trend. Essentially, we’re acting like a trend follower, but we bail so quickly we wind up taking a lot of small losses and have very few gains to show for it.

To alleviate this, let’s see if we can improve by using our MACD bars to signal momentum and only exit a position when the MACD bar crosses over. To see this, we’ll look for a sign change from positive to negative or vice versa.

```
def MACDSignalMomentumStrategy(data, N_fast=12, N_slow=26, N_sl=9,
N_mom=3, shorts=False):
df = calcMACDBars(data, N_fast=N_fast, N_slow=N_slow, N_sl=N_sl)
df['growth'] = np.sign(df['MACD_bars'].diff(1))
df['momentum'] = df['growth'].rolling(N_mom).sum()
# Enter a long/short position if momentum is going in the right
# direction and wait for cross-over
position = np.zeros(len(df)) + np.nan
for i, _row in enumerate(data.iterrows()):
row = _row[1]
mom = row['momentum']
if np.isnan(mom):
last_row = row.copy()
continue
if np.abs(mom) == N_mom and position[i-1] == 0:
# Enter new position
if shorts:
position[i] = np.sign(mom)
else:
position[i] = 1 if np.sign(mom) == 1 else 0
elif row['MACD_bars'] / last_row['MACD_bars'] < 0:
# Change in sign indicates cross-over -> exit
position[i] = 0
else:
# Hold position
position[i] = position[i-1]
df['position'] = position
return calcReturns(df)
```

Running this combined strategy our bot gets the following results:

```
df_sig_mom = MACDSignalMomentumStrategy(df.copy(), N_fast=N_fast,
N_slow=N_slow,N_sl=N_sl, N_mom=N_mom)
fig, ax = plt.subplots(3, figsize=(12, 8), sharex=True)
ax[0].plot(df_sig_mom['strat_cum_returns']*100,
label='MACD Momentum')
ax[0].plot(df_sig_mom['cum_returns']*100, label=f'{ticker}')
ax[0].set_ylabel('Returns (%)')
ax[0].set_title(f'Cumulative Returns for MACD Signal Strategy' +
f'and Buy and Hold for {ticker}')
ax[0].legend()
ax[1].plot(df_sig_mom['MACD'], label='MACD')
ax[1].plot(df_sig_mom[f'SignalLine_{N_sl}'], label='Signal Line',
linestyle=':')
ax[1].set_title(f'{N_fast}/{N_slow}/{N_sl} MACD for {ticker}')
ax[1].legend()
ax[2].bar(df_sig_mom.index, df_sig_mom['MACD_bars'],
label='MACD Bars', color=colors[4])
ax[2].set_xlabel('Date')
ax[2].set_title(f'MACD Bars for {ticker}')
plt.tight_layout()
plt.show()
```

This combined model does perform better, yielding us about 250% over this time frame. That is still about half of the long-only strategy that we’re trying to beat, but we could continue to tinker with these ideas to come up with something even better.

Before moving on, let’s take a look at some of the key metrics for each of these strategies.

```
def getStratStats(log_returns: pd.Series,
risk_free_rate: float = 0.02):
stats = {}
# Total Returns
stats['tot_returns'] = np.exp(log_returns.sum()) - 1
# Mean Annual Returns
stats['annual_returns'] = np.exp(log_returns.mean() * 252) - 1
# Annual Volatility
stats['annual_volatility'] = log_returns.std() * np.sqrt(252)
# Sharpe Ratio
stats['sharpe_ratio'] = (
(stats['annual_returns'] - risk_free_rate)
/ stats['annual_volatility'])
# Max Drawdown
cum_returns = log_returns.cumsum()
peak = cum_returns.cummax()
drawdown = peak - cum_returns
stats['max_drawdown'] = drawdown.max()
# Max Drawdown Duration
strat_dd = drawdown[drawdown==0]
strat_dd_diff = strat_dd.index[1:] - strat_dd.index[:-1]
strat_dd_days = strat_dd_diff.map(lambda x: x.days).values
strat_dd_days = np.hstack([strat_dd_days,
(drawdown.index[-1] - strat_dd.index[-1]).days])
stats['max_drawdown_duration'] = strat_dd_days.max()
return stats
```

Getting stats for each strategy:

```
stats_rev = getStratStats(df_reversion['strat_log_returns'])
stats_sig = getStratStats(df_signal['strat_log_returns'])
stats_mom = getStratStats(df_mom['strat_log_returns'])
stats_sig_mom = getStratStats(df_sig_mom['strat_log_returns'])
stats_base = getStratStats(df_reversion['log_returns'])
stats_dict = {'Mean Reversion': stats_rev,
'Signal Line': stats_sig,
'Momentum': stats_mom,
'Signal-Momentum': stats_sig_mom,
'Baseline': stats_base}
pd.DataFrame(stats_dict)
```

This baseline was pretty tough to beat. Over 9% annual returns despite some large drawdowns, however, the MACD mean reversion strategy beat it easily and with a better Sharpe Ratio to boot. The signal line and the momentum model both severely underperformed the baseline — with the MACD momentum model just being flat out atrocious. However, combining these two yielded better results, still below the baseline, but with a reasonable Sharpe Ratio and a lower drawdown (albeit longer than the baseline).

**No Guarantees**

Each of these models could be tweaked and improved, but they’re only a small taste of the possibilities available to trade using the MACD and its derivatives. No matter how good that mean reversion strategy looks, nothing here should be blindly implemented.

While we ran a fine backtest, there are issues. Dividends weren’t taken into account, transaction costs were ignored, slippage, only one stock was examined, and so forth. If you really want to trade algorithmically, you’re better off with a strategy that manages a diversified portfolio, on data that has been adjusted for dividends, with transaction costs, over a long time horizon.

At Raposa, we’re building an engine that will enable you to quickly test ideas like these on a portfolio of securities, with proper money management, a plethora of signals, and high quality data, all without a single line of code. We have a state-of-the-art system that will allow you to customize your model to suit your needs, and deploy it when you’re ready so you can trade live. If you want to move beyond simple backtests and strategies, then sign up below to be kept up to date on our latest developments.