Backtrader MACD Indicator Review

Introduction

The popularity of the Moving Average Convergence Divergence (MACD) indicator makes it a good candidate to test. It was first developed in the late 1970’s by Gerald Appel and provides a trader with an indication of an instruments strength, direction and momentum. In general, it shows the relationship between two moving averages but at first glance, it may seem that there is more going on. Let’s break down the different parts.

MACD Annotated

  • MACD Line: This line is created by subtracting the value of a fast exponential moving average (EMA) from a second slower EMA. Therefore, the MACD line is indicating the difference between the two values. When the line is above zero this means that the fast EMA is above the slow EMA (in other words, in an uptrend). Conversely, when the line is below zero the fast EMA value is lower than the slow EMA. Finally, when the line crosses zero, that means an EMA crossover has occurred.
  • Signal Line: The signal line is an exponential moving average of the MACD line values. This means it will lag the MACD line and will result in the crossovers between the two lines. Thus, buy/sell signals can be generated from the crossovers.
  • Histogram:  The histogram shows the relationship between the MACD line and the signal line.  Just like how the MACD line shows the relationship between two EMA’s, The histogram will cross the zero line when the signal line crosses the MACD line.

MACD Signals

A number of signals can be generated for entries and exits when using the MACD indicator. Let’s take a quick look at them.

  • Signal line crossover: Buy on a cross up and sell on a cross down.
  • MACD line zero crossover: Essentially an EMA crossover. Again, buy on the cross up and sell on a cross down.
  • Bullish divergence: When the price on the charts makes a new low but the MACD line makes a higher low.
  • Bearish divergence: As you might guess, it is the opposite of the above. It occurs when the price on the charts makes a new high but the MACD line makes a lower high.

Test Strategy

Since there are a number of different signal interpretations, we shall perform an individual test for each signal. During those tests, we shall simply enter when the signal is made and exit when the opposite signal occurs. Additionally, a more complex strategy will be created which shall require confirmation between 2 of the possible signals that can be generated.

These tests form part of the series to test and compare Backtrader’s library of built-in indicators. For the series introduction, some notes on the test methodology and results summary table see here: https://backtest-rookies.com/2017/07/31/backtraders-best-forex-indicators/

Note: Results below do not reflect real-world performance. As mentioned in the introduction and overview post (linked above), no commissions, leverage or margin are used during testing. The purpose of these tests is to compare the indicators to one and another where all other factors are equal.  For more information on how all indicator reviews will be performed, please click on the link above. 

Optimization

The MACD indicator has several input parameters. Therefore, in the interest time and efficiency not every single combination of parameters was tested. In the end, optimization was performed using the default settings as a base (12,26,9) and then:

  • Testing multiples of these values. E,g 24,52,18.
  • A “popular”  (5,35,5) setting according to Wikipedia.
  • Common moving average crossover settings.

The final optimization list can be found here: MACD Review: Optimization List

Note: Some additional optimization was performed for the divergence and confluence tests and is discussed further below. 

Results

Now we get to the fun stuff. Let’s start with a summary of the results for those who are not interested in the commentary or test notes.

Test CaseBest PnLWorst PnLDefault Settings AVG PnLAvg PnLBest Strike RateWorst Strike RateAvg Strike RateBest Settings (Avg all markets)Worst Settings (Avg all markets)Best Settings Avg PnLWorst Settings Avg PnL
MACD - Signal Cross285.2-211.6656.667531.3443.6225.6835.19fast_ema: 7 Slow ema: 21 signal_period: 5fast_ema: 50 slow_ema: 100 signal_period: 10129.2925-96.7375
MACD - Zero Cross239.95-204.3416.8738.1246.1521.7431.18fast_ema: 7 slow_ema: 21 signal_period: 7fast_ema:12 slow_ema: 26 signal_period:9151.715-29.1275
MACD - Divergence517.83-447.88112.737554.7364.7123.8149.42fast_ema: 7 slow_ema:21 signal_period:14 swing_period: 7fast_ema:40 slow_ema:200 signal_period: 40 swing_period: 21201.9425-137.465
MACD - Divergence With Signal Cross Confirmation270.48-178.7334.117511.568.759.0936.3fast_ema: 40 slow_ema: 200 signal_period: 10 swing_period: 14fast_ema: 20 slow_ema:50 signal_period:10 swing_period: 596.955-59.995
MACD - Divergence With Zero Cross Confirmation217.1-118.3617.797510.49100034.89fast_ema: 6 slow_ema: 13 signal_period: 4 swing_period: 14fast_ema: 24 slow_ema: 52 signal_period: 18 swing_period: 570.32-42.22
MACD - Divergence With Zero Cross entry and Signal Cross Exit197.54-110.8345.5152.06100038.28fast_ema: 12 slow_ema: 26 signal_period: 12 swing_period: 7fast_ema: 36 slow_ema: 78 signal_period: 36 swing_period: 748.945-50.8125

The table above follows the same format as the table found in the Best Forex Indicators overview. As such, the same table notes apply here.

  1. Default Settings Avg PNL: Is calculated from the average PNL recorded in all four markets under test.
  2. The Best strike rate may not be from the same test as the best PnL In some cases it may come from a test with different settings or market.
  3. The best and worst settings take the average PnL for that particular setting across all four markets. The best and worst average results are then used for this column.

After reviewing the results, I was a little surprised to see that trading on simple MACD divergence signals was most profitable. This was still the case when taking the best settings on average across all markets (instead of just focusing on what could be a lucky parameter configuration for one market).

Signal Crossover

Intended originally to generate the buy and sell signals, the signal crossover test was performed first.

Entry/Exit Criteria

  • Long: Following a signal/macd cross up
  • Short: Following a signal/macd cross down
  • Exit Long: Following a signal/macd cross down
  • Exit Short: Following signal/macd cross up

Result

Best PnLWorst PnLDefault Settings AVG PnLAvg PnLBest Strike RateWorst Strike RateAvg Strike RateBest Settings (Avg all markets)Worst Settings (Avg all markets)Best Settings Avg PnLWorst Settings Avg PnL
285.2-211.6656.667531.3443.6225.6835.19fast_ema: 7 Slow ema: 21 signal_period: 5fast_ema: 50 slow_ema: 100 signal_period: 10129.2925-96.7375

The signal crossover tended to favour parameters using shorter EMA’s. The absolute best settings across all markets were actually shorter in period lengths than the default settings of the indicator. Regarding effectiveness, the signal crossover method sat in the middle of the pack in terms of average best settings across all markets.

This slideshow requires JavaScript.

Zero Crossover

The zero crossover technique is essentially the same as an exponential moving average crossover strategy. In other words, If you setup two exponential moving averages and bought and sold on the crossovers, the results should be the same as these for the parameters tested. It is also worth noting that due to the way that this strategy works, the signal line has no effect on the final PnL. Therefore the best settings noted below would yield the same results no matter what the signal level parameter was set to.

Entry/Exit Criteria

  • Long: Following zero cross up
  • Short: Following a zero cross down
  • Exit Long: Following a zero cross down
  • Exit Short: Following a zero cross up

Result

Best PnLWorst PnLDefault Settings AVG PnLAvg PnLBest Strike RateWorst Strike RateAvg Strike RateBest Settings (Avg all markets)Worst Settings (Avg all markets)Best Settings Avg PnLWorst Settings Avg PnL
239.95-204.3416.8738.1246.1521.7431.18fast_ema: 7 slow_ema: 21 signal_period: 7fast_ema:12 slow_ema: 26 signal_period:9151.715-29.1275

Given that the zero crossover is just a moving average, there I had no expectations that it would perform much differently to the moving averages reviewed here: Backtrader Simple Moving Average Crossover Review. However, having seen the results it appears that EMA based strategies prefer shorter look-back periods. This is something I will be interested to explore further on another review.

This slideshow requires JavaScript.

Bullish / Bearish Divergence

To perform testing properly, a pivot indicator needed to be applied to strategy in order to identify the pivots/swings. Therefore, keep this in mind as you read on. The setting for this extra variable alone can influence the outcome of the tests. The pivot indicator used simply has a lookback period. It will look back a historical candle and check whether it was the highest high or lowest low both before and after it. If so, a signal will be generated indicating a pivot has been found.

The pivot indicator selected identifies pivots in a simple manner. It will look back a historical candle and check whether that candle was the highest high or lowest low for a given period both before and after it. If so, a signal will be generated indicating a pivot has been found. In testing this strategy, short and long look back periods were used when optimizing. (5,7,9,13,21 days). A look back period of 7, for example, will check 7 days before and after the candle.

Entry/Exit Criteria

The strategy will look the prices of the last two swing highs/lows and compare them to the MACD line values on the same bar/candles. If the price is rising but the MACD is declining, we have a divergence.

  • Long: When bullish divergence is detected
  • Short: When bearish divergence is detected
  • Exit Long: When bearish divergence is detected
  • Exit Short: When bullish divergence is detected

Finally, if we are not in a position but detect both bullish and bearish divergences at the same time, then we do nothing as the signal is inconclusive. This is possible given swing highs and lows are checked separately.

Result

Best PnLWorst PnLDefault Settings AVG PnLAvg PnLBest Strike RateWorst Strike RateAvg Strike RateBest Settings (Avg all markets)Worst Settings (Avg all markets)Best Settings Avg PnLWorst Settings Avg PnL
517.83-447.88112.737554.7364.7123.8149.42fast_ema: 7 slow_ema:21 signal_period:14 swing_period: 7fast_ema:40 slow_ema:200 signal_period: 40 swing_period: 21201.9425-137.465

Now we come onto the outstanding performer. Of all the methods tested this one is far ahead of the pack. Both in terms of overall best performance and also average best settings. The average strike rate was about 50% which could be quite nice if the same strike rate can be achieved with a sensible stop loss and good risk/reward in play. This is something I may look to test in a future post.

This slideshow requires JavaScript.

As noted above for the zero cross test, the signal parameter has no bearing on the final PnL outcome.

MACD Confluence

The final set of tests aims to combine the divergence signal with a second confirmation before entering a position. Often traders like to wait for confirmation of a signal before trading. As such, it worth looking at whether one of the MACD’s other signals could act as good confirmation.

Entry/Exit Criteria

  • Long: When bullish divergence AND a signal cross up is detected
  • Short: When bearish divergence AND a signal cross down is detected
  • Exit Long: When bearish divergence OR a signal cross down is detected
  • Exit Short: When bullish divergence OR a signal cross down is detected

For completeness, the same test has been performed by replacing the signal crossover with a zero crossover and then finally, a mix of the two.

Confluence with Signal Crossover

Best PnLWorst PnLDefault Settings AVG PnLAvg PnLBest Strike RateWorst Strike RateAvg Strike RateBest Settings (Avg all markets)Worst Settings (Avg all markets)Best Settings Avg PnLWorst Settings Avg PnL
270.48-178.7334.117511.568.759.0936.3fast_ema: 40 slow_ema: 200 signal_period: 10 swing_period: 14fast_ema: 20 slow_ema:50 signal_period:10 swing_period: 596.955-59.995

Interestingly, we can see from the strike rate image below that adding the second confirmation actually decreased the average strike rate marginally compared to just trading divergence by itself. The signal crossover confirmation method was the best performing of the 3 confirmation methods tests. However, the performance pales in comparison to the tests without confirmation.

This slideshow requires JavaScript.

Confluence with Zero Crossover

Best PnLWorst PnLDefault Settings AVG PnLAvg PnLBest Strike RateWorst Strike RateAvg Strike RateBest Settings (Avg all markets)Worst Settings (Avg all markets)Best Settings Avg PnLWorst Settings Avg PnL
217.1-118.3617.797510.49100034.89fast_ema: 6 slow_ema: 13 signal_period: 4 swing_period: 14fast_ema: 24 slow_ema: 52 signal_period: 18 swing_period: 570.32-42.22

Testing MACD divergence with a zero crossover confirmation shows an extreme strike rate range. Both 0% and 100% were recorded. After checking the results, this was found to be caused by the parameter combinations not generating enough valid signals over time.  In other words, we had one valid setup and one win/loss. The overall performance was a little worse than the signal crossover variant. Having said that, you could also argue that it is a little safer as losses were generally less for the worst settings too.

This slideshow requires JavaScript.

Confluence Mixing Zero and Signal Crossovers

The final test attempts to tune the MACD a little further by using the zero crossover signal for entries but exiting on the signal crossover in an attempt to get out nearer to the top/bottom of a move.

Best PnLWorst PnLDefault Settings AVG PnLAvg PnLBest Strike RateWorst Strike RateAvg Strike RateBest Settings (Avg all markets)Worst Settings (Avg all markets)Best Settings Avg PnLWorst Settings Avg PnL
197.54-110.8345.5152.06100038.28fast_ema: 12 slow_ema: 26 signal_period: 12 swing_period: 7fast_ema: 36 slow_ema: 78 signal_period: 36 swing_period: 748.945-50.8125

Here we have the same issue with the strike rate as we found with just the test above. Looking at the 12-year returns below shows that the best settings evaluated were skewed by a year of significant outperformance. However, following this, the strategy would have taken some faith to stick with over the years that follow.

This slideshow requires JavaScript.