MQL5 TUTORIAL – SIMPLE DOUBLE EXPONENTIAL MOVING AVERAGE

video
play-sharp-fill

In this video, we are going to trade the Double Exponential Moving Average (DEMA) that’s basically a red line you see on your chart.
If you hover the mouse over the line you will see that the Double Exponential Moving Average (DEMA) has a value. You can find the indicator if you click on: “Insert, Indicators, Trend, Double Exponential Moving Average”.
The standard value is 14 candles, it has no shift and it applies to the close price.
The Double Exponential Moving Average can be called by using the MQL5 function: iDEMA.
The function only has one buffer, it takes the current symbol, the current period on the chart, the third parameter will hold a period. In our case it’s a 14.
The shift will be zero because we don’t want to move it to the right or to the left.
For the applied price, we will use the close price. You see that it is going to generate buy or sell signals on our chart and basically a buy signal will always occur when the Double Exponential Moving Average is below the close prices of our candles and when it’s above our close prices here we will generate a sell signal.
Now how can we create an Expert Advisor that is able to calculate the indicator?
To do that please click on the little button here or press F4 in your Metatrader and now you should see the Metaeditor window.
Here you want to click on “File, New, Expert Advisor (template)” from template. Continue.
I will call this one: SimpleDoubleExponentialMovingAverage. (Simple Double Exponential Moving Average) Click on continue, continue and finish.
Now everything above the “OnTick” function can be deleted and please also delete the two comment lines here.
Okay.
Let’s create an array in the first step. We will call it: “PriceInformation” and we use MqlRates to create it.
After that we are going to sort it from the current candle to the oldest candle. That is done by using the “ArraySetAsSeries” and now we use “CopyRates” to fill the array with price data for the current symbol and the period that is currently selected on the chart.
We will start with the current candle, that’s candle zero.
We only need prices for three candles and those candles go into our “PriceInformation” array.
Let’s create a second array!
This one is for the Moving Average and now we use the iDEMA function of MQL5 to define the properties for the Double Exponential Moving Average.
The result will be stored in an integer variable called: “movingAverageDefinition” and we use the function iDEMA for the current symbol and the currently selected period on the chart for 14 candles.
Let’s exchange this one to a zero, because we don’t want to use any shift value here and the Expert Advisor should be calculated based on the close prices.
Let’s also sort this array by using “ArraySetAsSeries” to sort the price array from the current candle downwards and once again we use “CopyBuffer” for the “movingAverageDefinition” we have created here for buffer zero, because the Expert Advisor only has one signal line.
We start from candle zero, that’s the current candle. We only need the values for three candles. You could use more but calculating three candles is much faster and the values will be stored in “myMovingAverageArray”.
To calculate the Expert Advisor for the last candle we select candle one from “myMovingAverageArray” and store the result in “myMovingAverageValue”.
Now that we have done that, we check if “myMovingAverageValue” is above the close price of candle one in our “PriceInformation[1]” array.
In that case we have a sell signal, so we use the “Comment” function to create a chart output for the sell signal. In the other case if “myMovingAverageValue” is below the close price for candle 1 – that would be a buy signal. So, let’s create a chart output that’s about it.
Please don’t forget the last bracket and when you’re done you can click on the Compile button here – oh I messed up this character here!
Let’s try it again, and this time it worked without any errors and no warnings so now we can click the little button here or press F4 to go back to Metatrader.
And in Metatrader you want to click on “View, Strategy Tester” or press CTRL and R on your keyboard.
Here you want to select the file: SimpleDoubleExponentialMovingAverage.ex5, mark the visualization mode here and start your test. And here is our Expert Advisor at work.
Now it says buy or sell depending on the current trend and now you know how to create a simple Expert Advisor that is able to calculate the Double Exponential Moving Average (DEMA) and you have coded it yourself with a few lines of MQL5 code.

Download “MQL5 TUTORIAL - SIMPLE DOUBLE MOVING AVERAGE” SimpleDouble Exponential MovingAverage.txt – Downloaded 544 times – 1.16 KB