In this video we are going to create an expert advisor that uses two exponential moving averages and the MACD as a filter to create, buy and sell signals. So let’s find out how to do that with MQL5. To get started, please click on the IDE button or press F4 on your keyboard. Here we have the pre-coded solution. We start by importing the file Trade.Mqh. This one comes with MQL5 and it provides a few functions for position management.
The next two include statements here are for the modules that we use to create the EMA crossover signal and the MACD filter signal. We use normalise double and symbol info double for the current symbol on the chart to calculate the Ask price by using symbol underscore Ask and the Bid price by using symbol underscore Bid.
And with normalise double and underscore digits, we make sure that we can calculate the right number of digits behind the dot. That is a different number depending on the currency pair you use . Now let’s create an instance of the class Ctrade. That will be called Trade and we are going to use it to open positions later on. Inside of the Ontick function we use MQL rates to create an array for the price data. And with array set as series we sort our price info array from the current candle downwards. And with copyrates we fill the price array with price data for the current symbol on the chart and the currently selected period on the chart. Starting from candle zero for three candles and we store the result in our price info array. To calculate the current price we declare a double variable and that will get the price info value for candle zero.
Actually we are going to use the close price and as candle zero isn’t finished so far, the close price is the current price. In the next step we are going to find out if we have a trading signal by using the function Check entry EMA. That’s the one in this module. And afterwards we use a filter signal and check the entry signal for the MACD. That’s the one in this module. And if the trading signal equals buy and the filter also equals buy and we have no open positions, that’s when we use trade buy to buy ten microlot. In the other case, if the trading signal equals Sell and the filter signal also equals Sell and we have no open positions, that’s when we use trade sell to sell 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 check the EMA crossover module.
Here we have a function called Checkentry EMA. First we need to define a string variable for the entry. Then we will create an array for the 20 candles moving average and for the 50 candles moving average to do that, we use the IMA function for the current symbol on the chart and the currently selected period on that chart. We do that for 20 candles or for 50 candles. We don’t use a shift value, so we set that to zero. We use Mode EMA for the close price and we repeat the whole thing here for the second moving average. Afterwards, we use array set as series for the moving average array for 20 candles, and we do that again for the moving average array for 50 candles. And with copy buffer we can fill our moving average array according to the definition that we have created above. The EMA only has one line, and we copy the values from the current candle for three candles and store them in the moving average array for 20 candles or below in the moving average array for 50 candles.
And if the moving average for 20 candles is currently above the one for 50 candles, and if it was below for candle one before, that’s when we have a Buy signal, and that’s when we assign the word Buy to our entry signal. In the other case, if the moving average array for 20 candles now is below the moving average array for 50 candles, and if it was above for candle one, that’s the candle before, that’s when we have a sell signal, and that’s when we assign the word sell to our signal. Finally, we return our entry signal to the main module. That’s the one with the Ontick function here. Now let’s check out the MACD filter. Here we have a function called Check Entry MACD. We also create a string variable for the signal. Once again, we need two price arrays, and we use the function IMACD for the current symbol on the chart and the currently selected period on that chart. These are three standard parameters. You can check them out when you open the MACD on your chart, and we want to use the calculation based on the close price.