What is Backtesting?

2 min read

backtesting

Backtesting is the process of testing a trading or investment strategy using data from the past to see how it would have performed.

Understanding backtesting

Running a backtest

The general idea of a backtest is to run through stock prices in the past, usually with software, and hypothetically firing trades based on a certain trading strategy.

For example, let’s say your trading strategy is to buy Amazon when it falls 3% in a day, your backtest software will check Amazon’s prices in the past and fire a trade when it fell 3% in a day.

The backtest results will show if the trades were profitable.

Backtesting can be as simple as running analysis in Excel to something more complex such as creating custom backtesting software. It all comes down to your individual requirements.

The Aim of Backtesting

Backtesting accomplishes 3 things:

  1. Shows if a strategy performs well in periods when it is supposed to, and vice versa
  2. Provides an understanding of how the strategy performs in different markets.
  3. Produces insights on how the strategy might be improved on.

1. Performance during selected periods

With a backtest, we can check to see if a strategy makes money when it is supposed to and loses money when it is supposed to.

For instance, let’s say that our strategy is expected to perform better when the markets are volatile, or in other words, when they move much more than they normally do.

If our backtests then show that we make more money than expected during less volatile periods, this is a red flag (even though we made money).

We need to examine our strategy and figure out why.

2. Understand how the strategy performs in different markets

To gain more confidence over how consistently a trading strategy will perform, backtests can be run in different market environments.

This means running backtests with different stocks or other market assets.

It could also mean performing tests during periods where there are clear trends and comparing them to periods where there weren’t.

3. Improving the strategy

This involves making changes to a strategy after looking through the results of the backtest.

A common pitfall here is to continuously tweak the strategy so that it shows better results in a backtest.

This approach rarely leads to profitability when you trade it with real money and is known as overfitting.

Why is backtesting important to you?

Backtesting is an essential part of developing a trading strategy.

Pass, improve, or fail

A backtest can help decide if a strategy is suitable to trade real money, can use improvement, or if it’s best to give up on it.

Deploying a strategy

As mentioned, backtesting helps us understand how our strategy performs in different market environments, this will allow us to deploy our strategy better.

A trader might have multiple strategies. By knowing the strength and weaknesses of each of the strategies, it will be clear when is it best to deploy a certain strategy.

Certain strategies pair well with others. Some strategies don’t work well when market conditions are unfavorable. Running backtests will provide the information needed to decide when to deploy which.

Examples of Backtests

There are several tools available to help you conduct a backtest. 

A common way is to use a trading platform. Popular trading platforms that include backtesting capabilities include MetaTrader4, Tradingview, ThinkorSwim, and Ninjatrader.

The benefit of using such a platform is that most of them include the necessary data. Several of them also have built-in analysis.

Performance summary of a backtest in Tradingview

As an alternative to using a solution tied to a trading platform, there are several coding libraries that can help in backtesting.

For those familiar with Python, Backtrader and Zipline are both great options.

These libraries can accommodate a lot of customization. The trade-off is that the learning curve is a bit steeper.

Returns chart created in PyFolio

Analysis tools usually come with backtesting software. There are also third-party solutions available such as Pyfolio. This Python library simplifies creating charts and calculating statistics.

Backtesting software is not a necessity. The above image shows a chart that tests a hypothesis that gold prices rally for a few days after a Fed meeting

All it took was a simple charting library and some historical data.

What is Modern Portfolio Theory?

Modern Portfolio Theory (MPT) is a method to select which stocks and what amounts to buy such that as a group, these stocks give...
Lucas Liew
2 min read

What is a Hedge Fund?

Lucas Liew
2 min read

Turtle Trading

Definition Turtle trading refers to the way a group of traders in the 1980s traded. This group, who is known as the turtle traders,...
Lucas Liew
1 min read