MQL5 Tutorial – Simple IDEMA Standalone Expert Advisor (00:00 – 00:02)
- Introduction to creating a standalone Expert Advisor for the Double Exponential Moving Average Indicator in MQL5.
Creating an Expert Advisor in MetaEditor (00:02 – 00:27)
- Instructions on opening MetaEditor in MetaTrader and creating a new Expert Advisor file named ‘SimpleIDEMAStandaloneEA’.
Initial Code Setup (00:27 – 00:51)
- Removing unnecessary lines and preparing the initial code setup in the MetaEditor.
Including Trade.mqh and Initializing CTrade (00:51 – 01:04)
- Including the ‘Trade.mqh’ file and creating an instance of the ‘CTrade’ class for later use in opening trades.
Calculating Ask and Bid Prices (01:04 – 01:36)
- Using ‘SymbolInfoDouble’ to calculate Ask and Bid prices and normalizing these prices with ‘NormalizeDouble’ and ‘_Digits’.
Setting Up Price Array (01:36 – 01:56)
- Creating a price information array and using ‘ArraySetAsSeries’ to sort the array from the current candle downwards.
Filling Price Array with Data (01:56 – 02:09)
- Using ‘CopyRates’ to fill the price array with data for the current symbol and selected period on the chart.
Creating Signal and Price Data Arrays (02:09 – 02:43)
- Creating a string variable for the signal and another array to hold price data for the indicator.
Implementing IDEMA Indicator (02:43 – 03:07)
- Using the built-in ‘iDEMA’ function to define the Expert Advisor based on the Double Exponential Moving Average indicator.
Filling Price Array with Indicator Data (03:07 – 03:50)
- Using ‘CopyBuffer’ to fill the price array with data according to the IDEMA definition.
Calculating IDEMA Value and Assigning Signals (03:50 – 04:45)
- Getting the value for the last finished candle and assigning ‘buy’ or ‘sell’ signals based on the IDEMA value.
Executing Trades Based on Signals (04:45 – 05:08)
- Using ‘trade.Sell’ or ‘trade.Buy’ to execute trades based on the calculated signal and the absence of open positions.
Displaying Signal on Chart (05:08 – 05:20)
- Using the ‘Comment’ function to display the current signal on the chart.
Testing the Expert Advisor (05:20 – 05:52)
- Instructions for compiling the code and testing the Expert Advisor in MetaTrader using the Strategy Tester.
Conclusion (05:52 – 06:09)
- Conclusion of the tutorial with a demonstration of the Expert Advisor in action, showing how it trades the Double Exponential Moving Average and outputs buy and sell signals on the chart.
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.