In this video we are going to create an expert advisor that is able to calculate signals for three exponential moving averages, so let’s find out how to do that with mql4.
To get started please click on a little icon here or press F4. 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 by creating a signal variable that is a string variable but we don’t assign any value here because we are going to calculate that later.
So let’s start with the first exponential moving average. I will call this one ema ten. To calculate it we use the integrated ima function that comes with mql4, for the current symbol on the chart and the currently selected period on that chart. I would like to calculate the exponential moving average for the last ten candles, I don’t use a shift value and I’m going to use mode underscore ema that stands for exponential moving average. The result should be calculated based on the close price for the current candle that is candle zero.
Now let’s repeat the whole thing for the next moving average. This one is for fifty candles so it is called ema fifty and the only difference is that we are going to use fifty candles instead of ten candles and we also use a different variable name here. So let‘s repeat that one more time this time for the ema one hundred that is calculated for one hundred candles.
Now that we have done that we are able to calculate the buy signal and that would be the case if the ema ten is bigger than the ema fifty and if the ema fifty is also bigger than the ema one hundred. If all these conditions apply we consider that to be a buy signal so we assign the word buy to our signal.
Otherwise, if the ema ten is below the ema fifty and the ema fifty is also below the ema one hundred we would consider that to be a sell signal and now we assign the word sell to our signal.
So if our signal equals buy and the return value for orders total is zero – that would mean that we have no open orders and a buy signal and in that case, we use order send to buy ten micro lot.
Otherwise, if the signal equals sell and we also have no open orders we use order send to sell ten micro lot.
Finally, we create a chart output that is done by using the comment statement. This will output the text, the current signal is followed by the calculated signal.
So far so good.
Well 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 you want to buy the premium course. You can find that one on our website.
For now, please click on the compile button or press F7 on your keyboard, you shouldn’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 you want to pick an empty chart and click on insert, indicators, trend, moving average. We start with a blue moving average for ten candles, the method should be exponential and we want to calculate the result based on the close price, so let’s click on okay and here is the first moving average.
Now let’s repeat that insert, indicators, trend, moving average, pick fifty candles, change the color and click on okay, here is the second moving average and one more time, this time for hundred candles, let’s make this one yellow, click on okay and here we are.
Now right-click into the chart, select template, save template and save it as tester dot tpl, you can override the current value because tester tpl is what we will see in the next backtest so if you don’t see the strategy tester here please click on view, strategy tester or press control and r, please choose the new file simple triple ema dot ex4, mark the option for the visual mode and start your test.
Here we are! The expert advisor is running, so let’s see if we can create a crossover situation here, I will fast-forward a little bit and in a few seconds when we have a crossover here we should see a buy signal that happened right now, here is our first trade so our little expert advisor is working as expected and in this little video you have learned how to create an expert advisor for three exponential moving averages and you have coded it yourself with a few lines of mql4 code.