MQL5 TUTORIAL BASICS – 69 SIMPLE PRICE CORRELATION

video

In this video, we are going to calculate a moving average for two currency pairs and create an expert advisor to find out if we have a correlation.
To do that please click on a 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 file, expert advisor from template, continue, I will call this file, simple currency correlation, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here.
We start by calculating the ask price for the current currency pair that can be done by using symbol info double for the current symbol on the chart, we use symbol ask and underscore digits and normalize double to calculate the current ask price and the right number of digits behind the dot.
So far so good!
But now we want to know the ask price for another currency pair.
And to calculate that one I do almost the same except that I have hard-coded the currency pair for the first parameter. To make that work for your account you need to check if the currency pair is exactly shown as we have hard-coded it because some account type might use suffixes, for example, my RoboForex cent account would show the suffix dot m and if that is the case you would need to hard-code it here. Please notice this is just an example, usually, you wouldn’t hard code anything but in this little video, we just want to show the concept.
So let’s continue with the simple moving average.
First, we need to create an array, afterwards, we create a definition for the moving average by using the ima function for the current symbol on the chart – I have also hard-coded the time period here, I use period underscore m1, if you mark that and press F1 you will see that this one stands for one minute, half an hour would be period m thirty and so on.
I want the calculation for the result based on the last twenty candles for the current candle without any shift values, it should be a simple moving average so I use sma and the result should be calculated based on the close price.
Now we use copy buffer to fill the moving average array according to the definition that we have created here for buffer zero, that’s this line here, we start with the current candle zero and we want to copy data for three candles and store it in our array. And to calculate the value for the current candle we just have to look inside of the value for candle zero in our moving average array and assign it to our variable my moving average twenty.
A little hint here, I usually used a float type to save some memory and to cut a few digits behind the dot but that might cause a little problem after one of the recent updates I would recommend to stick with the original variable types, so this is how we calculate the moving average array for the current currency pair and now we are going to repeat that for the second currency pair, it’s very similar but this time we are going to hard-code the currency pair as we did for the ask price, everything else is the same except for the variable names.
And once again to calculate the current value for the Australian against the US dollar we look into the value for candle zero in our moving average array.
Finally, we want to create a chart output by using the comment statement, this one will output the ask price for the current currency pair and the ask price for the second currency pair followed by the current moving average value and the moving average value for the second chart.
Well, if this was too fast for you or if you don’t understand what all the code here does, maybe you want to watch one of the other videos in this basic video series or maybe even the premium course on our website might be interesting for you. By the way, this was again a question from a premium member and if you are already a premium member and want to make a suggestion for one of these videos, please let me know. For now, please click on the compile button or press F7, I don’t have any errors here and if this is also true for you please click on a little button here or press F4 to go back to Metatrader.
And in Metatrader we want to open a new chart for the currency pair Australian against US dollar, now please click on insert, indicators, trend, moving average, I would like to see a simple moving average, therefore I changed the method to simple, it should be calculated based on twenty candles, no shift value and the calculation should be based on the close price, so let’s click on okay, here is the simple moving average, now we right-click into the chart, select template, save template and save it as default dot tpl because this is the template that is going to be used when you open a new chart. You can replace the current one. We are not going to use the strategy tester because we cannot see the second currency pair in the strategy tester, but we can open another chart window and click on a little symbol here, now we see two charts and now we pick the new file, simple currency correlation and drag it on both charts, the values on the first chart should be identical because we compare the same currency pair. If you move the mouse to the end of the moving average you will see the current value and here in the second chart, you can see the current values for the Euro against the US dollar and the value for the first chart window. Please keep in mind that this was just a simple example and the values will only be updated when the price changes but in this little video you have learned to create a simple expert advisor that is able to compare the prices for two currency pairs and to calculate a moving average for both and you have coded it yourself with a few lines of mql5 code.