In this video, we are going to create an expert advisor that is able to trade randomized signals on multiple currency pairs in one chart, so let’s drag the expert advisor onto a live chart, I need to allow auto trading, I click on okay and now we see that it is able to open multiple positions on one chart, so let’s find out how to do that with mql4.
To get started please click on a 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 multi-currency order, click on continue, continue and finish.
Now you can delete everything above the ontick function and let’s also remove the two comment lines here.
We start with a statement to calculate the ask price, this is very uncommon for mql4, it’s what we usually do in mql5 and to calculate an ask price you can use symbol info double, in this case for the current symbol on the chart, the second parameter here is symbol underscore ask – all in capital letters – and with normalize double and underscore digits I am going to cut the number of digits behind the dot to automatically fit the currency pair because we have currency pairs with three digits behind the dot and others with five digits behind the dot. And that’s what we do here.
Well usually we just would take the ask price as it is but in this case we are going to open multiple positions for different currency pairs and therefore we need to find a way to calculate the ask prices for the other currency pairs, in this case for the US dollar against the Canadian dollar and this is how we can do it, in this case, I have exchanged the first parameter for the currency pair I want to trade.
Please make sure that it matches what you see here in the market watch because some brokers use suffixes so let’s make sure that you get it right.
Now let’s repeat that for the second currency pair. This is almost the same, except for the name of the variable and the currency pair string.
Of course, we can do the same for the bid price, in this case, its symbol info double for the current symbol on the chart and we use symbol underscore bid and now we repeat that for the US dollar against the Canadian dollar. I have called this one USDCADBid and I have exchanged the first parameter and you might have guessed it we also do it here for the British pound against the US dollar.
Now that we have all those bid prices and ask prices let’s create a string variable called signal. We are not going to assign a value here because this is going to be calculated now.
In this case, we are going to use random entries, so I use mathsrand and gettickcount to create an initial value because gettickcount returns the number of milliseconds since we have started the system, this is a unique value and this whole expression will make sure that we do not get the same random values all the time and to calculate the actual random number we are going to use mathrand.
This looks almost like the one we used before except the s in the middle and mathrand is used to calculate pseudorandom integer values between zero and thirty-two thousand seven hundred sixty-seven and this suffix here, percent two will give us two random numbers, so whenever the random number equals zero we would consider that to be a buy signal, so we assign the word buy to our signal.
And if the random number equals one we consider that to be a sell signal so now we assign the word sell to our signal and whenever we have a buy signal and when orders total equals zero – that would mean we have a buy signal and no open orders – that’s when we are going to send three orders for the current symbol on the chart, the British pound against the US dollar and for the US dollar against the Canadian dollar.
In the other case if the signal equals sell and we also have no open orders I would like to use order send to sell for the current symbol, for the British pound against the US dollar and for the US dollar against the Canadian dollar.
Now let’s add a chart output by using the comment statement that will output the current signal and the current symbol we trade directly on our chart.
That’s about it.
If this was too fast for you or if you have no idea what all the code here does 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 on your keyboard, you shouldn’t get any errors 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 need to use a live chart. The problem is that this is one of the rare examples where mql4 cannot do what you could do with mql5. In mql5 you would be able to actually use the strategy tester to trade multiple currency pairs at one time.
In Metatrader 4 I am using a demo account to show you that it works, so let’s enable auto trading and here are our three positions, so our little expert advisor works as designed and in this little video you have learned how to open several positions for different currency pairs in one expert advisor on one chart and you have coded it yourself with a few lines of mql4 code.