How to Backtest a Trading Strategy

How to Backtest a Trading Strategy

Intermediate
แƒ’แƒแƒ›แƒแƒฅแƒ•แƒ”แƒงแƒœแƒ”แƒ‘แƒฃแƒšแƒ˜ Dec 17, 2020แƒ’แƒแƒœแƒแƒฎแƒšแƒ”แƒ‘แƒฃแƒšแƒ˜ Jun 21, 2023
7m

TL;DR

Do you think you have great ideas on the market but donโ€™t know how to put them to the test without risking your funds? Knowing how to backtest trade ideas is the bread and butter of a good systematic trader.

The underlying premise of backtesting is that what worked in the past may work in the future. But how do you go about doing this yourself and how should you evaluate the results? Letโ€™s go through a simple backtesting process.

Introduction

Backtesting is one of the key components of developing your own charting and trading strategy. It entails reconstructing trades that would have happened in the past with a system based on historical data. The results of backtesting should give you a general idea of whether or not an investment strategy is effective.

What Is Backtesting?

First, if youโ€™d like a deeper dive into backtesting, read our article What Is Backtesting?ย 

In short, the main purpose of backtesting is to show you whether your trading ideas are valid. You start by using past market data to see how a strategy would have performed. If the strategy looks like it has potential, it may also be effective in a live trading environment.

What to Do Before Backtesting?

Before you start backtesting, you must establish what kind of trader you are. Are you a discretionary or a systematic trader?

Discretionary trading is decision-based โ€” traders use their own judgment to decide when to enter and exit. It's a relatively loose and open-ended strategy, where most of the decisions made depend on the trader's assessment of the conditions at hand. As such, backtesting is less relevant when it comes to discretionary trading since the strategy isnโ€™t strictly defined.

Of course, this doesnโ€™t mean that if youโ€™re a discretionary trader, you shouldnโ€™t backtest or paper trade at all. It just means that the results may not be as reliable as they usually are with systematic trading.

Systematic trading is more applicable to backtesting. Systematic traders rely on a trading system that defines and tells them exactly when to enter and exit. While systematic traders have control over most aspects of the strategy, it determines the entry and exit signals entirely for them. You could think of a simple systematic strategy in two simple steps:

  1. When A and B happen at the same time, enter a trade.ย 

  2. When X happens after, exit the trade.ย 

Some traders prefer this approach. It can eliminate emotional decisions from trading and provide a reasonable degree of assurance that a trading system is profitable. Of course, there are still no guarantees.

This is why itโ€™s important to make sure you have very specific rules in your system for when to enter or exit positions. A strategy that isnโ€™t well-defined will lead to inconsistent results. As you might expect, this trading style is more popular in algorithmic trading.

There is backtesting software you can buy if you want to automate the process โ€” you simply have to input your own data and the software will do the backtesting for you. In this example, however, weโ€™ll go with a manual backtesting strategy. It involves a little bit more work but itโ€™s completely free.

How to Backtest a Trading Strategy?

You can find a Google Sheets spreadsheet template using this link. This is a rudimentary template you can use as a starting point to creating your own. It gives you a general idea of what information a backtesting sheet may contain. Some traders prefer to use Excel or code it in Python; there arenโ€™t strict rules. You can add as much data as you need to it, alongside anything other information you may deem useful.

Date

Market

Side

Entry

Stop Loss

Take Profit

Risk

Reward

PnL

12/08

BTCUSD

Long

$18,000

$16,200

$21,600

10%

20%

3600

12/09

BTCUSD

Short

$19,000

$20,900

$13,300

10%

30%

-1900


Letโ€™s backtest a simple trading strategy:

  • We buy one Bitcoin at the first daily close after a golden cross. We consider a golden cross to be when the 50-day moving average crosses above the 200-day moving average.

  • We sell one Bitcoin at the first daily close after a death cross. We consider a death cross to be when the 200-day moving average crosses below the 50-day moving average.

As you can see, weโ€™ve also defined the time frame in which the strategy is valid. This means if a golden cross happens on the four-hour chart, we wonโ€™t consider it a trading signal.

The time period in this example begins at the start of 2019. However, if youโ€™d like to get more accurate and reliable results, you could go back much further in the history of Bitcoinโ€™s price action.

Now, letโ€™s see what trading signals this system produces for the stipulated time period:

  • Buy @ ~$5,400

  • Sell @ ~$9,200

  • Buy @ ~$9,600

  • Sell @ ~$6,700

  • Buy @ ~$9,000

Hereโ€™s how our signals look when overlaid on the chart:

Golden cross-death cross strategy. Source: TradingView

Our first trade turned a profit of about $3,800, while our second trade resulted in a loss of about $2,900. This means our realized PnL is currently $900.ย 

Weโ€™re also in an active trade, which, as of December 2020, had about $9,000 in unrealized profit. If we stick to our initially defined strategy, weโ€™ll close this when the next death cross happens.ย 

Evaluating Backtesting Results

So, what do these results show? Our strategy would have resulted in a reasonable return but it doesnโ€™t show anything outstanding so far. We could realize the currently open trade to drastically increase our realized PnL, but that would defeat the purpose of backtesting. If we donโ€™t stick to the plan, the results wonโ€™t be reliable, either.

Even though this is a systematic strategy, itโ€™s also worth considering the context. The unprofitable trade from $9,600 to $6,700 occurred at the time of the March 2020 COVID-19 crash. Such a black swan event can have an outsized influence on any trading system. This is another reason why itโ€™s worth going back further to see if this loss is an outlier or just a by-product of the strategy.

This is one example of a simple backtesting process. This strategy might have promise if we go back and test it with more data or include other technical indicators to potentially strengthen the signals it produces.

But what else can backtesting results show you?

  • Volatility measures: Your maximum upside and drawdown.

  • Exposure: The amount of capital you need to allocate from your entire portfolio to carry out the strategy.

  • Annualized return: The strategyโ€™s percentage return over the course of a year.

  • Win-loss ratio: How many of the trades in the system are likely to result in a win and how many in a loss.

  • Average fill price: The average price of your filled entries and exits when using the strategy.

Do bear in mind that these aforementioned examples do not constitute an exhaustive list. Which metrics youโ€™d like to track are completely up to you. In any case, the more details you include in your trading journal about relevant set-ups, the more opportunities youโ€™ll have to learn from the results. Some traders are very rigorous in their backtesting, which will likely be reflected in their results.

One last thing to consider is optimization. If youโ€™ve read our backtesting article, youโ€™ll know the difference between backtesting and forward-testing (or paper trading).ย 

Closing Thoughts

Weโ€™ve gone through the basic process of how to perform a manual backtest of a trading strategy. However, itโ€™s important to remember that past performance doesnโ€™t guarantee future performance.

Market environments change, and you must adapt to those changes if you want to improve your trading strategy. You should also be careful not to blindly trust the data. Common sense is a useful โ€” albeit often overlooked โ€” tool when it comes to evaluating results.

Further Reading

แƒžแƒแƒกแƒขแƒ”แƒ‘แƒ˜แƒก แƒ’แƒแƒ–แƒ˜แƒแƒ แƒ”แƒ‘แƒ
แƒ“แƒแƒ™แƒแƒ•แƒจแƒ˜แƒ แƒ”แƒ‘แƒฃแƒšแƒ˜ แƒกแƒขแƒแƒขแƒ˜แƒ”แƒ‘แƒ˜
Binance Beginner's Guide
A Beginner's Guide to Cryptocurrency Trading Strategies
A Beginnerโ€™s Guide to Day Trading Cryptocurrency
แƒ“แƒแƒแƒ แƒ”แƒ’แƒ˜แƒกแƒขแƒ แƒ˜แƒ แƒ”แƒ— แƒแƒœแƒ’แƒแƒ แƒ˜แƒจแƒ˜
แƒ—แƒฅแƒ•แƒ”แƒœแƒ˜ แƒชแƒแƒ“แƒœแƒ˜แƒก แƒžแƒ แƒแƒฅแƒขแƒ˜แƒ™แƒแƒจแƒ˜ แƒ’แƒแƒ›แƒแƒกแƒแƒงแƒ”แƒœแƒ”แƒ‘แƒšแƒแƒ“ แƒ’แƒแƒฎแƒกแƒ”แƒœแƒ˜แƒ— Binance-แƒ˜แƒก แƒแƒœแƒ’แƒแƒ แƒ˜แƒจแƒ˜ แƒ“แƒฆแƒ”แƒกแƒ•แƒ”.