fbpx

Backtesting Stocks: How to Test Your Strategy

W hen you’re developing strategies for trading stocks, you want to know if your systems will play out well in the real market before you risk tons of cash. Backtesting stocks provides a way to assess your strategies against historical data.

Key Takeaways:

  • Backtesting stocks can test if a trading strategy or system is viable by seeing how the strategy would have played out with historical data.
  • Backtesting can filter out price action noise and emotion to give you signals to follow when you’re trading.
  • You need to stay away from several biased actions to make sure your backtest gives you accurate and useful results.
  • If backtesting works, it can give you the confidence to employ the strategy in real markets going forward.

What Does Backtesting Stocks Mean?

Image via Unsplash by chrisliverani

If you can quantify a trading idea, you can also backtest your idea. Backtesting is a method to see how well a model or strategy would have performed. If you develop a strategy for trading stock, you can put it to the test with backtesting. Backtesting allows you to see how your trading strategy would have performed in the past using historical data. Using certain investment criteria, you can backtest to simulate what happened in the past.

You don’t have to wait for years to see how well a strategy might perform thanks to backtesting. It allows you to test out your hypotheses and pinpoint profitable strategies.

All in all, backtesting lets you use historical data to simulate a trading strategy and generate results. You can then analyze the risk of the strategy as well as the strategy’s profitability before you risk any real capital. It can be a key step to optimizing your trading strategy.

If you conduct a backtest well, you’ll gain valuable information about a planned strategy. A backtest that leads to positive results can assure you that your strategy is fundamentally sound and likely to get you profits if you implement the strategy in reality. On the other hand, if your backtest yields suboptimal results, you’ll know you need to alter or completely reject the strategy you just tested.

Benefits of Backtesting Stocks

A backtest tries to use mechanical and repeatable signals to find entry and exit signals. Ideally, these signals lead to bigger wins coupled with small losses over the long term. You do this by cutting losses short while letting winners run.

A trading system needs to have signals to filter out price action noise that can lead to overtrading. Effective signals show the meaningful moments when it comes to price actions.

Not only does trading with backtested signals filter out too much price action noise, but it also lets you trade while filtering out emotion. When you try to decide what to do with each trade from your own perspective, you inevitably bring in your emotions. By backtesting and then using systems you’ve proven to be effective, you instead follow a systematic process. You can ignore your feelings and simply follow signals after you’ve successfully backtested your strategies.

What Do You Need When You’re Backtesting Stocks?

If backtesting works, it can give you the confidence you need to employ your strategy going forward. But how do you go about backtesting stocks?

Make sure you execute any backtest over a long enough period of time that you can really see how the strategy performs in the market. You’ll want to see how your system plays out during everything from uptrends to downtrends to range-bound markets. By making sure you use a long enough period to see all of this, you’ll get more meaningful results from your test.

An ideal scenario when backtesting includes the following aspects:

  • Sample data: A backtest requires sample data from a relevant time period. The duration the data comes from should reflect a variety of market conditions. This allows you to better judge if positive market conditions. This allows you to better judge if positive backtest results really represent sound trading or if the results were just a fluke due to the specific data chosen.
  • Sample stocks: The historical data set used for a backtest should include a truly representative sample of stocks. That means including companies that eventually were sold, liquidated, or went bankrupt. If you use data only from historical stocks that still currently exist, you’ll end up getting artificially high returns from your backtest.
  • Trading costs: A good backtest will take all trading costs, even those you think are not significant, into consideration. Even small costs can add up over the period of the backtest, and those costs may significantly affect how a strategy’s profitability appears. You’ll want to make sure any software you use accounts for all trading costs.
  • Correlation with other tests: You can gain further confirmation about the effectiveness of your strategy by doing forward performance testing along with backtesting. If you see good correlation between test results from various testing methods, you can better understand how your strategy will perform before risking real cash. Good correlation can give further insight into the viability of your trading system.

You’ll also need to make sure you don’t fall into some common traps of backtesting. When you’re backtesting, you must:

  • Test strategies in good faith: You’ll need to carefully avoid bias as much as you possibly can to get meaningful results from backtesting. You should develop any strategy without using the data you’re going to use in your backtesting. Because traders typically develop strategies using historical data, that can be more difficult than it would seem to stick to. You have to be strict and test your system with different data sets than the data sets you use to train the models, or the backtest will end up producing great but completely meaningless results.
  • Avoid data dredging: Data dredging means you test a wide range of hypothetical strategies against one data set that will produce success that fails in real markets. Don’t do that. There are tons of strategies out there that can beat the market by chance over a specific time period.
  • Compensate for biased tendencies: For better or worse, human nature means certain biases. You can compensate for the tendency to cherry-pick or data dredge by using a strategy that succeeds in your relevant (known as in-sample) time period. Then, backtest the strategy on data from a different (or out-of-sample) period of time. If you get similar results from both in-sample and out-of-sample backtests, you can generally assume the results are likely valid.

Backtesting Stocks vs. Other Tests

Backtesting is a useful tool when developing new trading strategies, but it isn’t the only tool you have at your disposal. You can use more than one strategy to get a broader picture of how you can expect your strategies to perform.

Other testing possibilities include:

Paper Trading

Paper trading, or forward performance testing, gives you another set of out-of-sample data that you can use to evaluate your system. Paper trading is just a simulation of actual trading. It involves following the logic of a system in a live market. All trades are only executed on paper, hence the name paper trading. In other words, you’ll document the entries and exits of all trades as well as any profits or losses your system generates, but you will not execute any actual trades.

Forward performance testing requires you to exactly follow the logic of the system. If you don’t do that, you will not be able to accurately evaluate the process. You’ll need to stay honest about entries and exits and stay away from behavior such as cherry-picking trades.

If you don’t include a trade on paper because you rationalize that you would never have taken the trade, you end up skewing the results of the forward performance testing. You should make sure to document and evaluate any trade that would occur following your system’s logic to get an accurate result of how the system performs.

Scenario Analysis

Backtesting uses real historical data to test how a trading system would have performed. Scenario analysis, on the other hand, uses hypothetical data to simulate various possible outcomes.

Scenario analysis can simulate things like particular changes in the value of the securities in a portfolio. Scenario analysis can also look at what would happen should a key factor occur, like a change in interest rate.

You might use scenario analysis to estimate how your portfolio’s value would change when responding to an unfavorable event. You could also use it to analyze a worst-case scenario in theory.

Software and Websites for Backtesting Stocks

Stock backtesting websites and software let you check your trading strategy and see how effective it is. Software can provide a detailed report that includes risks, reward, winning percentage, drawdown, and more. Some programs to try include:

  • Interactive Brokers’ Portfolio Manager.
  • Portfolio123.
  • MetaStock’s Backtesting Software.
  • MetaTrader 5 Strategy Testing.
  • QuantBlocks.
  • Quantopian.
  • Quantshare.
  • Tradestation.
  • Tradingview.
  • The TrendSpider.

I f you’re looking for a way to test your strategies before using them on the market, backtesting offers a way to do it. Backtesting stocks will give you a sense of your strategy’s viability on the market, giving you the confidence you need to use it in the real world for success.