**Introduction to Creating an EMA Crossover Expert Advisor**(00:00 – 00:13) Introduction to the tutorial on creating an Expert Advisor for two exponential moving averages (EMAs) crossing, as requested by a YouTube subscriber.**Explanation of EMA Crossover Strategy**(00:13 – 00:44) Explaining the strategy where a buy signal is generated when a 20-candle EMA crosses a 50-candle EMA from below, and a sell signal when it crosses from above.**Demonstration of EMA Crossover on Chart**(00:44 – 01:36) Showing examples of EMA crossover on the chart and discussing potential buy and sell points, as well as the occurrence of false signals in sideways markets.**Creating the EMA Crossover Expert Advisor in MetaEditor**(01:36 – 02:38) Starting the creation of the EMA crossover Expert Advisor in MetaEditor, including setting up the basic structure and removing unnecessary lines.**Defining EMA Properties and Arrays in Code**(02:38 – 03:40) Coding the properties for the two EMAs and creating arrays to store their values.**Sorting Arrays and Filling with EMA Data**(03:40 – 04:14) Using`ArraySetAsSeries`

and`CopyBuffer`

functions to sort the arrays and fill them with EMA data.**Calculating Entry Signals for EMA Crossover**(04:14 – 04:53) Writing the logic to calculate entry signals based on the crossover of the two EMAs.**Outputting Buy and Sell Signals on Chart**(04:53 – 05:26) Using the`Comment`

function to output buy and sell signals on the chart based on the EMA crossover.**Compiling and Testing the Expert Advisor in MetaTrader**(05:26 – 06:14) Compiling the Expert Advisor and testing it in MetaTrader’s strategy tester with visualization to observe the buy and sell signals.

Okay you have asked for it so here it is.

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.

Download “MQL5 Simple EMA Crossover Expert Advisor from MQL5 Tutorial”

SimpleEMACrossover.txt – Downloaded 973 times – 1.77 KB