Tuesday, May 7, 2024

How to Backtest a Trading Strategy in Any Market

Must read


Backtesting is the systematic process of finding out if a trading strategy has worked in the past and therefore will be very likely to work in the future. 

This is the most important step that a trader can go through to prove that their trading strategy actually works.

Yet, so many aspiring traders miss this vital step.

The best part about backtesting is that you don’t necessarily need to know how to code to backtest.

There are many fantastic options for coders and non-coders alike.

So if you want to skip the pain of years of losing trades and blowing out accounts, keep reading to learn how to backtest a trading strategy in any market.

When you buy something through one of the links on our site, we may earn an affiliate commission.

Trading desk

Does Backtesting Really Work?

In my own personal experience and from reading the experiences of hundreds of traders since I started this website in 2007, the answer is a resounding YES.

But don’t take my word for it.

Watch my interviews with professional traders who have gone on to manage funds and trade full-time for themselves.

Here’s an example of one of my interviews.

?

In addition, there are countless trading books that prove that backtesting is the best way to master a trading strategy.

Think of it this way, would you buy a used car without test driving it first?

Of course not.

You need to test it out to see if it actually runs, if the air conditioner works, and that there aren’t any weird noises.

So in a similar way, you need to take a trading strategy for a “test drive” and find out its strengths and weaknesses.

But there are many more benefits to backtesting.

The Benefits of Backtesting

Here are the main benefits that you’ll get out of backtesting a trading strategy.

They are all very important for building your skills, proving that a strategy has an edge in the markets and optimizing a strategy.

Performance Assessment

Backtesting allows you to evaluate the effectiveness of a trading strategy by providing statistical data on its past performance, such as win rate, average profit per trade, drawdowns, and overall profitability.

It offers a risk-free environment to evaluate the potential of a strategy.

By analyzing historical data, you can gain insights into the strategy’s return on investment (ROI) and risk profile.

This is particularly valuable in identifying which strategies are likely to be profitable and which are not, enabling you to make informed decisions about where to allocate your resources.

Risk Management

Worried man

Risk is one of the most important things to manage in trading.

A thorough backtest will provide the following information about the risk profile of a trading strategy:

  1. Understanding Risk/Reward Ratios: It helps in understanding the risk/reward ratio of a strategy by quantifying potential losses and gains. You can see not just the profitability but also how much risk is being taken to achieve that profit.
  2. Exposure to Market Conditions: Backtesting exposes a strategy to various market conditions, including high volatility periods, market downturns, and bull markets. Understanding how a strategy performs under these conditions helps you manage risk by knowing when a strategy might not perform well.
  3. Setting Risk Management Parameters: Based on backtesting results, you can set stop-loss orders, take-profit levels, and position sizes that align with your risk tolerance and capital preservation goals. This ensures that trades are exited at predetermined levels to minimize losses or protect profits.
  4. Statistical Analysis: It provides statistical measures of performance like the Sharpe ratio, drawdowns, and win rates. These metrics are essential for assessing the risk-adjusted returns of a strategy.
  5. Risk of Ruin: This shows how likely a strategy is to have a large drawdown or blow out the entire account.

Strategy Optimization

You can use backtesting to fine-tune your strategies by adjusting different parameters and rules to improve performance and adapt to different market conditions.

Through backtesting, you can identify the optimal settings for your strategy, such as stop-loss orders, entry and exit points, and position sizing.

Adjusting these parameters based on historical performance can help in refining a strategy to achieve higher returns or to minimize risk.

Confidence Building

Doubt is the kiss of death in trading.

A well-backtested strategy can give you confidence in your approach since you have historical evidence that your strategy has been profitable in the past.

If you don’t have confidence in your trading strategy, you’ll mess with good trades unnecessarily and you’ll probably skip many profitable trades altogether.

Just like any great athlete has confidence in their skills, traders need to build confidence in their strategies to be successful.

When you’ve seen a setup hundreds of times in backtesting, taking a trade becomes a no-brainer because you know what a good trade looks like.

Identifying Market Conditions

A common question from new traders is: How do I know that the market is in a trend?

Well, you learn to identify any market condition through backtesting.

This could be a ranging market, trending market or anything else.

Once you’ve seen a particular type of market many times, you’ll get a feel for what it looks like.

Reducing Overfitting

When you backtest a trading strategy over a wide range of historical data, you can identify if the strategy is overfitted to a specific period or set of conditions.

A robust strategy should perform well across different timeframes and market environments.

One common backtesting mistake that many traders make is they only backtest and optimize their strategy over a short period of time.

Then they try to trade it in current market conditions and they wonder why it doesn’t work.

The reason that it doesn’t work is because the strategy was optimized over, say 1 year, but that could have been an unusually good period for that strategy.

Over the entire history of that market, that could have been the absolute best time for that strategy.

I’ve personally seen this happen.

So if they try to trade that strategy at any other time, it will fail miserably.

Backtesting over a long historical period ensures that a strategy is robust enough to work in many different types of markets.

Save Time, Money and Reduce Stress

Clock on wall

Backtesting is much faster than waiting around for the markets to print candles in real time.

You can get decades worth of backtesting trades in as little as a few minutes.

Testing also allows you to evaluate a strategy without risking real capital.

More importantly, backtesting will save you the headache of jumping from strategy to strategy, while losing money along the way.

So even though it can be exciting to jump into real-money trading right away, that’s always the longer route to success.

Backtesting first will seem longer initially, but is actually a shortcut. 

Emotional Discipline

Adhering to a strategy that has been rigorously backtested will make it easier to stick to your plan and make less impulsive decisions.

By testing and adhering to strategies that have shown promise in historical simulations, you’ll avoid taking random, unproven trades based on emotions or market volatility.

This disciplined approach is crucial in maintaining consistency and achieving long-term profitability.

Types of Backtesting

There are 3 types of backtesting.

They will all get you a similar result, but the route you take to get that result will be different.

Each one has its benefits and downsides, so don’t get too hung up on being able to do fully automated backtesting right away.

It sounds sexy.

But in reality, most people do better learning how to manually backtest first, then moving up the scale to automated backtesting…if they are so inclined.

Manual Backtesting

I feel that this is the place where most traders should start.

It’s easy and anyone can do it.

Moreover, manual backtesting allows to you get very “intimate” with the data and every single trade.

In other words, you can see what each trade is doing on a very granular level and that can make it much easier to spot potential optimizations and errors.

Another benefit of manual backtesting is that most trading strategies cannot be fully automated.

There is often an element of discretion in most trading strategies, and therefore you’ll have a lot more flexibility with manual backtesting.

You can use almost any trading platform to do manual testing, provided it has enough historical data.

Here’s an example of a way that you can do manual backtesting for free.

Semi-Automated Backtesting

An intermediate step that not a lot of people talk about is semi-automated backtesting.

This is when you create scripts or automations that only manage part of your strategy, like the entry, the exit or the trade management.

Semi-automation allows you to speed up the backtesting process dramatically, while still being able to use the discretionary elements of a strategy.

It’s a great compromise between manual and fully automated.

There are many ways to do this, but this example will get you started.

Automated Backtesting

Now we jump into fully automated backtesting.

To get the most out of this method, you’ll probably have to learn how to code.

Learning to program will give you the most flexibility and allow you to control every aspect of a backtest.

There are no-code ways to do automated backtesting, but they do have their limitations.

Again, this reduces the number of strategies you can backtest because not all strategies can be turned into computer code.

Another downside is that it can be tough to see potential issues with a strategy because you aren’t seeing every single trade on a chart.

But if you’re a developer or engineer, this option might be very appealing to you.

There have been many successful automated traders, so don’t let those facts discourage you.

The Turtles are a good example of very successful traders who use automated strategies.

How to Backtest a Trading Strategy Step-By-Step

Backtest a trading strategy with NakedMarkets

Regardless if which method you use to backtest a trading strategy, the process is always going to be the same.

These are the steps to do your first backtest.

Step 1: Pick One Market and Timeframe

There is a misconception among many new traders that a trading strategy will work equally well in any market and on any timeframe.

Not true.

So it’s best to pick one market and timeframe that you’ll master, before moving on.

How do you pick the best ones?

That’s a very individual choice.

There’s no “best” trading market or timeframe for everyone, only the ones that you’re most comfortable with.

So take some time to learn about different markets and pick the one that appeals to you the most.

Backtesting on the 4 hour chart or higher is fairly straightforward.

But day trading strategies are a little more involved so learn the details here.

Step 2: Learn Trading Strategies for Your Market

There are countless posts on forums like Reddit that show a chart and ask: Is this a good trade?

That’s always painful to read because it’s obvious that the person doesn’t know anything about trading strategies.

Just like basketball players have their favorite moves for getting the ball in the hoop, traders need to have their favorite trading strategies to extract profit from the markets.

The fastest way to find a trading strategy to test is to see what successful traders are doing in the market you’ve selected.

Like with choosing a market, choosing a trading strategy will be very individualized to you.

Find a strategy that makes sense to you and that looks easy to test.

It also helps to adopt a curious mindset.

Many times traders can get too wrapped up in finding the most profitable strategy.

I’ve certainly been there.

But if you approach strategy selection from the standpoint of having a feeling that a strategy will work, and being genuinely curious about the results, I believe that you’ll get much further than if you just look for maximum profit.

Step 3: Create a Complete Trading Plan to Backtest

Journaling at night

Once you have a general strategy idea, it’s time to turn that into a complete trading plan.

It helps a lot to write that plan down because you can reference it throughout your backtesting sessions.

When you don’t have a written plan, it’s too easy to stray from the plan and take random trades.

If you take random trades, you won’t know how well your plan really works.

Define every element of your plan like:

  • Entry criteria
  • Exit criteria
  • Trade management criteria
  • Risk per trade
  • When you trail your stop loss or move it to breakeven
  • Maximum number of open trades at the same time
  • If you’re going to take trades in correlated markets or not

An easy way to get started is to download my free trading plan worksheet.

Step 4: Choose Your Software and Get Historical Data

Before you start testing, make sure that you have access to a lot of historical data for your chosen market.

At least 20 years of data is a good place to start.

This will determine which backtesting software or programming language you’ll use.

I provide some software suggestions in the next sections.

The best software for you will depend on the market your trading.

But for now, just know that you have to test your trading plan in some sort of software platform.

You can also download historical data from third party data providers and upload it to your software.

So if you like a software solution, but it doesn’t have enough data, just know that this option is available.

Step 4: Backtest

Backtesting computer

Alright, now it’s time to backtest!

Start up your backtesting software and take trades according to your plan.

Most backtesting platforms will have instructions on how to do your first test.

Obviously I cannot list instructions for every single platform and programming language out there because it would make this article way too long.

So it’s up to you to find the instructions for your platform.

Many people also make tutorials on YouTube, so that’s another great resource.

When backtesting on the 4 hour chart or higher, then backtest with all of the data you have available.

That’s fairly straightforward.

The shorter timeframes are a little harder to test.

It’s not always possible to backtest all of the data because there is just too much to backtest.

In this case, pick a few different market conditions like:

  • Ranging markets
  • Strongly trending markets
  • Weakly trending market
  • Highly volatile markets

It helps to zoom out to a higher timeframe to see these types of markets.

For example, if you’re backtesting on the 15 minute chart, zoom out to the 4 hour chart to see the overall market conditions.

Then within each of those periods, test a couple of years.

These “spot checks” will give you a good idea of how your strategy performs under different market conditions and if you should continue testing or not.

If your results are favorable, then you can do more in-depth backtesting.

Step 5: Review the Statistics

Trading strategy results.

When you have completed a full backtest, review the results to see if the strategy has potential.

You can use a simple Excel spreadsheet to do your calculations.

Even better, if the software you’re using has built-in analytics, that will save you a lot of time.

What you’re looking for will depend on your goals.

Some traders look for total return, others look for consistency, and others value low risk.

Ask yourself what you value most and make that your goal. 

Step 6: See if the Results can be Optimized

Backtesting results graph

Spoiler alert: Most backtests will have mediocre or losing results.

But don’t get discouraged.

Almost all trading strategies will have to be tweaked and optimized to work well. 

That’s the nature of the beast.

So be willing to experiment and try different ideas. 

Here are some ideas for optimizing the results of your strategy:

  • Experiment with different indicator settings
  • Trail your stop loss
  • Target a smaller profit target
  • Target a larger profit target
  • Split your profit targets
  • Tighten your stop loss
  • Make your stop loss larger
  • Add an additional indicator or criteria to enter or exit
  • Don’t trade on days or at times that have a high percentage of losses

Just be careful of over optimizing a strategy.

This is when you make a trading strategy work very well for the backtesting period, but it doesn’t perform well in other periods.

To avoid this, it helps to split your data up into in-sample and out-of-sample data.

In other words, leave some data out of your optimization process so you can backtest on it to see if your strategy will work well in a period that hasn’t been optimized for.

For example, let’s say that you have 20 years of data on the daily chart.

You could backtest and optimize on 15 years of data.

Then see how that strategy works on the remaining 5 years of data that you didn’t optimize for.

This is easier to do on shorter timeframes because there is much more data.

Doing this one extra step can help you understand how well your strategy will work in the future.

Step 7: Decide to Keep or Trash the Strategy

trash can

Once you’ve done all of the potential optimizations you can think of and the strategy still isn’t as profitable as you would like, then it’s time to trash the idea and move on.

That’s usually very obvious.

What isn’t as obvious is when a strategy is slightly profitable.

If that’s the case, then test the trading strategy on multiple timeframes and multiple stocks, futures contracts or currency pairs.

Adding more trading opportunties can create a more favorable return.

You might also consider trading a portfolio of different strategies.

Each one on its own might not have a fantastic return.

But when traded together, they could be very profitable.

Backtesting in Different Markets

Each trading market has its own nuances and best practices when it comes to backtesting strategies in that market.

So now I’ll give you the benefits and downsides to each market.

I’ll also provide some tools and tips that can help you backtest more efficiently in each market.

Backtesting in Forex

In my experience, Forex is the easiest market to backtest.

There are only a set number of markets and some currency pairs have a long history.

The data is also easy to get and usually pretty clean.

It’s also the most liquid market in the world, so there’s very low slippage.

Transaction costs are also low on the major pairs.

My favorite backtesting software is NakedMarkets because it has free updated data and I can build semi-automated and fully automated strategies with the no-code interface.

Backtesting Indexes

Indexes like the S&P 500 are also easy to backtest because they have one continuous chart that goes back a long time.

To trade indexes, you can use futures, ETFs, or any other product that tracks an index.

Ease of backtesting will vary depending on which derivative you trade, but they can be a great market to trade.

I’ve seen some traders make a very good living just trading the S&P500 E-mini.

Great backtesting platforms are TradeStation, NinjaTrader or NakedMarkets.

Backtesting Stocks

Bull on Wall Street

Stocks are harder to backtest than other markets because there is a huge universe of individual stocks listed on any stock exchange.

I’m also going to group ETFs into this category because they are traded in a similar way to stocks.

On the upside, there are always many trading opportunties because there are so many stocks available to trade.

I use Amibroker, but there are many other platforms out there like TradeStation.

You can also do automated backtesting with programming languages like Python.

Backtesting Futures

Like in Forex, futures are fairly easy to backtest because there are a limited number of markets.

The biggest downside is that futures contracts expire, so there will always be a slight “jump” in the data when there is a contract change.

On top of that, you can trade different expiration months in the same contract, which can create some confusion.

Therefore, the easiest way to backtest futures is to find data that uses a continuous chart of the front month, or the contract that is going to expire the soonest.

This is usually the most liquid contract, making it less likely that you’ll get choppy price action and unreliable backtesting results.

It can also help to backtest each contract individually to eliminate some of the discrepancies that can come when one contract expires and the next contract kicks in.

I’ve tried to backtest futures, but I found it too frustrating to navigate the contract changes.

However, there are obviously many successful futures traders out there, so don’t get discouraged if you really like this market.

The premier backtesting platform for futures is TradeStation, but there are many other ones out there like NinjaTrader.

Backtesting Crypto

Since crypto is an easy market to backtest, there are many software packages that can backtest this market.

The biggest downside is that crypto is a fairly new market, so you won’t have much data to test with.

Therefore, you might be better off trading a lower timeframe, or using a scale in / scale out approach.

Many markets also don’t have a lot of liquidity, so you’re generally better off testing the major ones like Bitcoin, Litecoin and Ethereum.

An upside to backtesting crypto is that there are very noticeable boom and bust cycles, making it somewhat easier to build strategies around.

I suggest using NakedMarkets to backtest cryptocurrencies.

Backtesting Options

Backtesting options is much different from other markets because of the way the contracts are structured and how strategies are constructed.

I’m not an expert in options, so the information here is from my research and not personal experience.

One thing that makes options hard to backtest is that there are different types of options: vanilla, binary, one-touch, double no-touch, American, European, etc.

European vanilla options are the most common, so that’s generally best to start there.

Since these options can only be exercised at maturity, it provides fewer variables in backtesting.

Popular backtesting platforms are tastylive and OptionAlpha.

How Far Back Should You Backtest a Trading Strategy?

Backtesting at laptop

The short answer is that you should backtest as far back as possible, and with as much data as possible.

You want to see how the trading strategy performed in as many market conditions as possible.

A common meme on the internet is that you need to backtest a minimum of 100 trades to prove that a strategy works.

That’s a myth.

As I detail here, the amount of trades you need to prove a trading strategy will depend on the strategy and trading timeframe.

Final Thoughts on Backtesting

So that’s how to backtest a trading strategy in any market.

Remember that there’s no best strategy or market for everyone.

The best combination will depend on your trading personality and what you like best.

So don’t look for the “most profitable” strategy and market.

Pick the ones that make the most sense to you.

Note: You may notice that I’ve left a popular backtesting tool off the list, TradingView.

fThis is a fantastic platform for doing many things, but backtesting is not one of them.

You’ll need to buy their higher plans to get the Deep Backtesting feature, which gives you access to more data.

In my opinion, it’s not worthwhile, at least at this point in time.

 





Source link

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article