In this video we are going to create a standalone Expert Advisor for the Double Exponential Moving Average Indicator; it’s the red line here and whenever it crosses our price it will produce either a buy or a sell signal and now we are going to find out how to create an EA that is able to actually trade the signals automated.
To do that please click on the little button here or press F4 in Metatrader.
Now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will give it the name: “SimpleIDEMAStandaloneEA”, click on “Continue”, “Continue” and “Finish”.
Now we can delete everything above the “OnTick” function. We start by including the file: “Trade.mqh” that is part of MQL5, now we create an instance of “CTrade” that will be called: “trade”, let’s remove the two comment lines here.
Inside of the “OnTick” function we calculate the Ask price and the Bid price by using “SymbolInfoDouble” for the current symbol on the chart, “SYMBOL_ASK” will give us the Ask price while “SYMBOL_BID” will give us the Bid price and with “NormalizeDouble” and “_Digits” we calculate the number of digits behind the dot because some currency pairs have 3 and others have 5 digits behind the dot.
Now let’s continue by using “MqlRates”, we create an array for the price data that will be called: “PriceInfo”, we sort the price array (PriceInfo) from the current candle downwards that is done by using “ArraySetAsSeries” for the price info array (PriceInfo) we just created and by using “CopyRates” we fill the array (PriceInfo) with price data, that is done for the current symbol and currently select a period on the chart, we start with candle 0 (zero) – that’s the current candle – and we copy the prices for 3 candles and store it in the price info array (PriceInfo).
We also need to create a signal that is a string type variable, this is an empty value because we need to calculate the value later on. Let’s create an array for the price data of the Expert Advisor that will be called: “MyEaArray” and we define the properties for the Double Exponential Moving Average by using the built in “iDEMA” function that comes with MQL5 for the current symbol and the currently selected period on the chart.
We will calculate the result based on 14 candles without any shift to the right or to the left and we use “PRICE_CLOSE” to calculate the results based on the close price.
Now when you click on: “Insert/ Indicators/ Trend/ Double Exponential Moving Average” you will see that we here also see the period of 14 candles, the shift value of 0 (zero) and it is applied to the close price so that’s what we use here.
Let’s sort that array also by using “ArraySetAsSeries” and use “CopyBuffer” to fill it according to the my moving average definition (movingAverageDefinition) that we have created here for buffer 0 (zero) – that’s the red line here – from the current candle 0 (zero) for 3 candles and store the values in our price array (MyEaArray).
Now we can calculate the Expert Advisor for the last finished candle – that’s candle 1 in our EA array – (MyEaArray) and we assign the result to the variable “myMovingAverageValue” and if that moving average value (myMovingAverageValue) is bigger than the close price for candle 1 in our price info array (PriceInfo) that would be a sell signal and in that case we assign the word: “sell” to our signal.
Otherwise if the moving average value (myMovingAverageValue) is below the close price for candle 1 in our price info array (PriceInfo) – that’s when we want to buy – so now we assign the word: “buy” to our signal.
So if our signal equals sell and “PositionsTotal” is below 1 – in other words we don’t have any open positions – that’s when we use “trade.Sell” to sell 10 micro lot.
Otherwise if the signal equals buy and we have no open positions we use “trade.Buy” and buy 10 micro lot.
In the last step we are going to use the “Comment” function to output the words: “The current signal is:” and the calculated “signal” directly on our chart.
Don’t forget the closing bracket here and when you’re done please click on the “Compile” button or press F7, that worked without any errors, so now we can click here or press a F4 to go back to Metatrader.
And in Metatrader we click on: “View/ Strategy Tester” or press “CTRL and R” here we pick the file: “SimpleIDEMAStandaloneEA.ex5”, mark the visualization option here and start a test.
…and here we go, our Expert Advisor is already trading and now you know how you can create an MQL5 EA that is able to actually trade the Double Exponential Moving Average signal and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 TUTORIAL - SIMPLE IDEMA STANDALONE EXPERT ADVISOR”SimpleIDEMAStandaloneEA.txt – Downloaded 187 times – 2 KB