Four Simple Steps to Backtest and Optimize Your EA on MetaTrader 5
You’ve got an algorithm. You think it’s going to be great. Now you’re thinking to yourself, “Let’s test it out!”
I couldn’t agree more!
One the best features about MetaTrader 5 is it’s built in backtester — and it’s super simple to use!
In this article, I’ll show you how.
About This Series
In this series, I cover everything you need to build your own advanced Expert Advisor from scratch. I cover:
- How to get started
- Using built in technical indicators from MetaTrader
- Making your EA autotrade
- Adding some simple-yet-powerful risk management techniques to your EA
- Backtesting and Optimizing your soon-to-be-awesome algorithm
- Adding some advanced features
All of the code that I use is available on my public GitHub repo, and each episode contains working code snippets you can immediately use in your own trading bot.
If you’d like some more general help, join our Discord server here, and if you prefer content in a video format, you can check out my YouTube channel here.
GitHub Code
You can find all the code for this series on my GitHub
About This Episode
In this article, I show you how to use the built in back tester from MetaTrader 5.
What You’ll Need to Complete This Episode
- A working MT5 terminal
- A working EA. Check out the previous episodes of this series if you’d like to learn how to build one.
Backtesting vs Optimization
Before showing you how to use the MT5 back tester, I want to clarify a couple of points that often cause confusion.
- Backtesting. Back testing refers to running a test of an algorithm on historical data.
- Optimization. Optimization refers to running multiple tests of the same algorithm using different input parameters with the goal of figuring out the set of parameters which potentially delivers the optimum outcomes you are targeting.
Historic Data Does Not Equal Future Results
It’s worth stating this clearly: Backtesting does not guarantee future results, and neither does a thorough optimization process.
Instead, backtesting is just a simple way of getting a higher level of confidence about your Expert Advisor.
Four Simple Steps
Let’s get into backtesting!
Step 1: Open the Strategy Tester
You can find the strategy tester in View -> Strategy tester
Step 2: Set Up Your Settings
In the Settings tab of the Strategy Tester, you can set up your test in whatever way suits you. I’ve listed out each item below for you:
- Expert. This is where you choose which Expert Advisor you will be using for your test. If you’ve been following this tutorial series, it will be
rsi_trading_bot_tutorial
- Symbol. This is the asset you will be trading, along with the timeframe. You can choose any asset from the list that your broker provides.
- Date. This is the period of time you would like to run your backtest for.
- Forward. This allows you to ask MT5 to optimize your parameters for the first half of a test, then test those optimized parameters on the second half of the test.
- Delays. This allows you to model different types of execution stages on your algorithm. For instance, if you want to see what happens if your trade takes 100ms to execute, you can do this.
- Modelling. This allows you to model how you want the backtest to be performed. For instance, you can do it on every tick (i.e. just like real life), or you could do it on every one-minute candle. Using something like the one minute candle is faster, but potentially less accurate.
- Deposit. This allows you to set a custom deposit amount to test with.
- Leverage. This allows you to choose what leverage you will use.
- Optimization. This allows you to choose how you would like to optimize your parameters. More on this in a moment.
Go ahead and select what you’d like to choose now.
If you’re following along this series, for now, choose the following values:
- Expert.
rsi_trading_bot_tutorial.ex5
- Symbol.
XAUUSD
, Timeframe.M1
- Date.
Last year
- Forward.
No
- Delays.
Zero latency, ideal execution
- Modelling.
Every tick
- Deposit.
100000
(because why not. Lol),USD
- Leverage.
1:200
- Optimization.
Fast Genetic Based Algorithm
, set toBalance Max
Step 3: Set Up Optimization
Optimization in MT5 is primarily handled through the Inputs
tab in your Strategy Tester
. However, before I get into that, I’ll quickly explain your optimization options from the previous step.
Put simply, once you get into optimization, it’s quite common to have literally tens or hundreds of thousands of different possible combinations to test. This can put enormous strain on your compute and time resource, so MT5 offers you a few choices to help you navigate the task:
- Slow complete algorithm. This will test literally every combination. Even the ones that don’t make sense.
- Fast genetic based algorithm. This will attempt to identify a range of combinations that are most likely maximize your preferred outcome. Note the words ‘most likely’. It will not always be right.
- All symbols selected in market watch. This will answer the question “I wonder if this algorithm works on xxxxxx.
Ok, back into the actual terminal.
We’ll be optimizing all of our parameters execept for Lot Size. Watch the counter for the number of tests to see how the quantity of tests can quickly skyrocket! I’ll track mine as I update them here.
- Use live RSI price? Total count: 2
- RSI High Threshold. I selected a range of 60 to 80 for the RSI, giving me a total count of: 42
- RSI Low Threshold. I selected a range of 20 to 40 for the RSI, giving me a total count of: 882
- Take Profit in Pips. I selected a range of 10 to 40 for the TP, giving me a total count of: 27,342
- Stop Loss in Pips. I selected a range of 10 to 40 for the SL, giving me a total count of: 847,602
If I was to use the ‘Slow complete algorithm’, my MT5 estimated that this would take80-83 hours!!!
83 hours to backtest!!!
In contrast, if I was to use the Fast Genetic based algorithm
, this goes down to around 42 minutes.
Note. For reference, this was a 13th Gen Intel(R) Core(TM) i5–13600K 3.50 GHz with 64Gb Ram.
Anyways, run your optimization and let me know how it goes.
Step 4: Review Results
Once you’ve run your backtest with optimization, you can check out your results.
To do this, head to the Optimize Results
tab, and start looking around.
For instance, using my test, I was able to see that the best result I had over a year was -$720.14
. Pretty annoying.
Each result will has the parameters listed out for you.
Wrapping Up
Congratulations on getting your backtest done. I’d be super interested in hearing what results you got for your algorithm, so drop them in the comments!
You might be wondering:
Ok, how do we start levelling up this Expert Advisor on MT5? Check out my next article to learn how!
List of Articles in this Series
- Build Your Own AutoTrading MetaTrader 5 Expert Advsior
- The Easiest Way to Add the RSI Indicator to Your MetaTrader 5 EA
- The Easiest RSI Algorithm for Your MetaTrader Expert Advisor
- The Easiest Way to Place BUY and Sell Orders with your MetaTrader EA
- The Simplest Way to Manage Your Expert Advisor Trades on MetaTrader
- Four Simple Steps to Backtest and Optimize Your EA on MetaTrader 5
Connect With Me
My name is James, and you’ll often see me online using the AppnologyJames handle. Although my background and training is in Cyber Security, I’ve spent the last few years building trading algorithms for clients, along with a powerful market analysis platform called TradeOxy.
I love hearing from my readers and viewers, so feel free to reach out and connect with me:
- On YouTube
- Through TradeOxy
- On my Discord Channel
- On LinkedIn