MQL TUTORIAL – SIMPLE BACKTEST COMBINED OPTIMIZATION

video
play-sharp-fill
In this video we are trying to find out how we can test two different values – you see we have a profit value and a stop loss value here – and we want to find out how to test different values in an advanced back-testing series in the Strategy Tester.
Our Expert Advisor will have two different values that can be changed without recompiling the code and we are going to test several combinations.
To do that please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleCombinedOptimization“, click on “Continue”, “Continue” and Finish.
Let’s remove everything above the “OnTick” function and two comment lines.
First we create an instance of “CTrade”, to do that we use the “include” statement for the “Tarde.mqh” file and the instance for our “CTrade” class will be called: “trade”.
In this simple example we will test two variables; one for the profit, the other one for the stop loss value. The input modifier defines external variables and as you see they can be changed only from the program properties window. I have used the default value for the take profit and the stop loss here.
Let’s get the Ask and the Bid price that is done by using “SymbolInfoDouble” for the current symbol on the chart, “SYMBOL_ASK” will deliver the Ask price, “SYMBOL_BID” will deliver the Bid price and with “NormalizeDouble” and “_Digits” we get the right number of digits behind the dot.
We also will need to have the balance and the equity to see if we make a loss or a profit, both values can be calculated by using “AccountInfoDouble”, “ACCOUNT_BALANCE” will give us the balance, “ACCOUNT_EQUITY” will give us the equity and if we have no open positions – so if “Positions Total” equals 0 (zero) – we want to open a buy trade and a sell trade, each for 10 micro lot and we will use the stop loss values and the profit values.
At the end we will use the “Comment” function to create a chart output; this is how it looks like in the Strategy Tester and that’s about it.
This is a really simple example just to create anything that can be back-tested so please don’t use it on your real account, I promise you you will lose all your money because this is how the results look like, so let’s stop the Strategy Tester here. Now let’s compile the code and that worked without any errors, so now we go back to Metatrader by clicking on the little button here or pressing the F4 key.
In Metatrader you want to click on: “View/ Strategy Tester” or press CTRL and R and here in the “Strategy Tester” we are going to use the optimization; the “Fast generic based algorithm”, click on the “Inputs” tab, mark the “MyTakeProfitValue” here, go back to “Settings” and start a test and you will see that we have, one, two, three, four, five, six results here, now let’s go back to the “Inputs” tab, also mark the “MyStopLossValue” and repeat the test and this time it’s going to do 48 test.
The reason is that we have two values and now our back-testing will check out all the possible combinations. I started with a value of 15 for the “MyTakeProfitValue”, I wanted to stop the optimization at 25, the step value is 2 – so it will test every other value – you see these are the values for 15, the next one is for 17. For the “MyStopLossValue” the step is 15 so if I sort by that you will see that it goes from 150 to 165 all the way out to 255, that’s because 250-150 = 100 and 100 cannot be divided by 15 without a remainder so Metatrader has no other choice to go from 240 to 255.
Now I have changed the values, we are going to test from a start value of 150 up to 160 for the take profit and the stop loss value that will be 121 combinations so let’s go back to “Settings”, restart the test – this will take a few seconds – and here are our results!
Now you could export everything to an xml file for your spreadsheet calculation, let’s do that here and this is how it looks like.
Okay. Now you know how you can test several different variables in a single back-testing session and you can create your testing code with a few lines of MQL5 code, just use the input modifier for each value you want to test.

Download “MQL5 TUTORIAL - SIMPLE COMBINED BACKTEST OPTIMIZATION”

SimpleCombinedOptimization.txt – Downloaded 689 times – 1.02 KB