MQL5 Tutorial

MQL5 TUTORIAL BASICS – 69 SIMPLE PRICE CORRELATION


Listen Later

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,
...more
View all episodesView all episodes
Download on the App Store

MQL5 TutorialBy Raimund Bauer