MQL5 TUTORIAL BASICS – 78 SIMPLE MULTI CURRENCY ORDER

video
play-sharp-fill

In this video, we are going to create an expert advisor that is able to open positions for multiple currency pairs in the back-test, so let’s find out how to do that with mql5.
To get started please click on the little icon here or press the F4 key on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple multiple currency order, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
We will start with an include statement to include the file trade dot mqh, it contains the class ctrade and we create an instance called trade that we are going to use to open positions later on.
Inside of the on tick function, we start by calculating the ask price that is done by using symbol info double for the current symbol on the chart we use symbol underscore ask and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot.
Well as we are going to trade several currencies we also need ask prices for the other currencies, so let’s calculate the ask price for the GBPUSD. We also use symbol info double but this time the first parameter is not underscore symbol but the name of the currency pair.
You might have different names, so please make sure that you spell that correctly.
Now let’s repeat the whole thing, this time I would like to calculate the ask price for the currency pair USDCAD, so that’s what we use here and of course, we need to repeat the whole thing for the bid prices that is done by using symbol info double underscore symbol and symbol bid for the current currency pair and please change it here for the currency pairs you want to trade.
Now let’s create a string variable called signal, so far we don’t assign any value here because we are going to calculate that now.
For this simple example I am going to use a random entry signal, so let’s use mathsrand to create an initial value for the random calculation. We use gettickcount as a parameter because this function returns the number of milliseconds since we have started the system, so this will create a base value that we use to create our random values.
And this is done by using mathrand we just need two values, so we use percent two and if the random number that we have calculated equals zero we would assume that this is a buy signal, so we assign the word buy to our signal.
Otherwise, if the random number equals one that would be a sell signal and now we assign the word sell to our signal and whenever we have a sell signal and positions total returns a value that is below ten we would use trade dot sell to sell ten micro lot for each of our currency pairs here.
This expression here stands for the current currency pair, you could also use underscore symbol like we did here, basically, it stands for no value.
Let’s repeat the whole thing for the buy trades, so if our signal equals buy and positions total is below ten we use trade dot buy to buy ten micro lot either on the current chart or for the two other charts that we have defined.
And that’s about it.
If you have no idea what all the code here does or if this was too fast for you maybe you want to watch one of the other videos in the basic video series first or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button or press F7, I didn’t get any errors here and if this is the case you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader, we click on view, strategy tester or press control and r, please click on the file, simple multiple currency order dot ex5, mark the option for the visual mode and start a test.
Here is our expert advisor! We already have lots of orders and as you can see we have orders in different currency pairs in the strategy tester, so it’s possible to open positions for other currency pairs from right inside the strategy tester that might be interesting for hedging systems and now you know how to create an expert advisor that is able to trade several currency pairs in a single strategy test and you have coded it yourself with a few lines of mql5 code.