Beginner’s Guide to Algo Trading Part III: Backtest Trading Strategy in Python

Sarp Nalcin
4 min readAug 26, 2022

--

Photo by Nick Chong on Unsplash

This is the last section of “Beginner’s Guide to Algo Trading” where we will perform a Backtest for the Trading Strategy we built in the previous two articles. Those that have not read the previous posts are highly recommended to check them out below for better understanding of the Backtest. If you already read or if you are familiar with testing the performance of trading strategies, then sit back and relax. You are at the right spot

TL;DR

We use RSI, BB, MFI to generate trading signals for a long/short Trading Strategy for BTC/USDT and ETH/USDT pairs, visualize them on price chart and perform Backtest to see the result with metrics such as cumulative return and win ratio

Check out my Github page below to jump in the code:

What is Backtesting?

Backtesting is a methodology to assess the performance of a trading strategy by discovering how it would play out with historical dataset. In other words, backtesting is a simulation of trading strategy on past data. If the Backtest results are satisfactory, then the trading strategy may be exploited going forward. For more information, you can check the link below:

Under the umbrella of backtesting, there are 5 metrics to check before exploiting a trading strategy in real. For this article, we will focus on 4 of those metrics, elaborated here:

Cumulative Return (most critical one): Shows cumulative return at the end of the Backtest period

Profitability (in terms of number of positions): # of profitable positions divided by sum of # of profitable positions and # of unprofitable positions

Win Ratio: # of profitable positions divided by # of unprofitable positions

Maximum Drawdown: Shows minimum cumulative return experienced in the Backtest period

Main misconception about backtesting is that beginner traders focus solely on the Cumulative Return of the trading strategy while missing the rest. Although it is true that Cumulative Return is one of the key metrics, it is not the only one to consider and skipping checking the other metrics may result in significant damages to your portfolio

Photo by Loic Leray on Unsplash

Backtesting our Trading Strategy

As extensively explained in Part II, our Trading Strategy combines signals from 3 technical indicators to produce long/short positions for BTC/USDT and ETH/USDT pairs. These indicators are RSI (Relative Strength Index), BB (Bollinger Bands) and MFI (Money Flow Index)

For backtesting, we assume at every trading signal, the position size is 1 BTC for BTC/USDT and 1 ETH for ETH/USDT. To clarify, when there is a buy signal (long) for BTC/USDT, the position size will be 1 BTC and for a buy signal for ETH/USDT, the position size will be 1 ETH, vice versa for sell signal

Because we are not going to use a package to do the Backtest but build it on our own, we need to prepare the dataset before getting the metrics. All the transformation done on the dataset can be found in the comments below:

The dataset is ready now. Let’s produce the metrics and see how they look like for BTC/USDT pair (note that depending on your time period, you may have different results then here which is normal)

We observe that Cumulative Return: 2.58%, Profitability: 62%, Win Ratio: 1.62 and Maximum Drawdown: 2.58%. Having the Cumulative Return > 0 is a powerful point. Also, Profitability > 50% shows the Trading Strategy performs quite well

We can now repeat the Backtest for ETH/USDT, compare the results with BTC/USDT

Backtesting metrics for ETH/USDT can be achieved here:

As opposed to what we saw for BTC/USDT, Cumulative Return: -6.14%, Profitability: 43%, Win Ratio: 0.75 and Maximum Drawdown: -15.93%. of ETH/USDT does not look well. Especially, Cumulative Return is way below zero and Maximum Drawdown shows that the Trading Strategy resulted in huge losses during the time period

Conclusion

We started with setting up the Python environment to extract data from cryptocurrency exchange Binance. Then, we produced trading signals by using Technical Indicators RSI, BB and MFI to check the trading signal frequency of each of these indicators. Later on, we built a Trading Strategy by waiting for a buy/sell signal from each of the three indicators to go in position. In the end, we backtested this strategy and concluded that it is a profitable one for BTC/USDT but not ETH/USDT

I hope you enjoyed this Beginner`s Guide. Do not forget to like my article!

Disclaimer: Statements are for educational purposes only. Nothing contained herein is intended to be or to be constructed as financial advice.

--

--