Earn2Trade Blog
backtest, backtesting

Backtesting in Trading – A guide on how to Backtest a Trading Strategy

Trading without proper backtesting is like playing the casino. You may win a couple of times, but you will suffer significant losses in the long term. The reason is that, before you go out there and start risking your capital, the most important thing is to fine-tune your trading strategy. It needs to provide relatively stable performance in various market scenarios. This guide will help you learn how to backtest a trading strategy, which backtesting method works best, how to evaluate the results of your tests, and most importantly – when to consider you are ready to go live and trade with confidence.

new ad e2t en

What is Backtesting?

Backtesting is a method of analyzing your current trading strategy’s performance during a time-frame within the past. Backtesting a trading strategy helps you assess its behavior during post-factum market scenarios and determine where it stands out and where it falls short. It is a vital tool to help you validate a trading model ex-post.

An example of backtesting is if you go back in time and check out how your trading strategy would have performed during the peak of the Global Financial Crisis or at the start of the COVID pandemic.

Backtesting aims to help generate results and evaluate risk and profitability without risking any actual capital. Think of it as swimming with a life vest – you taste the water, but you can’t drown.

Backtesting for traders is what training is for professional athletes. They spend hours refining their technique before going out and competing with the rest. That way, they become better and build confidence, based on numerous tests, data, and analysis.

The Basics

There are several different software we can use for backtesting. The options include Microsoft Excel, ready-made third-party platforms, or building one from scratch. The leading algorithm trading firms program their backtesting software in different computer languages. These can include C++, C#, Python, or R (for less complicated projects).

To perform proper backtesting, you need historical data. The program takes your strategy’s specifications and applies them over a particular market period in the past to show you how it would have behaved back then.

Depending on the backtest results, the trader or the analyst will decide whether the strategy needs some fine-tuning or if it is good enough to be applied as is.

The idea of backtesting rests on the theory that financial markets run in cycles. If something was viable in the past, many traders assume it will stay relevant going forward. And vice-versa – if it failed in the past, it would probably not succeed in the future either.

Why Do You Need to Backtest Your Trading Strategy?

The two main pillars for building a trading or investment strategy are risk and return and their relationship. Backtesting helps you quantify those two factors to show your strategy’s overall profitability and risk appetite.

You need to backtest your trading strategy to be aware of how it will perform under real market scenarios. Backtesting allows you to simulate your trading idea using historical data and put its risk management mechanisms to the test.

Backtesting a trading strategy can help you find its weak spots, test its resilience, and highlight where you need to fine-tune it without any risk involved. By doing all this on the blackboard, you can clear out all issues, strengthen your risk management tools, and gain confidence that your trading strategy is sound enough. Doing so will ensure a more satisfying performance when implemented in real market scenarios.

What does Backtesting tell you?

Essentially, backtesting will give you crucial answers to essential questions like:

  • What is the best trading setup for your needs and goals?
  • What is the optimal risk per trade?
  • In which markets does this strategy work best?
  • Are your entry and exit triggers well-adjusted?

Another fundamental reason why you should backtest your trading strategy is that markets today are run by data. Historical data, forward-looking indicators, predictive analysis models – all these can help you build a sound trading strategy. Without incorporating real market data, you simply can’t get an accurate indication of your trading strategy’s future performance and whether it is viable under actual market conditions.

By backtesting your trading strategy, you can find how it would have performed in the past. If it performed poorly, the chance it will be successful in the future is minimal. And vice-versa.

Backtesting a trading strategy can give you a competitive advantage. It equips you with actionable insights on what you can expect when you go live to compete with other traders.  

What Do You Need Before You Backtest it?

The most important thing is to try and find unbiased data to avoid skewing your model’s performance. If you use biased data, it will almost inevitably skew the results of the test.

Although completely staying away from biases isn’t possible, you should make sure to mitigate their effect to get as transparent and reliable results as much as possible. There are several types of biases that can affect your data and, consequently, your model’s performance.

Optimization Bias

First, the optimization bias (a.k.a. curve-fitting) describes situations where traders introduce additional parameters and win trades until their strategy’s performance satisfies their expectations. Alternatively – “covering the cracks” of the system and artificially inflating the results. However, the only thing this will achieve is to deceive you and lead to unexpectedly poor performance when you go live.

Look-Ahead Bias

There is also the look-ahead bias where you accidentally include future dates in the simulation. This error can happen due to improper parameter calculation, a technical bug (mostly when writing the backtesting script from scratch), and more. To avoid such situations, always make sure to double-check your data and backtesting methodology before you go live. Otherwise, the strategy might underperform in real trading.

Other Biases

There are also other types of biases. One of them is the survivorship bias. This bias develops when backtesting strategies on datasets that fail to represent the full range of relevant assets you are interested in trading. Another one is the psychological tolerance bias. This one occurs when backtesting on long-term periods to improve the performance, but in reality, you plan to trade on short-term ones).

After you make sure your data and backtesting methodology are bias-free (as much as possible), it is time to focus on choosing a backtesting software. If you are trading through a particular broker, the chance is they will have a built-in backtesting feature in their platforms. At least the most popular ones have. In this case, the benefit is that you will be using a tested solution that is user-friendly and proven to work. It will also help you with one crucial issue that traders often underestimate – incorporating the trading costs into the backtesting model. Even if they are insignificant, when they pile up throughout trading in the long-term, it will affect your strategy’s profitability.

There are also ready-made backtesting platforms you can subscribe to. However, bear in mind that backtesting usually is an ongoing process. You should backtest your strategy every once in a while or if you plan to widen your portfolio, trade alternative assets, etc. Doing so means you will have to set aside a specific budget to pay for your backtesting software regularly.

Those with technical skills can write a backtesting script from scratch in R, Python, or even use Excel. You can also hire a programmer to turn your strategy into code.

After finding the perfect backtesting software, it’s time to roll your sleeves up and get to work.

Choosing A Strategy to Backtest

There is no restriction on which exact strategy you will backtest. Most traders have several trading strategies, depending on the market situation (downtrend/uptrend), type of asset, risk/profit potential, and more. Understandably, you should make sure to test all your strategies and evaluate their performance.

Make sure to backtest your strategy right before you apply it in the real world. If, for example, a trading strategy showed excellent performance during the bear market in Q1 last year, it might underperform in the current year’s bull market. The key here is to contextualize information.

Also, it is essential to backtest the trading models over a variety of market conditions. While the market never moves precisely in the same manner. In most cases, the trading assets show similar patterns to the ones before.

The more scenarios you backtest, the more representative and trustworthy the results will be.

Choosing An Asset to Backtest

The best-case scenario is to backtest your strategy on data for the same instrument you plan to trade with real money. For example, if you plan on applying your method to trade Soybean futures (ZS), make sure to download historical data from CME or another service provider and run your model over it.

That way, you will be sure that the results will consider only asset-specific factors. Factors like seasonality, volatility, supply and demand, external risks (i.e., harsh weather conditions in the biggest soybean producers region), etc.

Make sure to always backtest your strategy with the exact asset you intend to apply it on. If that isn’t possible (you can’t get historical data, for example), find reasonably similar assets that accurately mimic the original asset’s behavior. In that case, you might need to make small adjustments to your backtesting model to make sure the results are viable.

If you plan to trade a set of stocks, make sure to collect a representative sample. The data should include even shares of companies that have gone bankrupt over the particular period. Don’t exclude those as you risk skewing the performance of your strategy. For example, if you cherry-pick stocks of all present companies today, you will produce artificially high returns when backtesting your system.

How to Backtest a Trading Strategy

The principles of backtesting trading strategies are fundamentally similar, no matter which platform you use.

Step 1

As a first step, you have to feed the backtesting algorithm with the carefully-sourced historical data. When testing a trading strategy on historical data, you need to specify a concrete period for your training set (e.g., AAPL stock’s price in the period 2020 – 2021). Then you need another set of data for an alternative period. The reason for testing a strategy over different periods is to validate its reliability and mitigate the role “randomness” plays in the whole process.

Step 2

Next, you have to set-up some parameters, depending on how complicated your backtesting model is. These may include initial capital, capital at risk (%), portfolio size, commission fees, average bid-ask spread, and most importantly – a benchmark (usually the S&P 500).

You should also set-up the specific parameters of your trading strategy. These include stop loss and trailing stop loss instructions, take profit level, when to close a position, type of the preferred positions, and more.

Step 3

Next, you run the backtest over your testing data sets. All of the information mentioned above will be used to simulate trades over a specified period.

After you conclude the backtest, you should rerun the process (at least a couple of times) over another set of data. This will help make sure you cut off any potential bias and the role of the “random” factor.

Most backtesting software also support automated strategy optimization features. This feature is handy. The computer can figure out what input (or information combination) your strategy would have worked best with. Ideally, it also provides you with some ideas on how to fine-tune your model.

Methods of Backtesting

The most popular methods of backtesting are designed to measure the so-called “Value-at-Risk.” VaR reveals the maximum loss you can expect for the particular examination period and data set and its chance to materialize as its name suggests.

By being aware of their portfolios’ Value-at-Risk, investment managers or traders can more thoroughly prepare for the worst-case scenario.

We can examine Value-at-Risk using several different methods. Some of these include the Monte Carlo simulation, the Variance-Covariance Method, and more.

What unites all backtesting methods is their conclusions. They tell you where your strategy falls short and where it performs well so that you can make the right adjustments and optimize it to ensure optimal risk/return.

How to Evaluate The Results?

After you finish backtesting, it is time to interpret the results and see how your strategy performs over the observed period.

You should know that there is no golden formula or rule that will define whether your trading strategy is good or bad. For all analysis you do, you need to keep in mind the necessary context. Some of that context includes what other assets you have in your portfolio, the market environment, and the strategy’s unique characteristics. Some strategies, for example, are riskier by design. Naturally, they will achieve higher profits as well. Others are more conservative and will lead to a lower increase in your portfolio’s value at the end of the backtest.

The best way to evaluate your strategy’s results is by defining your risk appetite and profit goal. After you run the backtest, check whether the strategy generates results in line with your goals. Also, make sure to add a benchmark (the S&P 500 is the most widely used one). After you execute the backtest, you will see how your strategy fares to the market.

The backtesting methodology will generate results for different measures. These include net profit and loss, total portfolio return over the given time frame, risk-adjusted return, market exposure, volatility, and more.

In the end, the software will quantify the results of the backtest for each of the measures. Depending on the software you use, you might also generate graphs and visualize the backtest’s result.

What Are The Metrics of Success?

Don’t make the mistake of choosing your strategy based solely on its returns. Although profits are essential, when out-of-context, they don’t provide any useful information. Just the opposite – they can deceive you and make you choose a highly-risky strategy.

To avoid falling for this trap, analyze returns in addition to the adopted risk. Understandably, the best strategy achieves satisfying returns without significant risk exposure. Alternatively, it has a high Sharpe ratio.

Also, make sure to track the volatility. If, after the backtest, you spot that your portfolio has periods with high volatility, you should be aware that if this translates into the real trading world, you risk triggering your stop-loss or take-profit orders. That is why it is essential to wait and adjust your orders according to your portfolio’s volatility.

Another essential thing that defines your success is the correlation among the constituents. If there is a high correlation between the assets, it means your portfolio won’t be resilient enough to withstand shocks and sector-specific risks. Alternatively, it will have low diversification and inadequate hedging. That makes it more vulnerable and likely to be affected by different hazards.  

Final Thoughts

A decade ago, backtesting was an exclusive right reserved only for the big sharks like hedge funds, investment banks, high-frequency trading firms, etc. Today, thanks to technology, backtesting is available even to retail traders and small-scale investors. Backtesting nowadays is no more a luxury. It has turned into a necessity and a real must if you want to navigate financial markets successfully.

Trading without proper backtesting in the best-case scenario is taking an educated guess. Backtesting a trading strategy is the trader’s homework. Without an accurate initial analysis and risk estimation, you go out in the wild unprepared, vulnerable, and ready to be exposed not only by the market but also by the other traders as well. Bear in mind that most of the traders you compete with today employ backtesting. If you want to avoid losing ground and gain a competitive advantage, always do your homework.