How To Backtest A Trading Strategy

Last Updated on November 24, 2022 by Oddmund Groette

In your day-to-day life, before you buy anything — a mobile phone, a house, or a car — you would want to check its features and history to know if it is worth your money. That also applies to trading, but here, what you check are your trading strategies. With a wide range of markets to trade, you need to backtest your strategies to be sure they work in the different markets. But how do you backtest a trading strategy?

A backtest is a way of testing a trading strategy on historical data to assess its performance before committing real funds to it. The steps involved include defining the criteria of the strategy, selecting the market and timeframe to test it on, loading up the historical data, writing the code and implementing the backtesting, and finally, evaluating the results.

In this post, we take a look at how to backtest a trading strategy.

Backtesting course

Before you continue reading, we remind you that we have an online backtesting course that goes much deeper than this article. It consists of many lessons and the course is a summary of our knowledge and experience after more than 20 years of (successful) trading and investing solely by using quantified trading strategies based on backtesting (please also read our trading lessons from trading the markets). For a full curriculum please click on the banner:

What is a backtest?

A backtest is a way of testing a trading strategy on historical data. It is a way to simulate the historical performance of a trading strategy using historical data before committing real funds to the strategy on live trading. Backtesting is based on the assumption that if the strategy performed well in a particular market previously, it has a good chance of working again, and on the flip side, if the strategy did not perform well in the past for that market, it may not work well in the future.

So, let’s say you have a swing trading strategy that says that if the S&P 500 Index has a positive return in the past month, it will give a positive return over the next week. Now, you want to test that theory to know if true. While you can forward-test it by using a demo account and waiting for the positive months to come so you check how the market performs in the week that follows each of them, that would be very time consuming.

A faster way to test your theory is to test it on historical price data. You go into the past to find all the times the market has a positive monthly return and then check how the market performed in the week that followed. If the performance on historical data is good, you can assume that the strategy would perform well in the current market, but if the performance is poor, you may discard the strategy or tweak and re-test it. You can, of course, backtest any time period you like, the point is to measure how your predictions on past data work on future unknown data.

The best strategies can be found in our….

Strategy Shop

Backtested trading strategies

Thus, backtesting is a very important step in creating a trading system. It can help a trader test, optimize, and improve their strategies, thereby giving them the confidence to apply the strategy in live trading.

Why do you backtest?

There are many reasons to backtest a strategy, and these are some of them:

  • To know if you have a trading edge in the market (what is a statistical trading edge?): Many traders, even experienced traders lose money, and they don’t lose money because they don’t know how the market works. But simply because their trading strategies don’t have any statistical edge in the market they are trading. No matter how you master trading discipline, control your emotions, and manage risk, you cannot make money from a market unless your strategy has an edge in that market. If you want, risk only 0.1% of your account per trade, without an edge, the account would slowly bleed out. The only way to know if your strategy has an edge in a market is by backtesting it. The likelihood of trading profitably in the market is boosted if you backtest your strategy before trading. Emotional control and risk management only help a profitable strategy to make money — they can’t do anything if the strategy has no positive expectancy.
  • To improve a strategy: Another reason to backtest a strategy is to see how you can tweak it to improve its performance. This involves altering some variables or adding new variables to see how they improve performance. The process is sometimes called optimization. Too much of it can lead to curve-fitting and dimmish the strategy’s robustness.
  • Check for correlation in trading: for those more advanced, you might want to backtest a portfolio of trading strategies. You see, even if you have 20 very good trading strategies, they might not work well together if they have many overlapping trades.

How do you do a backtest?

These are the steps to follow when doing a backtest:

  1. Define the logic of the strategy to backtest: The first step is to define the strategy you want to backtest and clearly state the criteria for each action with specific trading rules. The logic you want to backtest should be clear. For example, if your strategy is a moving average crossover system, you clearly state the type of moving average, the period of the two moving averages, and the entry and exit criteria. The entry and exit logic could be (as an example):
  • When the 20-day SMA crosses above the 100-day SMA, exit any short position and enter a long position
  • When the 20-day SMA crosses below the 100-day SMA, exit any long position and enter a short position
  1. Select the market(s) for the backtesting: At this stage, you specify the market you want to test the strategy on, as well as the time frame. The market you backtest must be the one you want to trade the strategy. You don’t use a strategy backtested on the silver market to trade the gold market, even though both are precious metals. Note that you can backtest the strategy on different markets and on different time frames and choose the ones with the best performance, but beware of curve fitting.
  2. Get the data for backtesting: You will need historical data of the market you want to backtest. While some trading platforms offer some data, they may be limited. You may have to pay to get enough historical data for your backtesting. You have to divide your data into two — in-sample data for backtesting and out-of-sample data for optimization. However, be careful about the data you backtest on. Your backtest is only as good as the quality of your data! There are plenty of bad data out there. Please read our article on the importance of good data sets in trading.
  3. Implement your backtesting: You may have to learn a programming language like Python, but some trading platforms like TradeStation and Amibroker use languages that are easy to use.
  4. Evaluate your results: After the backtesting, you need to evaluate your result to know how the strategy performed. Here are some of the performance metric statistics used in trading:
  • The total number of trades: This tells you how many trades were taken during the period under study. The higher the number of trades, the more reliable the result.
  • Holding time: This is the length of time that the strategy has open positions in the market. The lower the holding time, the better.
  • Profit factor: This is the total profit divided by the total loss for the entire trading period. It indicates the amount of profit per unit of risk. A profit factor of more than 1 indicates a profitable strategy, but the higher the better. Note that on some platforms, this is presented as “Average size of winner/loser”.
  • Maximum drawdown: This is the difference between the higher amount and lowest amount the trading capital got to during the backtesting. The lower the maximum drawdown, the better the strategy.

Where can you backtest a trading strategy?

You backtest a trading strategy on a trading platform. Most trading platforms have a strategy tester section where you can backtest your strategy, but not all of them are free to use. As a retail trader, you may want a free platform for backtesting, and there is some free software available in the market for backtesting a trading strategy. Some of the free backtesting software are Microsoft Excel, TradingView, NinjaTrader, Trade Station, Trade Brains, etc.

But the paid platforms may offer more features. One of the most commonly used paid platforms is Amibroker. The analysis window of the Amibroker platform allows you to back-test your trading strategy on historical data. At about 450 USD for a lifetime license, the platform is pretty cheap, and it offers full customization features for backtesting and some lightning-fast optimization features. It is easy to test strategies on a portfolio level with the platform.

Practically all of the backtest done on this website are done by Amibroker. We are extremely pleased and we also use it for live trading by adding just a little extra code. Considering the cheap and its features, we believe Amibroker is a good option for most retail traders. Based on our experiences, we have created an online Amibroker course that consists of several modules (basic, live trading, and trading strategies):

Other interesting and relevant articles about backtesting platforms are found in our trading platform guide.

How long should a trading strategy be backtested?

It depends on a lot of factors. It is easy to say backtest for a period of one year or two, but statistically, it is not a question of duration but that of sample size. That said, we recommend including several types of markets, like bear and bull markets. They behave differently.

When backtesting, the key thing is that you want to have enough sample size (the number of trades) that can be representative of the population (in this case, the market).

So, in terms of duration, you would need as much time as can give you enough trades to have a statistically reliable result. While a sample of 250 trades may be sufficient, the bigger the sample size is, the smaller the margin of error, and the more reliable the result. If your trading system generates enough trades, a sample of 500 – 750 trades is good.

The number of trades you get would depend on the strategy and how many trades it generates per day, week, or month. For a day trading strategy that generates a trade per day, it means you get about 250 trades per year, so backtesting over a 2-3 year duration should be able to give you enough data to make reliable assumptions.

On the other hand, a swing trading strategy that generates only four trades per month would get you only about 48 trades per year. So, to get up to 500 trades sample that would ensure reliable assumption, you will have to backtest over a period of more than 10 years.

How do you backtest a trading strategy in Excel?

When backtesting a trading strategy in Excel, here are the steps to take:

  • Get your data into Excel: There are two basic ways to do this: First option is to manually download historical data from Yahoo Finance directly as CSV and then load it into Excel. This works, but you’ll need to re-download that historical data and then copy and paste either the entire dataset or a subset to update your strategy. The other option is to use code to go grab data automatically from Yahoo Finance. You can search on Google for some VBA codes you can use. Some third-party tools, like AnalyzerXL, can make the job simple.
  • Create your indicator: Now that you have got your data into Excel, you use them to construct an indicator or indicators for your strategy. You can use different columns to create different indicators or different aspects of an indicator, with each one taking part of the calculation. You can use other add-ons or third-party tools for more functionality.
  • Formulate your trading rules: After creating your indicator, you need to formulate your trading rules. You create the calculation in a new column you name the “Signal” column. Your trading rule can be as simple as going long if the calculation is above a certain level and short if below it. But you could also have more complex rules with criteria for when to vary the position size or when to stay out of the market.
  • Get the equity curve: You can get the equity curve with the Excel function, but if you need other trading stats like CAGR and the Sharpe ratio to compare the strategy with other strategies, you will have to do a bit of work or use a third-party tool like AnalyzerXL.

We used Excel as our main backtesting tool all the way up to 2017. Excel is a lot better than most traders imagine! You don’t need any fancy tools to backtest, the main asset is, after all, you, who put in the trading rules. As a matter of fact, Excel can be a very useful tool because you in most cases need to test a strategy on one instrument at a time. This way, you see small details you otherwise wouldn’t!

We even used Excel for live trading. We had a VBA script that we used for both sending orders, but also when we closed our positions. It worked really well, but it’s not as dynamic as dedicated software platforms (like Amibroker, Tradestation, etc.).

Do professional traders backtest?

Yes, professional traders always backtest their strategies before deploying them. They know how important backtesting is to the profitability of a trading strategy and cannot afford to make the mistake of trading a strategy that is not backtested. Some even go ahead to forward-test their strategies — if they are intraday trading strategies that can generate enough trades within a short period — before committing real money to them.

Even professional traders who use discretionary trading methods still backtest their strategies. They do so manually by either going back in time to check the occasions where their trade setups occurred and how the market reacted. All serious traders keep a trading journal. Alternatively, they can use strategy tester software that prints historical data as though they are in real-time and then trade their setups as they occur.

Can you backtest for free?

Yes, there are some trading platforms that allow you to backtest your strategy for free, but you may have to look for historical data from market data vendors to get enough data. Some of the free backtesting platforms include TradingView, NinjaTrader, TradeStation, and so on.

Alternatively, you can use free software like Microsoft Excel, which enables you to backtest with the Excel function. However, you have to source your data elsewhere. A common source of data is Yahoo Finance, where you can either download the data manually or write a code that can do that. However, if using free data please be careful about bad data.

Backtesting examples

Let’s say you have a swing trading strategy that says that if the S&P 500 Index has a positive return in the past month, it will give a positive return over the next week. You can backtest it easily to find out if it makes money. In this case, your market is the S&P 500 Index — you can test it on e-mini S&P 500 futures or the SPY ETF (SPY ETF trading).

Your strategy rules are:

  • Go long on the next day’s open if the monthly bar closes positively
  • Exit at the close of the trading week

You can get the market data of more than 30 years since it is a monthly signal. After the backtesting, you evaluate the performance using stats like profit factor, Sharpe ratio, maximum drawdown, and so on. If the performance on historical data is good, you can assume that the strategy would perform well in the current market, but if the performance is poor, you may discard the strategy or tweak and retest it.

Backtesting software

This refers to a platform you need to backtest a strategy. It can be a stand-alone platform or a trading platform. Most trading platforms have a strategy tester or analysis window where you can backtest your strategy, but not all of them are free to use. Some of the free backtesting software are Microsoft Excel, TradingView, NinjaTrader, Trade Station, Trade Brains, etc.

How to backtest a strategy in Python

Python is a free open-source and cross-platform language with a rich library for almost every imaginable task, including automated trading. Thus, it is an excellent choice for backtesting a trading strategy.

If you want to backtest a strategy with Python, here are the steps to follow.

  1. Import the necessary libraries for backtesting
  2. Download the needed market data
  3. Calculate daily returns
  4. Create strategy-based data columns
  5. Create strategy indicators
  6. Create signals and positions
  7. Implement the backtesting
  8. Analyze results

We have written about Python trading strategy in a previous article.

How many trades do you need to backtest?

In most cases, a sample of 200-250 trades may be sufficient to make a reliable assumption about a market, but the bigger the sample size is, the smaller the margin of error, and of course, the more reliable the result.

However, if your trading system generates enough trades, a sample of 500 – 750 trades will even get a more reliable result. But again, please make sure you test in different markets (both bull and bear).

How do you backtest trading strategies without coding?

To backtest without coding, you have to use a code-free trading software. Such a platform allows you to create codes with a simple drag-and-drop interface. Examples of code-free platforms include TradeStation, Amibroker, MetaTrader 5, TradingView, QuantShare, and Forex Tester.

How to backtest a trading strategy – an example

Let’s end the article and make a specific backtest with trading rules and settings. The strategy is called the Turnaround Tuesday strategy and is one of the most well-known strategies there is, yet it’s still working pretty well.

We make the following trading rules:

  1. Today is Monday.
  2. The close must be at least 1% lower than Friday’s close.
  3. If one and two are true, then enter at the close.
  4. Exit at the close on Tuesday.

This is an overnight trading strategy that owns S&P 500 (SPY) for 24 hours.

This is what the equity curve looks like:

How to backtest a trading strategy

The 171 trades have an average gain of 0.67%, something we consider excellent considering you only hold the position for 24 hours! Even during bear markets, it has worked pretty well.

We did this backtest in less than two minutes by using Amibroker. This shows the power of validating or scrapping your trading idea by backtesting. This is why backtesting is so powerful!

Amibroker code

We have made an inexpensive library from our list of free trading strategies where you can get the Amibroker code for all those strategies. Please read more by clicking on the green banner:

How to backtest a trading strategy – conclusion

Because emotions run deep in any human being and we tend to jump to conclusions, we recommend backtesting. You can validate your ideas quickly (or falsify them). Backtesting is powerful, and actually pretty easy if you practice.

You don’t need any fancy tools to backtest. Start with a spreadsheet, and if you like it, go on to something more sophisticated like Amibroker, Tradestation, etc. From there you need to formulate trading ideas, backtest, test on known and unknown data, validate and falsify, and make conclusions. It’s as simple as that – that is how to backtest a trading strategy.

Similar Posts