MQL5 TUTORIAL BASICS – 72 SIMPLE TRIPPLE EMA

video
play-sharp-fill

In this video we are going to code an expert advisor for this triple exponential moving average, so let’s find out how to do that with mql5.
To get started 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 triple ema, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
We start with an include statement to include the file trade dot mqh. This one comes with mql5 and now we use the class ctrade and create an instance called trade that we are going to use later on to open positions.
We want to calculate the ask price for buy positions 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 automatically calculate the right number of digits behind the dot.
Let’s repeat that for the bid price. Everything is identical except for the variable name and this time we are going to use symbol underscore bid as the second parameter.
Afterwards, we use the function mql rates to create an array called price info and with array set as series, we are going to sort the array downwards from the current candle and finally, we can use copy rates to fill the array for the current symbol on the chart and the currently selected period on that chart. We start with candle zero – that’s the current candle – and copy the price data for three candles to store it inside of our price info array.
Let’s create a string variable called signal for our signal but we are not going to assign any value here because this is what we want to calculate now, therefore we create three arrays for each of the exponential moving average values.
This one is for ten candles, the second one is for fifty candles and the third one is for one hundred candles. The integrated ima function that comes with mql5 can be used to create an exponential moving average for the current symbol on the chart, we use underscore period to calculate it for the current period. In this example I have done it for ten candles, we don’t use a shift value here and we want to create an exponential moving average, therefore we use mode underscore ema and the result should be calculated based on the close price.
Now let’s repeat the whole thing for the other two exponential moving averages.
Everything is identical except for the name for the variable and the number of candles. This is the last one for one hundred candles. Those arrays also need to be sorted from the current candle downwards so let’s repeat that for all the three arrays and as you might have guessed we use copy buffer to fill each array with data according to the definition that we have created. We do that for buffer zero – that’s the signal line – starting from candle zero and in this case, we do it for ten candles. Please make sure that you use different names for the ema and for the ema arrays here.
And now we can calculate if we have a buy signal and that would be the case if the exponential moving average for ten candles is bigger than the one for fifty candles and if the exponential moving average array for fifty candles is bigger than the one for one hundred candles if that is the case we have a buy signal and that’s when we want to assign the word buy to our signal.
In the other case, if we have a moving average for ten candles that is below the one for fifty candles and if the one for fifty candles is also below the one for one hundred candles, that’s when we want to sell, so now we assign the word sell to our signal and if our signal equals sell and the return value for positions total is below one – that would mean we have no open positions but a sell signal and that’s when we use trade dot sell to sell ten micro lot.
In the other case, if the signal equals buy and we also have no open positions that’s when we use trade dot buy and buy ten micro lot.
Finally, we use the comment statement to create an output on our chart. We want to see the current signal is followed by the calculated signal.
Okay, 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 this basic video series 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, 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 click on view, strategy tester or press control and r, please pick the new file simple triple ema dot ex5, mark the option for the visual mode here and start a test.
Here we are! The expert advisor is running, this is our first position and we see a sell signal on the chart, now we have a crossover and in a few seconds we should get a buy signal, that happened now so our expert advisor is working as expected and in this little video you have learned how to create an expert advisor for triple exponential moving average and you have coded it yourself with a few lines of mql5 code.