This article offers a collection of resources for traders seeking introductory information on the subject of backtesting — a process that uses historical data to test the viability of trading strategies.
Backtesting is a technique that helps investors understand how well a particular investment model or trading strategy would have performed in the past. The underlying theory behind backtesting is that any trading strategy which has historically performed well may be likely to do so again in the future. Backtesting can also help identify and avoid strategies that have historically performed poorly.
Backtesting a trading strategy involves translating it into logic with clear entry and exit rules, and then simulating the strategy against historical data to see how well it would have performed in the past. This help traders determine whether a particular trading strategy is worth pursuing before risking any money on it in the market.
Backtesting is a complex process that traditionally has been practiced by a limited number of individuals and organizations. Why is this so? For years developer resources and expensive data sets of sufficient scope and detail were necessary for backtesting.
Today, web-based backtesting tools, such as Tradewell, allow for easy implementation without any coding knowledge. As a result, the popularity of backtesting has grown in recent years.
Backtesting is widely known as a tool traders employ to test strategies and improve returns. However, the practice of backtesting actually has relevance in areas of finance other than speculation. These areas include risk management, investment strategy development, and forensic accounting.
For example, portfolio managers apply use backtesting to determine ideal allocations and optimize rebalancing strategies. This type of backtesting typically suits investment approaches that are less active and have a longer time horizon.
Backtesting is also an important part of regulating financial institutions.
The process provides supervisory authorities such as the Basel Committee and bank regulators with a critical evaluation technique to verify the adequacy of internal VaR models used by subject banks, and to identify banks using models that may potentially underestimate risk.
The range of trading strategies employed by professional money managers is vast. One systematic approach that has been used for years and is also easy to backtest is called the simple moving average (SMA) crossover. This is a strategy where traders rely upon averages to determine when to buy or sell securities.
The simple moving average crossover technique triggers buys when the primary SMA crosses above the secondary. Likewise, a “sell” signal occurs whenever the primary SMA crosses below the secondary. The crossover of two averages can be used on any time frame, but traders often apply it to daily or weekly intervals.
We know that in order to backtest an SMA strategy, the trader must transform the strategy's buy and sell rules into code. The trader can then use this code to generate buy and sell signals for the security in question.
The code featured below executes a trading strategy that buys Microsoft shares when the 20-day SMA of the stock crosses above its 50-day SMA and sells the same security when the 20-day SMA crosses below its 50-day SMA. The trading strategy is implemented in python.
After converting a strategy into code, the next step is to test its performance by using past data to simulate how it would have performed in prior market periods. This allows traders to gauge how effective the strategy has been in the past and whether or not it is worth pursuing further. Testing a strategy's performance can also help traders identify any potential weaknesses so that they poor strategies can be abandoned before they are deployed into live markets.
As a final step, a trader will need to import a dataset of the price history of Microsoft stock. The example below shows how a trader can use python code to extract this historical data via an API provided by IEX Cloud.
A trader will naturally want to review the results of a backtest to assess the strategy’s suitability for trading real capital. The profit/loss ratio (P/L), compound annual growth rate (CAGR) and win rate are some of the standard metrics used to analyze a strategy’s performance.
To ensure a strategy is viable, it’s important to check that the majority of profitable trades are scattered across different time periods rather than closely clustered within shared windows of time. When the latter situation occurs, performance could be skewed and misrepresent the true viability of the strategy.
Retail traders who are new to backtesting often gravitate to platforms such as Metatrader or Pro Real Time. These platforms provide backtesting capabilities, as well as order execution, so traders can use them to automatically place trades directly from the algorithms they've tested.
The drawback with platforms like these is that traders need to write code fluently in order to operate the software, and therefore must overcome a learning curve before they can perform backtests. This limits the number of people who can use these platforms effectively.
Traders seeking tools that support a more rigorous backtesting process may find that standalone backtesting platforms such as Amibroker better meet their needs. This category of backtesting offers more in the way of data analysis than "hybrid" platforms offering a combination of backtesting and order execution or platforms whose core competency is charting.
Traders who operate unique strategies or who require uncommon datasets likely need a more flexible backtesting solution than the typical out-of-the-box platform. For these use cases, custom software, often written in R or Python, is usually the best option. While this approach is typically the most expensive, it is also the most versatile.
Although backtesting is a common practice in the financial industry, it still has its limitations. For example, backtesting models may fail to identify some of the factors that drive under-performance or over-performance of the forward price of a security. And there are times when datasets comprising those factors may be unobtainable.
Moreover, backtests can be vulnerable to unrealistic simulations of order executions. For example, in cases of high volatility and low liquidity, it is possible for traders to affect the price of an asset by simply buying or selling it. In situations like these, the price at which a trade settles in real life may be materially different from the price that the backtest predicts.
Overfitting is another common error. It happens when a set of trading rules is over-optimized on a single dataset, and ends up being useful only that specific historical data. Trading rules derived from backtests that are overfit rarely have any predictive power on future data series.
Start with the free version and then upgrade once you need to run backtests with longer lookback periods or against an expanded set of metrics.