Backtesting: How to Backtest, Analysis, Strategy, and More

Articles From: QuantInsti
Website: QuantInsti


Backtesting is considered to be an important tool in a trader’s toolbox. Without backtesting, traders wouldn’t even think of risking money into the financial markets.

Think about it, before you buy anything, be it a mobile phone or a car, you would want to check the history of the brand, its features etc. You check if it is worth your money. The same principle applies to trading, and backtesting helps you with it.

We will cover the following topics in this article.

  • Why is backtesting important?
  • What is backtesting?
  • Prerequisites for backtesting
  • How to do backtesting with Python?
  • Analyzing the performance of the trading strategy
  • Interpreting and analysing backtesting results
  • Backtesting vs Walk forward trading testing
  • Paper trading & Live trading
  • Common mistakes in backtesting
  • Backtesting software

Why is backtesting important?

Why is backtesting important?

Do you know the majority of the traders in the market lose money?

They lose money not because they lack understanding of the market. But simply because their trading decisions are not based on sound research and tested trading methods.

They make decisions based on emotions, suggestions from friends and take excessive risks in the hope to get rich quickly. If they remove emotions and instincts from the trading and backtest the ideas before trading, then the chance to trade profitability in the market is increased.

What is backtesting?

Backtesting a trading strategy is the process of testing a trading hypothesis/strategy on the historical data.

Let’s say you formed a hypothesis. This hypothsesis states that securities that have positive returns over the past one year are likely to give positive returns over the next one month.

  • How would you test this hypothesis?
  • How would you know whether the strategy will work in the market or not?

By using historical data, you can backtest and see whether your hypothesis is true or not. It helps assess the feasibility of a trading strategy by discovering how it performs on the historical data.

If you backtest your strategy on the historical data and it gives good returns, you will be confident to trade using it. If the strategy is performing poorly on the historical data, you will discard or re-evaluate the hypothesis.

Prerequisites for backtesting

Before you start backtesting a trading strategy, you need to consider some of the factors:

  • Trading logic
  • Market segment
  • Data
  • Programming language

Let us look at each of these factors in detail.

Trading logic/hypothesis for backtesting

You decided to backtest a trading strategy, but before you backtest, you need to have a clear picture in your mind of what you are going to backtest. That is what is the trading logic or hypothesis of this backtest.

In the above example, you calculate the past one year returns of securities and check whether the returns are positive or negative.

  • If positive, then you check for the future 1 month returns of the stocks.
  • If negative, you will do nothing.

If you are clear with the trading logic, then only you can backtest the trading strategy, and therefore this is the most crucial step in backtesting.

Choosing the right market or asset segment for backtesting

There are various factors that you can look at to decide which market or assets will be best for the kind of trading you are looking to conduct.

The factors can be risks you are willing to take, the profits you are looking to earn, and the time you will be investing, whether long-term or short-term.

For example, trading in cryptocurrencies might be riskier than other asset classes but can give higher returns and vice versa. Hence, it is a crucial decision to select the right market and asset class to trade-in.

Data for backtesting

Once you have shortlisted the assets, you would want to backtest your trading strategy. The next step is to choose historical data of the asset. You can get the data from the data vendor or from your broker.

It is important to select high-quality data, that is, data without any errors. If you choose poor quality data, then the output analysis from backtesting will be incorrect and misleading.

You can check out this free course on Quantra for getting the market data for different asset classes.

Choosing the programming language for backtesting

You were clear with the trading logic, selected the right asset for the trading and got the required data of the asset.

The final step is to decide the programming language which you will use to backtest a trading strategy. Actually, it is a matter of personal choice and the language you are comfortable with. Every programming language has its pros and cons.

Python – Python is a free open-source and cross-platform language. It has a rich library for almost every task imaginable and a specialised research environment. It is more suitable for medium to low frequency trading that is trading on a time scale of minutes and above. However, Python is not suitable for high-frequency trading.

C++ – C++, on the other hand, is suitable for high-frequency trading. It has ultimate execution speed. It offers the most flexibility for managing memory and optimising execution speed but can lead to subtle bugs and is difficult to learn.

MATLAB – MATLAB is another programming language with multiple numerical libraries for scientific computation. It boasts high execution speed but is still less appealing to retail trades as it is quite expensive.

R – R is a dedicated statistics scripting environment that is free, open-source, cross-platform, and contains a wealth of freely available statistical packages for extremely advanced analysis but lacks execution speed unless operations are vectorised.

Note: It is important to note that if you are not comfortable with any programming languages for backtesting, that’s not an issue. It doesn’t hinder you from backtesting your trading strategy. You can also start with Microsoft Excel.

For illustration, we will demonstrate how to backtest a trading strategy in Python in the next part of this article.

How to do backtesting with Python?

To learn how to use Python for backtesting a trading strategy, check out this highly recommended video on How to use Python for Trading and Investment. It introduces you to the basics of Python programming from a financial markets’ point of view.

Example of backtesting trading strategies

The strategy that we are going to backtest is based on the concept of moving average. Moving average is the average of the specified data field such as the price for a given set of consecutive periods.

As new data becomes available, the average of the data is computed by dropping the oldest value and adding the latest one.

The trading logic is very simple.

  • When the short-term moving average (50-day moving average) crosses above the long-term moving average (200-day crossover), we buy the security. This is also called a golden crossover.
  • When the short-term moving average crosses below the long-term average, we sell. This is called the death cross.

We will follow the below steps to backtest the above trading strategy.

Getting the price data for backtesting

We will do the backtesting on the Microsoft stock. To do that, you need to get the price data of Microsoft stock. We will use Yahoo! Finance to fetch the data.

import pandas as pd
import numpy as np
import yfinance as yf
import seaborn as sns
import matplotlib.pyplot as plt

data ='MSFT', '2015-01-01')
plt.title('Microsoft Stock Close Price')

Script: hosted with ❤ by GitHub

Data: Yahoo! Finance

Visit QuantInsti to read the full article:

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.

Disclosure: Digital Assets

Trading in digital assets, including cryptocurrencies, is especially risky and is only for individuals with a high risk tolerance and the financial ability to sustain losses. Eligibility to trade in digital asset products may vary based on jurisdiction.