Excerpt
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 = yf.download('MSFT', '2015-01-01') data['Close'].plot(figsize=(10,7)) plt.title('Microsoft Stock Close Price') plt.ylabel('Price') plt.show()
Script: libraries_moving_average.py hosted with ❤ by GitHub
Visit QuantInsti to read the full article: https://blog.quantinsti.com/backtesting/.
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation 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 its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.
Disclosure: Bitcoin Futures
TRADING IN BITCOIN FUTURES IS ESPECIALLY RISKY AND IS ONLY FOR CLIENTS WITH A HIGH RISK TOLERANCE AND THE FINANCIAL ABILITY TO SUSTAIN LOSSES. More information about the risk of trading Bitcoin products can be found on the IBKR website. If you're new to bitcoin, or futures in general, see Introduction to Bitcoin Futures.