MQL4 TUTORIAL – 131 STANDALONE EMA MACD EXPERT ADVISOR

video
play-rounded-fill

 

In this video we are going to create an expert advisor that is able to use two exponential moving averages to create a Buy or a Sell signal. And we are going to use the MACD as a filter. So let’s find out how to do that with MQL4. To get started, please click on the little icon here or Press F4 on your keyboard. Now you should see the Metaeditor window and here we have the precoded solution. In my case, I’m doing an include function for the Exponential Moving Average Crossover module and for the MACD module.

 

Both are separate files and the include function can import the content of those files. And they will be compiled when you compile the main module, which is the one with the Ontick function. And here we create a string variable for the trading signal and another string variable for the filter. And to get the trading signal we are opening a function that is called CheckEntry EMA. And to create a filter signal we open a function that is called Check Entry MACD. And when we have a Buy signal and the filter for the MACD also says that we should buy and if the return value for Orders total equals zero, that would mean all conditions are good for a Buy signal.

 

And that’s when we use Order Send to open a Buy Trade for ten microlot. Otherwise, when the trading signal equals Sell and when the filter also equals Sell and we have no open positions, that’s when we use Order Send to open a Sell Trade for ten microlot. Finally, we use the comment statement to output the trading signal and the filter signal on our chart. That’s it for the main module. Now let’s continue with the trading signal. Here we have a separate module. It’s an MQ4 file in the same directory and the function we use is called Check Entry EMA. Inside of the function, we first need to create a string variable for the signal, but we don’t assign a value here. Now we need to create four exponential moving averages. The first one is for 20 candles for the current candle.

 

The second one is for 50 candles also for the current candle, we also want to calculate the values for the old moving average for 20 candles. That’s for candle one, and we want to calculate the value for 50 candles for the last candle. That’s what we do here. The number of candles is defined here. If you don’t know what all that here means, you maybe want to watch one of the other videos in our basic video series, or maybe even the premium course might be interesting for you. You can find that one on our website. And if the moving average for 20 candles for the current candle is above the moving average for 50 candles, and if it was below for the candle before, that’s when we have a Buy signal and that’s when we assign the word buy to our signal. Otherwise, if the moving average for 20 candles is below the moving average value for 50 candles and if it was above for the candle before, that’s when we have a sell signal. And now we assign the word sell to our signal. Finally, we use the return statement to return the signal to our main module. That’s it for the signal. Now let’s do the same for the filter.

 

We want to open a function that is called checkentry MACD. This is the function and inside of the function we create a signal, also a string variable. And the MACD actually has two signals. One is the main signal and the other one is called signal. You can see that when you open the MACD window. We have a red line in this grey area here. To output the values we use the print statement for the main value and for the signal value for the MACD. And if the MACD main is above the MACD signal value, that would be a buy signal for us. In the other case, if the MACD main is below the MACD signal that would mean we have a sell signal.

 

And finally, we want to return the signal to our main module here. So this is it. Once you have all three modules in place you can click on the compile button. Here we are. And if you don’t have any errors you can click on the little button here or press the F4 button to go back to MetaTrader. Now here is our first sell trade. That was too fast. We couldn’t see anything. But now the MACD says we have a buy signal and when we see the next cross over here we should see that the next position is opened. So let’s speed that up a little bit. Here we have a sell position. So the expert advisor seems to work. Now let’s go to full speed for a few seconds. You see that we have several positions that are opened. Now let’s pause the whole thing and click on the graph tab.

 

We have a few wins here. So it looks like our expert advisor works as expected. And in this little video you have learned how to create an expert advisor that is able to calculate buy and sell signals by using an exponential moving average. And you also can use a filter for the MACD signal and you have coded it all yourself with a few lines of MQL4 code.