A YouTube subscriber with the name Laurent ZB wanted me to create an Expert Advisor where two exponential moving averages cross. So now I create the video.
Okay what you see here is a crossing of two exponential moving averages, this one is calculated for twenty candles and this one is calculated for fifty candles.
And the idea is, that if the twenty candles exponential moving average crosses the fifty candle exponential moving average from below, you should buy.
In the other case that the twenty candle moving average crosses the fifty candle moving average here, you should sell.
And in this situation it is likely that the two moving averages will cross soon.
Now that they have crossed each other you can see, there is a buy signal in the left upper corner of your chart.
In the best case this crossing could be the optimal point to go short, and this is where you would close your trade.
In the other case you would buy here, go long and you would close your trade here.
This is very easy to understand, but if the market is going sideways this indicator might produce several false signals as every other indicator does.
But if everything is going well, you would have sold here and it goes all the way down until the next crossing happens here.
Now how can we create a crossing moving average Expert Advisor for MQL 5?
As always please click on this little button here or press the F4 to bring up the Meta Editor, and we will create a new file from template, it will be an Expert Advisor, continue.
We will call it simple EMA crossover. This is exponential moving average, continue, continue, finish and remove everything above the OnTick function.
We also delete the comment lines here and the first step is to create two arrays, we will call them my moving average array one and my moving average array two.
Now we want to define the properties for the first EMA by using the IMA function for the current currency pair on the chart and the currently selected time period on my chart.
And we want to calculate twenty candles, we don’t want any shift to the left or to the right and we want to use the mode EMA, that’s an exponential moving average.
For a simple moving average we would use mode SMA and we want to calculate based on the close prices of the last twenty candles.
For the second exponential moving average we will do all the same, but we want to calculate fifty candles and now for both of our arrays, we want to use arraysetasseries for my moving average array one and my moving average array two.
To sort the price array from the current candle downwards, and with the function copy buffer we will fill my moving average array one and my moving average array two according to our definition that we created here.
This zero stands for only one line. This zero is for the current candle and we want to copy the prices of three candles.
Okay let’s calculate if we have an entry signal, because if my moving average array one for the current candle is greater than my moving average two for the current candle, and if the last value for the candle before was lower than the last value of my moving average array two then we have a crossover because conditions changed from candle one to candle zero.
Candle zero is always the current candle and candle one is the candle before and if this is the case we want to use the comment function and it will output the word “buy” on the left upper corner of the chart screen.
And in the other case if my moving average array one for the current candle is now below my moving average array two for the current candle, and if it was above my moving average array two for the last candle, then I want to output the word “sell” on the left upper corner of the chart.
Okay so let’s compile that Expert Advisor, click this little button over here or hit the F4 key to bring up the MetA Trader. Click on View strategy tester and select the simple EMA crossover.ex5 file:
Mark the visualization option here and start your engine. And here we go… no crossing so far but it should happen very soon.
Let’s speed that up a little bit here and here is our crossing and it says buy and it would have gone all the way up until the next crossing will happen.
So now this is the case and now it is saying “sell”. So now you know how you can create a crossover entry signal with MQL 5 for two exponential moving averages and you have done it with this little Expert Advisor.