Backtesting is an important tool in the tool set of financial traders. By using historical data, you can backtest and see if your assumptions are correct. It helps to evaluate the feasibility of trading strategies.
Backtesting is considered an important tool in the concentration of financial traders' tools. Think about it, before you buy anything, whether it's a phone or a car, do you need to check brand history, features, etc., and check if it's worth it. The same principle applies to transactions.
This article is divided into the following parts:
What is backtesting?
Key decisions for backtesting trading strategies
Backtesting platform
Selection of programming languages
Backtesting type
How to conduct backtesting?
Backtesting evaluation parameters
Backtesting deviation
What is backtesting?
Backtesting trading strategies is the process of testing trading assumptions/strategies based on historical data.
By using historical data, you can backtest and see if your assumptions are correct. It helps to evaluate the feasibility of trading strategies by discovering their performance on historical data, and then implementing them in the real market.
Key factors in backtesting trading strategies
Choose the correct market/asset segmentation
You can examine various factors to determine which market or asset is most suitable for the type of transaction you want to engage in. Whether in the long or short term, these factors may be the risks you are willing to take on, the profits you hope to achieve, and the time you are about to invest.
For example, cryptocurrency transactions may have greater risks than other transactions, but can provide higher returns, and vice versa. Therefore, choosing the appropriate market and asset class for trading is a crucial decision.
Once you have decided on the niche markets to invest in, you need to start trying to find some information about them.
Data covering various market conditions
The prices in the market are easily influenced by various factors, so they will fluctuate continuously according to different situations. These factors may include significant announcements such as monetary policy, the release of company annual reports, and inflation rates.
The key point to consider here is that the market does not always operate in a similar manner, which is why we need to test trading strategies under various market conditions so that we can understand the performance of the strategy under these conditions.
Include transaction costs
It is important to consider various commissions, spreads, and sliding points during backtesting. In the absence of these costs, the strategy is likely to perform well, but when these costs are included, it will greatly affect the profitability of the strategy.
Backtesting platform
Some platforms can provide the function of performing backtesting on historical data.
The key points to consider before selecting a backtesting platform are:
Understand which asset categories the platform supports
Understand the sources of market data it supports
Identify which programming languages can be used to write the trading strategy to be tested. We will provide a detailed introduction to various backtesting platforms later in this article.
Retail transaction backtesting platform
TradeStation
TradeStation provides electronic order execution across multiple asset categories. It allows trading from charts and real-time profit and loss P& L portfolio management for trading. TradeStationUse "EasyLanguage" to create charts and develop algorithmic trading strategies. As the name suggests, this language is easy to learn because it is very similar to English, making it very suitable for beginners in coding.
MetaTrader
The main product is a foreign exchange trading platform. These are custom scripts written in proprietary languages that can be used for automated transactions.
NinjaTrader
NinjaTrader is a freeware, which uses C # programming language and DotNet, which are widely used and rich in documentsFramework. It can be used for advanced charts, strategic backtesting, and simulated trading in the stock, futures, and foreign exchange markets.
Blueshift
Blueshift is a free and comprehensive trading and strategy development platform that also supports backtesting. It helps traders focus more on strategy formulation rather than code, and provides integrated high-quality minute level data. Its cloud based backtesting engine enables traders toDevelop, test, and analyze trading strategies in the Python programming environment.
Amibroker
Amibroker is a trading analysis software that can conduct portfolio backtesting and optimization, and has a series of good technical indicators to analyze strategies. It uses "AmiBrokerFormulaic Language (AFL) "to develop and implement trading strategies and indicators.
Network based platform
QuantConnect
Supports programming in multiple languages. Currently providing access to US stock and foreign exchange quotation data, and adding new databases. It supports high-speed backtesting and uses hundreds of servers in parallel.
Deltix
DeltixSupports stocks, options, futures, currencies, and custom synthesis tools. Provide an open and flexible architecture that allows seamless and robust integration with multiple data sources such as Bloomberg, Thomson/Reuters, etc. Widely used by quantitative funds, proprietary trading companies, etc. Due to software licensing fees exceeding their budget, not many retail traders are using this backtesting software.
Quantum
Like Deltix, QuantumDue to the high licensing fees, it is mainly used by institutions. It provides an integrated solution for data collection, strategy development, historical backtesting, and real-time execution across tools and portfolio trading parties.
AlgoTrader
This is a company headquartered in Switzerland, based on the WebThe front-end system provides open source and commercial licensing. Supports foreign exchange, options, futures, stocks, ETFs, commodities, synthetic tools, and custom derivative spreads, allowing for complete historical backtesting and even complex event processing.
Selection of programming languages
It plays an important role in developing backtesting platforms. Different languages have different advantages and disadvantages, and careful weighing as needed can greatly improve the performance of the system.
We briefly discuss some of the most commonly used languages below:
Python
Python is a free open source cross platform language. It provides a rich library and a specialized research environment for almost all imaginable tasks. The execution speed is more than enough for Day traders, and trading is based on a time frame of minutes or more.
C++
It provides maximum flexibility for managing memory and optimizing execution speed, but may generate minor bugs and be difficult to learn.
C # and Java
Both are good choices for development backtesting, as they have native GUI functionality, numerical analysis libraries, and provide fast execution speed.
MATLAB
MATLAB is a commercial IDE with multiple numerical libraries for scientific calculations. It has fast execution speed, but its appeal to retail traders is still low because it is quite expensive.
R
RIt is a dedicated statistical scripting environment that is free, open source, cross platform, and contains a large number of free and available statistical packages for performing extremely advanced analysis, but lacks execution speed unless the operations are vectorized.
Backtesting type
There are two ways to backtest the model: vectorization or event driven. Let's understand these two methods through an example.
The vectorization method is a rough method for quickly testing strategies. Establish a quantitative backtesting framework based on research, without considering the commission and transaction behavior of transactions, which is significantly different from the actual market situation.
Compared with vectorization methods, event driven systems have many advantages. Firstly, event driven backtesting can be used for historical backtesting and real-time trading, while vectorized backtesting must obtain all data at once for statistical analysis; Secondly, using event driven backtesting will not result in forward-looking bias, as market data reception is treated as an "event" and can be used to replicate the behavior of order management and investment portfolio systems through "drip irrigation" of market data; Thirdly, event driven backtesting allows customization of how orders are executed and transaction costs are incurred. Due to the ability to build custom transaction handlers, it is easy to process basic market and limit orders.
Although event driven backtesting systems have many advantages, compared to simple vectorization systems, two major drawbacks are also prominent: firstly, implementation and testing are much more complex, with more "activity components" (modules), resulting in a greater chance of introducing errors; The second reason is that the execution speed is slow, making it difficult to utilize the optimal vectorization operation when performing mathematical calculations.
How to conduct backtesting?
Vectorization Backtesting in Python
In this section of this article, we will demonstrate how to backtest trading strategies based on moving averages in Python.
We will use a simple Moving Average, which is calculated by adding the prices of the last n days and dividing by the number of days.
The trading rules assume that when the short-term moving average (50 day moving average) exceeds the long-term moving average (200 day moving average), we buy.
When the short-term moving average is lower than the long-term average, we sell.
The stEPS to retest this strategy are:
Obtain price data
Calculate moving average
Generate transaction signals
Draw a profit curve
Obtain price data
We obtain daily stock price data from Yahoo Finance.
Microsoft closing price
Calculate moving average
We will calculate the 50 day and 200 day moving averages of the closing price. We will use the Panda Rolling and Mean method to calculate the moving average.
Generate transaction signals
As mentioned earlier, we will buy when the 50 day moving average is greater than the 200 day moving average, and short when the 50 day moving average is lower than the 50 day moving average.
Long and short signals
Draw a profit curve
We will calculate and plot the cumulative return of the strategy.
Backtesting evaluation parameters
We have conducted backtesting, and now it is time to measure its performance based on certain parameters.
Based on the backtesting results on Blueshift,
Let's try to understand some indicators.
Annualized return
It is the return on an investment over a period of over a year.
Annualized return=(1+cumulative return) ^ (252/trading days) -1
Accumulated return
Cumulative return is the total return on investment.
Accumulated income=(final initial value)/initial value) * 100
Annualized volatility
Volatility is a statistical measure of the dispersion of returns in a given market index or investment portfolio.
Annualized volatility=daily volatility * square root of 252
Sharpe ratio
The Sharpe ratio is the excess return per unit of volatility. Excess returns are total returns minus risk-free returns. Generally speaking, risk-free return refers to the return on risk-free assets, such as government bonds.
Sharpe ratio=excess return/volatility
A higher Sharpe ratio is preferred because the return per unit of risk is higher.
Maximum fallback
It is the maximum loss of the investment portfolio from peak periods. This is the maximum loss that this strategy can cause.
Maximum retreat=(valley peak)/peak
Backtesting deviation
A good retester should be aware that there may be biases that may completely change your retest results.
Now let's take a look at some of them.
The past is the measure of future predictions
Retrospective testing assumes that past performance is a measure of future performance. For example, suppose you want to test a strategy based on the concept that internet IPOs perform better than the overall market.
If you want to test this strategy during the internet boom of the late 1990s, its performance will be significantly better than the market. However, trying the same strategy after the foam burst would lead to poor returns.
When backtesting trading strategies, it is necessary to consider that 'past performance may not necessarily guarantee future returns'.
Excessive fitting/optimization bias
Like any other model, backtesting is prone to overfitting. When testing the model on historical data, you unintentionally try to use fitting parameters to obtain the best results. However, you have achieved the best results on historical datasets, and when you deploy the same model on an unknown dataset, it may not be able to provide the same results.
How to avoid overfitting during back testing?
The best way to avoid overfitting is to divide the dataset into training and testing datasets (similar to machine learning). You test the trading strategy on the training dataset and run the strategy on the test dataset using the same parameters you used on the training dataset to ensure its effectiveness.
Forward looking bias
Forward looking bias is the information used in analysis before it actually occurs. When formulating a strategy, you can access the entire data, so in some cases, it may include future data that cannot be used during the testing period.
A seemingly insignificant oversight, such as assuming that the earnings report was available the previous day, may lead to deviations in the results during the backtesting period. You need to ensure that data that is only available in the future is not used to avoid forward-looking bias.
Survivorship bias
During the backtesting of a strategy, you usually tend to backtest the strategy in the current scope rather than in the historical database.
There is a famous example of survivorship bias. If you use the stocks of technology companies to make strategies, but obtain data after the Internet foam burst, it is quite different from the situation that you included data before the foam burst.
How to avoid survivorship bias in the backtest?
One way is to purchase expensive data without survivorship bias. Another method is to use only the most recent data from no more than 5 years (not a very reliable method)
FAQ
Q: How to explain the backtesting results?
There are multiple performance parameters available for evaluating and interpreting the backtesting results. Some performance indicators are Sharpe ratio, rollback, annualized return and volatility.
Q: How to distinguish between good and bad backtesting results?
It is necessary to test whether the backtesting results are good or bad. One method is to divide the dataset into training and testing datasets. In the training dataset, you can create and optimize policy parameters. On the test dataset, you can analyze performance.
If the strategy performs well on test data, it indicates that it is good. If the performance is not optimal, it may be that you overfitted the parameters and need to re evaluate the model.
Q: What should be the timeframe for the backtesting?
The time period for retesting depends on the average holding time of your position. If the holding period of the trading strategy exceeds one month, it is best to use a longer period of time, preferably 15 years.
If you are creating an intraday strategy, then 10 years is a great time. However, when you start trading, this runs well 10The strategy of the year may no longer be effective. Therefore, it is crucial to test your strategy on an out of sample dataset.
last
Backtesting has proved to be one of the biggest advantages of algorithmic trading, because it allows us to test our strategies in the real market before actual implementation.
Procter & Gamble leads in consumer goods with diverse brands and innovation. Its stock has seen a 1.673% increase since 1990. showing stable growth.
2024-09-06The capital adequacy ratio measures a bank's financial health and risk tolerance. A high ratio boosts stability, but too high may reduce efficiency.
2024-09-06Johnson & Johnson leads in healthcare with strong finances. The stock is near fair value, offering a good entry point despite market risks.
2024-08-30