Automated Backtesting Archives - Trading Heroes https://www.tradingheroes.com/tag/automated-backtesting/ Discover Your Grail Trading Strategy Tue, 12 Aug 2025 23:08:53 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 https://www.tradingheroes.com/wp-content/uploads/cropped-white-color-32x32.jpg Automated Backtesting Archives - Trading Heroes https://www.tradingheroes.com/tag/automated-backtesting/ 32 32 Backtesting Limitations (Manual and Automated) https://www.tradingheroes.com/backtesting-limitations/ Tue, 09 Apr 2024 00:34:12 +0000 https://www.tradingheroes.com/?p=1025402 Learn the limitations of both automated and manual backtesting. Also discover tools and techniques that will help you overcome them.

The post Backtesting Limitations (Manual and Automated) appeared first on Trading Heroes.

]]>

Backtesting is the best way to verify that a trading strategy has an edge and optimize that strategy to meet your goals.

Despite its importance, backtesting does have its limitations.

The primary constraints of manual backtesting are that it requires discretionary input from the trader and is not highly scalable. Automated backtesting is limited by the quality of the code and it lacks flexibility. 

I've been backtesting since 2008 and it has helped me tremendously and has helped countless traders that I've met over the years.

My goal in this article is to help you understand the details of the limitations of the backtesting method you use, so you can minimize the negative impact they can have on your results.

These limitations apply to backtesting trading strategies in any market.

Alright, let's get into it…

Limitations of Manual Backtesting

Manual backtesting involves a trader simulating a trading strategy on historical data by manually checking whether each trade would have been a success or a failure according to the strategy's criteria.

This process requires the trader to scroll through past market data, apply the trading strategy rules, and record the outcomes of each hypothetical trade.

There are many software solutions that can dramatically speed up this process, or a trader can choose to simply record their results on a spreadsheet.

Here are the limitations of manual backtesting that you need to be aware of.

Human Discretionary Bias

Trader backtesting at desk

Arguably, the biggest limitation of manual backtesting is that human bias is introduced into the backtesting process.

This is not necessarily a bad thing however.

The benefit to manual backtesting is that it can test discretionary trading strategies, of which there are many more than automated strategies.

So if you backtest manually, the key to success is applying your logic consistently.

Even though you are making discretionary decisions, do your best to understand what a good trade looks like.

But even the most diligent trader will have some variability in their backtesting, so it's important to test the same strategy several times before trading it live.

It's also worth noting that since there is individual discretion in manual backtesting, results can vary greatly between traders.

Therefore, it's always best to test a strategy for yourself and not rely soley on the results of others.

Relatively Slow Process

Low speed

A big downside of manual backtesting is that it's inherently time-consuming.

You must painstakingly go through historical price data, applying a strategy's criteria to determine entry and exit points.

This process is not only slow but also prone to errors, as it relies heavily on your attention to detail and patience.

So when backtesting manually, it's important to focus on testing one strategy at a time.

The upside to this slower process is that you get a more detailed look at each trade and that can help you develop new optimizations faster than with automated backtesting.

Not Scalable

Since manual backtesting is slow, it cannot be scaled efficiently.

As traders seek to validate their strategies across different instruments, timeframes, and market conditions, the manual process becomes exponentially more cumbersome.

So if you're going to backtest manually, you have to be resigned to the fact that you'll only be able to test a few markets and timeframes at a time.

But this can be a blessing in disguise because it will allow you to become an expert in a few core markets, instead of a generalist in many markets.

Risk of Overfitting

Super profitable trading strategy

The second major limitation is the risk of overfitting.

Overfitting occurs when a strategy is too closely tailored to past data, making it perform exceptionally well on historical data but poorly in real-world trading.

Manual backtesting, with its reliance on subjective judgment, increases the risk of introducing biases into the testing process.

You may unconsciously select data that confirms the strategy's effectiveness or overlook data that contradicts it.

This selection bias can lead to over-optimistic results that do not accurately reflect the strategy's real-world performance.

Therefore, it's important to test your strategies on as much historical data as possible and not “cherry pick” the best performing periods.

Potential Miscalculations

Real-world trading involves factors such as slippage, transaction costs, and varying liquidity, which can significantly impact the profitability of a strategy.

Manual backtesting can oversimplify these aspects and make strategy look much more profitable than it really is.

Without a realistic representation of market conditions, the results of manual backtesting can be misleading, painting an inaccurate picture of a strategy's potential success.

To overcome this, always check that you're using realistic settings for things like commission, spread and slippage.

If these things are hard to factor in, you can always manually lower the return of your strategies slightly to account for unknown variables.

Limited Complexity

Another critical limitation of manual backtesting is the difficulty in testing complex or quantitative strategies.

If you're going to backtest discretionary trading strategies, you have to stick to methods that are simple and easy for you to calculate and execute.

Manual backtesting is not suited to handle high-frequency trading strategies or those that rely on complex analysis.

Although it may seem appealing to execute super complex, uber clever strategies, in my experience it's often the simple strategies that do best.

Limitations of Automated Backtesting

Automated or programmatic backtesting has revolutionized the way traders develop and evaluate their strategies, leveraging historical data to predict future performance without manual intervention.

By simulating trades based on specific criteria and algorithms, this method offers efficiency, precision, and the ability to test complex strategies across multiple datasets and timeframes.

Despite these advantages, automated backtesting is not without its limitations, which can impact the reliability of the results and the ultimate success of trading strategies.

Overfitting Risk

Office with blackboard and math equations

One of the primary challenges with automated backtesting is the risk of overfitting.

Overfitting occurs when a model is excessively complex, with numerous rules or parameters that are tailored to perform exceptionally well on historical data but fail to perform in the future.

This phenomenon leads to inflated backtest performance that cannot be replicated in live trading.

An example of this is the story of LTCM (Long-Term Capital Management), a hedge fund that relied heavily on complex mathematical models.

Despite the brilliance of its team, which included Nobel laureates, LTCM folded in 2000, due to a combination of excessive use of leverage and strategies that were overfit to past market conditions.

Look Ahead Bias

Another significant limitation is the “look-ahead” bias, which occurs when a strategy inadvertently utilizes future information in its trading decisions, leading to unrealistic backtest results.

This can happen through programming errors or when the data set includes future data not available at the time of trade execution.

The illusion of extraordinary returns generated by such biases can be misleading and result in substantial losses when the strategy is applied in real-time trading.

Trading Fees Miscalculation

Trading fees also present a hurdle for automated backtesting.

Historical data may not fully capture the market's liquidity, bid-ask spreads, and slippage that can significantly affect transaction costs and execution.

Automated backtests often assume ideal trading conditions, neglecting these real-world trading expenses.

This oversight can lead to an underestimation of costs and an overestimation of strategy performance.

Lack of Human Input and Intuition

Furthermore, the absence of human intuition and experience is a notable drawback of automated backtesting.

While automated systems excel in processing vast amounts of data and executing predefined strategies, they lack the ability to interpret nuanced market signals or adjust to unforeseen events.

In other words, they are only able to run the instructions they are given.

Famous traders like Jim Simons, the founder of Renaissance Technologies, have successfully combined automated trading with human oversight.

Simons, a mathematician, and his team developed sophisticated algorithms that have consistently outperformed the market.

However, the success of Renaissance Technologies also relies on continuous refinement of its models and the expert judgement of its team, highlighting the importance of blending automated strategies with human insight.

Technology Risk

Computer next to window

The dependence on technological infrastructure can pose serious risks.

Once an automated strategy is backtested, live trading requires robust computing resources, stable internet connectivity and continuous human oversight.

Since computer programs only follow instructions they are given, if certain scenarios are not accounted for, any failure in these systems can lead to significant losses, especially with high-frequency trading strategies.

One example is what happened to Knight Capital Group in 2012.

A glitch in their computer programs caused them to amass huge losses and almost sent them into bankruptcy.

Final Thoughts on Backtesting Limitations

Even though there are limitations to both manual and automated backtesting, there's no doubt that some sort of backtesting has to be done to verify and optimize trading strategies.

Risking real money on an untested strategy is like hiking in the mountains without a map.

You don't know a proven path, you're very likely to get lost on your hike and maybe not even make it back.

Therefore, the solution is to pick the backtesting method that best suits your skills and goals.

Then understand the limitations of your chosen method and minimize the negative impact of its limitations.

If you want to learn the tools and techniques for backtesting both manual and automated trading strategies, continue reading my tutorial on how to backtest a trading strategy.

 

The post Backtesting Limitations (Manual and Automated) appeared first on Trading Heroes.

]]>
How to Backtest a Trading Strategy in Any Market https://www.tradingheroes.com/how-to-backtest-a-trading-strategy/ Sat, 06 Apr 2024 05:06:10 +0000 https://www.tradingheroes.com/?p=1023943 Learn how to backtest a trading strategy and choose the best strategy for you. Get the tools, tips and techniques that pros use.

The post How to Backtest a Trading Strategy in Any Market appeared first on Trading Heroes.

]]>

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

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

Backtest a trading strategy with NakedMarkets

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.

 

The post How to Backtest a Trading Strategy in Any Market appeared first on Trading Heroes.

]]>
How to Automate Backtesting of a Trading Strategy https://www.tradingheroes.com/automate-backtesting/ Fri, 29 Mar 2024 10:13:37 +0000 https://www.tradingheroes.com/?p=1025107 Learn how to automated backtesting of a trading strategy in any market and on any timeframe. Get the tools, techniques and more.

The post How to Automate Backtesting of a Trading Strategy appeared first on Trading Heroes.

]]>

In my experience, backtesting a trading strategy is an essential step before deploying it in the market.

It allows me to assess how the strategy would have performed in the past, giving me a clearer idea of its potential success in the future.

Manual backtesting is great, but automated backtesting speeds up the process considerably.

There are three ways to automate backtesting of a trading strategy: learn a programming language, use pseudocode software or use no code software. Each one has pros and cons that must be considered when choosing an automated backtesting method. 

So in this article, I'll explain each of the options and how to figure out which one is the best one for you.

Keep in mind that you don't necessarily need to learn programming to do an automated backtest, and this can be done in any trading market where sufficient historical data is available.

I'll also provide links to the best resources that I know of, based on my experience and research.

Let's get into it…

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

Backtesting in office

Key Takeaways

  • Automated backtesting can be done even if a trader doesn't want to learn how to code.
  • Not all trading strategies can be fully automated. But in those cases, semi-automated backtesting can be extremely helpful.
  • There are many free and paid tools that can help traders of all skill levels get into automation.

Why Backtesting a Trading Strategy is Important

I've written many articles and tutorials on backtesting.

You can find them here.

But if you're new to the idea of backtesting, I'll give you the elevator version of why it's so important.

By utilizing backtesting software, I'm able to simulate years of trading within minutes, allowing me to test multiple strategies against a wide range of market conditions.

The software does the heavy lifting, executing my strategy across the selected time period and accumulating the results for my analysis.

What I look for in the backtest results is not just overall performance, but also how the strategy behaves during different market phases.

Whether the market is trending, ranging, or experiencing high volatility, I need to know that my strategy can handle these conditions.

The insights gained are invaluable.

They either prove that the strategy has a legit edge, or I go back to the drawing board to make improvements.

Why Automated Backtesting?

The bottom line is that automated backtesting helps me speed up the process of creating and optimizing profitable trading strategies. 

I first started backtesting with a simple chart and a spreadsheet.

That was fine in the beginning because there weren't the tools that are available now.

But it would take foreeeever to backtest just one market, even on the daily chart.

When I can automate backtesting, I can speed up that process dramatically and test multiple markets in a fraction of the time.

What many traders also miss is that they can do semi-automated backtesting. 

Since not all trading strategies can be 100% automated, semi-automated backtesting allows me to automate just the entry, for example.

Then I can use discretion to exit the trades.

Any aspect of a trading strategy that can be semi-automated, should be, because it saves a ton of time.

Benefits of Automated Backtesting

Automating the backtesting process offers efficiency and the ability to test numerous strategies across different timeframes and market conditions very quickly.

Many software packages will also provide detailed analytics on each backtest.

This allows me to focus on coming up with ideas and optimizing, instead of figuring out how to get these metrics in a spreadsheet.

The tools available nowadays can make this easy to do.

Limitations of Automated Backtesting

Despite its many benefits, there are limitations to automated backtesting.

One primary challenge is ensuring the historical data used is complete and accurate.

Any gaps or inaccuracies can significantly alter the test results.

There's also the risk of overfitting a strategy to past data, making it less adaptable to future live market conditions.

Another point to consider is simulation limitations, as models may not account for factors like market impact, liquidity, or real-time transaction fees.

Finally, not all trading strategy criteria can be completely programmed into a computer.

The First Step Before Backtesting

Hands writing in journal

Before you start backtesting with one of the solutions below, be sure that you have a trading strategy to test.

This means having a complete trading plan.

You need to have specific parameters for your entries, exits, risk and trade management.

Once you have a strategy to test, now it's time to pick a platform to help you do an automated backtest.

Automated Backtesting Software Tools

Before picking a robust backtesting framework, it's essential to understand your options.

So here are the 3 types of tools that are available, along with some resources in each category.

New solutions are constantly popping up, so use this list as a starting point in your exploration.

I'll update this list as frequently as possible, but it's up to you to find the best solution for your situation.

Learn a Trading-Friendly Programming Language

Trading at computer

To effectively develop and customize a trading algorithm, you must be fluent in a programming language commonly used by traders.

I haven't had the patience to learn a programming language myself, so I'm not an expert in this area.

But I have done quite a bit of research on the topic and here's what you need to know.

Benefits of Programming

  • There are highly mature languages that have been proven to work for trading
  • Many free tools available
  • The ultimate level of customization capabilities
  • You can build reusable blocks of code that you can use for future backtests
  • There is a large library of open source code and tutorials that you can use to get started quickly
  • Can be used in any market where historical data is available

Downsides of Programming

  • It can take time to learn how to code at a level that will allow you to create meaningful backtests
  • Creating simple things like performance graphs usually takes extra time to learn
  • One misplaced semi-colon can break the entire program and take extra time to find and fix

There are 3 programming languages that are frequently used by professional traders:

  • Python
  • R
  • C#

I'll give you a quick summary of each one and where you can go to get more information.

Python Programming Language

Python programming language

I personally know a couple of traders who build their trading strategies with Python.

It emphasizes code readability and allows programmers to express concepts in fewer lines of code compared to other languages.

Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming.

It has a vast standard library and a thriving ecosystem of third-party packages.

Python's interpreted nature facilitates rapid development and debugging, making it popular among both beginners and seasoned developers.

Here are resources that will get you started with programming in Python: 

R Programming Language

R is a programming language and environment that is primarily used for statistical computing and graphics.

Perfect for trading.

It provides a wide variety of statistical and graphical techniques, including linear and nonlinear modeling, time-series analysis, and clustering.

R is open-source and highly extensible, with thousands of packages available for specialized tasks.

It is widely used in academia, research, and industry for data analysis, visualization, and machine learning applications.

R's syntax is designed to make data manipulation and analysis intuitive and efficient, making it a popular choice among statisticians and data scientists.

Here are some resources that will get you started with programming in R: 

C# Programming Language

C# is a versatile, object-oriented programming language developed by Microsoft as part of its .NET framework, designed to be simple, modern, and type-safe.

It's widely used for developing desktop applications, web services, and enterprise software, and it has extensive libraries that facilitate tasks ranging from database interaction to web development.

Traders use C# to program automated backtests by writing algorithms within trading platforms like WealthLab, or QuantConnect, which support C# natively.

They utilize the language's robust features to define trading signals, manage orders, and analyze historical data for performance evaluation.

The backtesting environment provided by these platforms allows for the simulation of trading strategies over past market data to assess their efficacy before live deployment.

Here are some R resources that will get you started with programming in C#: 

Now if programming isn't your cup of tea, then luckily there are other options out there.

Use Pseudocode Software

 

Pseudocode software serves as an intermediary solution between programming and no code software.

It's meant to be a little easier than traditional programming, but still give you more control and customization than the no code route.

There are many built-in shortcuts that are included in each platform, reducing the amount of code needed for each application.

I'm personally not a fan of these solutions either, because they are still too much like programming for my taste.

However, this is useful to some traders who want to automate their backtesting, without learning all the nuances of a true programming language.

So if you don't want to become a full-on programmer, then this might be a happy medium.

Benefits of Pseudocode Software

  • Easier to learn than programming
  • Usually more built in tools for things like graphs and statistical analysis
  • Less code needed when compared to traditional programming

Downsides of Pseudocode Software

  • Platform dependent, so your pseudocode will only work on one platform
  • You may not be able to do everything you can do with a regular programming language, since there isn't the same level of customization available

I've personally used 4 solutions:

  • Pine Script
  • MQL
  • EasyLanguage
  • AFL

Here's a summary of each of them and where to get more information.

The right one for you will depend on the platform you use and the market you trade.

MetaTrader / MQL

MetaTrader 5 website

MQL, or MetaQuotes Language, is a proprietary scripting language for developing trading robots, technical indicators, scripts, and function libraries for use with the MetaTrader software.

MQL4 is used with MetaTrader 4, while MQL5, its successor, offers more advanced capabilities and is used with MetaTrader 5.

Both versions of MQL are specifically designed for automated trading, strategy testing, and financial market analysis.

This is a very popular language that some people might consider a full-on programming language.

In my opinion, it falls more into the category of pseudocode because you don't need to include all of the elements that are required for a normal computer program.

It's fairly complex however, so it does take some time to learn.

Here are some resources that will get you started with MQL: 

TradingView / Pine Script

Pine Script is TradingView's platform-specific scripting language used for creating custom technical analysis indicators and strategies directly within its charting platform.

It's designed to be user friendly, allowing traders with minimal programming experience to design, test, and share their trading ideas and systems.

Pine Script enables the customization of charts and the creation of alerts based on specific trading conditions.

I've created a few custom indicators with Pine Script and it is easier than MQL.

Here are some resources that will get you started with Pine Script: 

TradeStation / EasyLanguage

EasyLanguage is a proprietary programming language used in the TradeStation platform for developing and backtesting trading strategies.

It's designed to be user-friendly and accessible to traders without extensive programming experience.

EasyLanguage allows traders to create custom indicators, strategies, and trading systems using a simplified syntax and built-in functions.

Here are some resources for learning TradeStation's EasyLanguage: 

Amibroker / AmiBroker Formula Language (AFL)

AmiBroker Formula Language (AFL) is a scripting language used in the AmiBroker trading platform to create custom indicators, trading strategies, and analytical tools.

This solution is primarily for stock traders.

AFL allows users to define and manage charts, scan for opportunities, and backtest trading systems with speed and precision.

It's designed to be easy to understand for those familiar with technical analysis and trading concepts, while also being powerful enough to implement complex algorithms.

Here are some resources for learning Amibroker's AFL:

Use No Code Software

Man at beach

No code backtesting software allows traders to test their trading strategies using a visual interface without writing any code, typically by selecting indicators, setting parameters, and defining rules through dropdown menus and form fields.

These platforms often provide a user friendly drag-and-drop or point-and-click environment to facilitate the creation and historical evaluation of trading strategies.

They aim to make backtesting accessible to those without programming skills, while still offering robust analysis and performance metrics.

Benefits of No Code Software

  • Extremely easy to use
  • Can start creating meaningful backtest very quickly
  • Some platforms have a wide range of trading strategy inputs to create many different strategies

Downsides of No Code Software

  • Lack the same level of customization that programming or pseudo coding provides
  • Some platforms are very limited in what they can do

I'll share my experience with 2 platforms, but do your own research because new ones are popping up all the time.

NakedMarkets

Automate backtesting with NakedMarkets

This is my favorite no code platform.

It has a ton of flexibility that I haven't seen before in a no code platform.

I can create fairly complex trading strategy logic with its intuitive drag and drop interface.

You can backtest Forex, stocks, indexes and more.

The detailed analytics suite also provides a ton of data on a backtest that you don't get in other platforms.

Everything can be backtested in the platform, so there's no need to export an EA or plugin to another platform for backtesting.

They are also working on an export feature that will export automated trading strategies to MetaTrader.

Check out the videos and tutorials below to get a feel for what the program can do.

You can also get a discount and a special offer on NakedMarkets here.

Here are some resources for learning NakedMarkets:

EA Builder

EA Builder is an online platform that allows traders to build MetaTrader 4 and 5 Expert Advisors (EAs), and TradeStation strategies, without coding.

The benefit is that the software will export the strategy that you build on the website to a file that you can import into MetaTrader or TradeStation.

I found that this helps a lot with very simple trading strategies.

But once you try to build more complex ones, the platform simply doesn't have the capacity to do that.

So if you're looking for something to build simple strategies and backtest them in MetaTrader or TradeStation, then this could be for you.

However, you should try their free demo first to see if this is a good fit for what you want to do.

Here are some resources for learning EA Builder:

Evaluating Backtesting Results

Automated backtesting results have to be evaluated a little differently than manual backtesting results.

The first thing I look for are potential errors in the logic of the trading strategy.

Since the trades are being executed automatically, I cannot see the trades in action.

So I have to double check that the trades look correct.

I do this by looking at the trades list and I spot check a few of the executions manually.

If that checks out, then here's what I look at:

  • Profitability: I not only look at the overall profit of the strategy, I also look at how consistent the strategies is on a yearly basis. A strategy can be profitable, but all of that profit could have come from just one month. I like to see steady growth over multiple years.
  • Win / Loss Ratio: I also take note of the win-loss ratio which indicates the proportion of winning trades to losing ones. A low ratio is not necessarily bad if the winners are much bigger than the losers, but I want to see if there is any room for improvement.
  • Drawdown: Understanding the drawdown of a strategy is vital. It involves assessing the largest peak-to-trough decline in my account balance. I calculate the maximum drawdown and if it's too large, it indicates potential high risk, and I should probably adjust my risk management approach.
  • Monte Carlo Simulation: I run a simulation over at least 100 simulations to get a good idea of the maximum potential drawdown.
  • MFE/MAE: These metrics show how much the trades went in my favor or went against me, while the trades were open. This can give me valuable clues as to how to improve the strategy.
  • Risk of Ruin: This shows my chances of blowing out an account with a strategy. I want the risk of a huge loss to be fairly low. Other traders can tolerate a more risky strategy.
  • In and Out of Sample Data: It helps to test a strategy with only part of the historical data, then test the optimized strategy with the rest of the data. If the strategy works well with both data sets, then the strategy is more robust and probably more reliable.
  • Time of Day or Day of the Week Analysis: I look at how profitable certain days are, or how profitable certain hours of the day are, if I'm testing a day trading strategy. This will show me if I might want to stop trading certain days of the week or hours of the day.

The reality of backtesting is that most trading strategies won't be profitable enough to trade with real money. 

But by reviewing the metrics above, I can potentially spot ways to turn a failed strategy into a profitable one. 

I can also potentially make a profitable strategy even more profitable.

Final Thoughts on How to Automate Backtesting

Automated backtesting can take your backtesting process to the next level by allowing you to test a ton of ideas in a very short amount of time.

In addition, you can leverage semi-automated backtesting to test specific aspects of a trading strategy, if the entire strategy cannot be automated.

The technology nowadays is quite good, so I feel that all traders should at least do some automated backtesting because it's so easy and fast.

So if automated backtesting appeals to you, take the time to learn your preferred platform.

It's one of the greatest skills that you can have as a trader.

 

The post How to Automate Backtesting of a Trading Strategy appeared first on Trading Heroes.

]]>