This time we are going to calculate the Adaptive Moving Average Indicator; this is the red line you see on the chart. Whenever the candles are above the line we have a buy signal, otherwise if the candles are below the red line this is a sell signal.
Now we are going to find out how to create an Expert Advisor in MQL5 that is actually going to trade the Advanced Moving Average.
To do that please click on the little button here or press F4 in Metatrader and now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleAMAStandaloneEA” (Expert Advisor), click on: “Continue”, “Continue” and “Finish”.
Now let’s delete everything above the “OnTick” function and please also remove the two comment lines.
We start by including the file: “Trade.mqh”, that file contains a few useful functions and now we can create an instance of “CTrade” that will be called: “trade” and we are going to use it to open positions later on.
First we calculate the Ask price and the Bid price by using “SymbolInfoDouble” for the current symbol on the chart, “SYMBOL_ASK” will deliver the Ask price, “SYMBOL_BID” will deliver the Bid price and with “NormalizeDouble” and “_Digits” we automatically calculate the right number of digits behind the dot because this currency pair would have 3 digits while this one has 5 digits and this function (_Digits) is going to handle that.
We create an array for the prices by using “MqlRates”, let’s call that array: “PriceInfo”, now we sort the price array (PriceInfo) from the current candle downwards by using the “ArraySetAsSeries” function, we use “CopyRates” to fill the array with price data for the symbol that is currently selected on the chart and the current period on the chart. We start with candle 0 (zero) for 3 candles. Candle 0 (zero) is the current candle.
Let’s create a string variable for the signal, we will call it: “signal” and we don’t assign a value here that is going to be calculated now.
We need another array (myPriceArray), this one for several prices that contain the Indicator values; the one here was just for the candle prices (PriceInfo).
Let’s define the properties for the Adaptive Moving Average value (AdaptiveMovingAverageValue) that is done by using the “iAMA” – it’s an included function in MQL5 – once again for the current symbol and the currently selected period on the chart. You will see three values here; 9, 2 and 30 and when you right click the Indicator and select “Properties” you would see that is calculated for 9 candles, the “Fast EMA:” is 2, the “Slow EMA:” is 30 and the “Shift:” is 0 (zero), that’s exactly what we are using here and the calculation will be based on the close prices.
Now let’s sort the second array from the current candle downwards, once again we use “ArraySetAsSeries”, with “CopyBuffer” we fill our price array (myPriceArray) according to the Adaptive Moving Average definition (AdaptiveMovingAverageDefinition) we have created here for the first buffer – that’s the first signal – from candle 0 (zero) for 3 candles and store the result in our price array (myPriceArray).
To get the value for the current candle we just look at candle 0 (zero) in our price array (myPriceArray) and with “NormalizeDouble” and the 6 we calculate the current value, as you can see here it has 6 digits behind the dot and if the Adaptive Moving Average value (AdaptiveMovingAverageValue) is above the close price for candle 0 (zero) – that’s actually a shortcut for the current price because close prices are always the last price for a finished candle and candle 0 (zero) isn’t finished so the close price will deliver the current price here – and if this expression is true we want to assign the word: “sell” to our signal.
In the other case if the Adaptive Moving Average is below the current price that’s a buy signal and now we assign the word: “buy” to our signal. And if the signal equals sell and “PositionsTotal” is below 1 – that means we don’t have any open positions – 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” to buy 10 micro lot.
Finally we use the “Comment” function to create a chart output that will say: “The current signal is:” followed by the calculated signal.
So far so good, please click on the “Compile” button or press F7. I don’t have any errors and I don’t have any warnings so now we can click here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, here we select the: “SimpleAMAStandaloneEA.ex5” file, enable the visualization mode here and start a test.
…and here we are! Our Expert Advisor is creating buy and sell signals, we already have an open position here and in this little video you learned how to code an Expert Advisor that is actually going to trade the Adaptive Moving Average Indicator and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 TUTORIAL - SIMPLE ADAPTIVE MOVING AVERAGE EXPERT ADVISOR”SimpleAMAStandaloneEA.txt – Downloaded 393 times – 1.81 KB