avatarDanny Groves

Summary

A Python backtest of a dual MACD trading strategy for Bitcoin shows it outperforms a buy and hold strategy with a 115x return compared to 49x, while also avoiding significant drawdowns.

Abstract

The article presents a detailed analysis of a dual Moving Average Convergence Divergence (MACD) trading strategy for Bitcoin (BTC-USD), as introduced by the Financial Wisdom YouTube channel. The strategy, which involves going long when both daily and weekly MACD lines are above their respective signal lines, has been backtested using Python and yields a 48% win rate with an average profit of 16%. The backtest results, dated June 28, 2022, indicate that the strategy significantly outperforms the traditional buy and hold approach, with a return of 115 times the investment compared to 49 times for buy and hold, while being invested only 34% of the time. The strategy also includes a risk management component through the use of stop-loss orders, which are adjusted based on the MACD's performance. Although the strategy shows promising results for Bitcoin, its application to other assets like Tesla (TSLA) stock does not yield the same level of outperformance, emphasizing the importance of testing trading strategies across multiple assets.

Opinions

  • The author is impressed by the dual MACD strategy's performance, particularly its ability to outperform the buy and hold strategy for Bitcoin.
  • The strategy's effectiveness is attributed to its reliance on confirmed uptrends and good entry points, as well as its risk management approach.
  • The author believes that the strategy's simplicity is one of its strengths, making it an attractive option for investors.
  • There is a cautionary note that the strategy's success with Bitcoin does not guarantee similar results with other assets, highlighting the need for extensive testing.
  • The author suggests that the strategy could potentially be adapted for use with index funds, hinting at future exploration of this idea.
  • The article encourages readers to connect on LinkedIn or Twitter for further discussion, indicating the author's openness to engagement and feedback on the strategy.
  • A cost-effective AI service is recommended for readers interested in similar performance to ChatGPT Plus (GPT-4), with a special offer mentioned.

A Buy and Hold Beating MACD Strategy Applied to Bitcoin

A python backtest of the strategy from the Financial Wisdom YouTube channel

Photo by Pierre Borthiry on Unsplash

TL;DR: I used Python to backtest the dual Moving Average Convergence Divergence approach laid out by the Financial Wisdom Channel on YouTube. On the BTC-USD trading pair, the strategy outperformed buy and hold and avoided major drawdowns in the asset.

A few weeks ago, I stumbled across the excellent video by the Financial Wisdom channel on YouTube; this video lays out the author’s long-only trading strategy for Bitcoin, which is claimed to have beaten buy and hold considerably while having a small investment time. It’s a price action system that relies on the principles of momentum, with an added element of risk management. Here is the equity curve for the strategy, as compared to the buy and hold approach. The green and red shaded areas correspond to time spent in winning and losing trades.

Naturally, a buy and hold beating super strategy with that equity curve gained my attention, so I was particularly eager to learn more about the strategy and, importantly, to further convince myself it is correct by testing on other assets!

In this article, I’ll go through:

  1. The basic rules of the strategy.
  2. Implement the strategy in Python and a simple backtest (all the code included in the article).
  3. The results from the strategy for the BTC-USD trading pair, and a further test on the Tesla (TSLA) stock.

Please note that all charts in this article are interactive! If you’re viewing on your phone, you may get a better view by clicking on the chart and going to the Datapane website, where it is hosted🙂

Interested? Let’s dive in!

The MACD Indicator

The strategy relies on the Moving Average Convergence Divergence (MACD) technical indicator which is derived from the price action, the “dual” in this strategy means we are considering multiple time-frames to make trading decisions. For those who have not heard of the MACD before, it’s a trend-following indicator which is formed by using two lines:

  1. A MACD line, which is the 26 period EMA minus the 12 period EMA (exponential moving average).
  2. The signal line, which is a 9 period EMA of the MACD line.

The idea is to go long when the MACD is above the signal line, or conversely go short when the MACD is below the signal. Here is a chart of BTC-USD truncated to 2021–2022 to show the regions where the daily MACD was positive (green highlighted areas).

The MACD indicator can be implemented in Python using the following code:

When ran, this code will download the latest BTC-USD data from Yahoo Finance using the excellent yfinance package, and calculate the daily and weekly MACD (you may need to pip-install the yfinancepackage). Note that a flag is required to indicate whether the asset is a stock or not, which tells the code how many days to consider for the calculation of the weekly MACD (i.e. since Bitcoin trades 24/7, a factor of 7 is required to calculate the weekly MACD, for stocks, the factor is 5).

The Strategy and Backtest

Like a good portion of strategies based on technical indicators, a lot of false signals can be generated. The approach of this strategy is to use a “dual” approach, where the daily and weekly MACD are considered; in essence, we only take trades if the long-term trend is in our favour; and hopefully remove a good portion of the false signals.

The strategy is briefly summarised as follows:

  1. If the weekly and daily MACD lines are above their respective signal lines, then we have a trading window of opportunity and can buy in on the next open.
  2. If we are in a trade, we hold until the daily MACD crosses below the signal line — it’s at this point we set a stop-loss to the low of the day.
  3. The stop loss is only adjusted if the MACD crosses below the signal line again (i.e. if the price continues upwards and then the MACD crosses again, we set a higher stop-loss).

And that’s it! Pretty simple right? Here’s a chart to show all the buy signal windows that were open throughout Bitcoin’s life (i.e. when criterion #1 is met).

Wait! If it’s this selective, then how can it possibly work!?

The idea behind this approach is that it heavily relies on an asset having a confirmed up-trending environment (given by the weekly MACD), and then the daily MACD crossing signals a good entry point within that uptrend. We can see that broadly speaking, the highlighted areas are the interesting parts to a long-only trader; the other areas are either consolidations or downtrends (like lately, for example!).

Another good factor is the stop loss setting, which allows for good risk management. This way the risk-reward ratio is tipped in our favour; in other words, we have small losers and let the winners run!

OK, enough chat, show me the backtest!

You asked for it! (well… I did because I wrote the article…)

A few summary points about the above code:

  1. The get_macd script is the code presented earlier in the implementation of the MACD. This file needs to be in the same directory as the code above!
  2. The backtest starts at the first date offered, you may need to adjust if you wish the backtest to start at a different date!
  3. The backtest is implemented with a simple loop — I prefer this because it’s logically easier to read and debug than vectorised code; and if you’re performing backtests on mass then you can wrap the function in a @numba.jit() decorator which speeds up the code when performing many backtests consecutively.
  4. I put in some key stats I am interested in, but perhaps there are others you may wish to add to the dictionary in the print_stats function.

The Results

After running the above script, here’s the results I obtained (backtest performed on the 28th of June, 2022):

  • Win rate = 48%
  • Mean profit = 16%
  • Median profit = -1%
  • Max gain = 203%
  • Max loss = -18%
  • Number of trades = 46
  • Mean holding time = 21 days
  • Invested time/invested time of buy & hold: 982 days/2872 days~ 34% of the time
  • Buy and hold return = 49x
  • Strategy return = 115x

Wow, although there is a 48% win rate, we are invested 34% of the time and returned 115x our money, compared to the buy/hold 49x return!

⚠️Warning!⚠️

This performs great, sure, but we have only considered one asset. What happens if we consider another super performing asset like Tesla (TSLA)?

Clearly not as good, here the buy and hold approach outperforms it considerably. However, we do avoid periods of major drawdown (e.g. 2022) and remain invested a small amount of time, saving us precious opportunity cost where we can deploy our capital in other more lucrative places.

The moral of the story here is, do multiple tests on multiple assets before considering any trading strategy as solid. It could be that the excellent trading strategy you see is rather well curve-fit for that particular asset, when applied to other assets, it could perform poorly!

Wrapping Up

In this article we have seen a backtest for a dual-MACD trading strategy applied to the BTC-USD trading pair, with pretty phenomenal results. It relies heavily on the principles of momentum and risk-management. Overall, I really like this approach; I think if adapted to longer time-frames it could be a useful way to invest in index funds (but more on that in the future).

Thank you for reading, I hope you enjoyed the article! Please feel free to connect with me on LinkedIn — I’d love to hear from you! Also feel free to follow/DM me on twitter.

Crytpocurrencies
Bitcoin
Trading
Investing
Technical Analysis
Recommended from ReadMedium