MQL5 Tutorial – Simple Adaptive Moving Average Robot


Okay, this time we are talking about an Adaptive Moving Average Indicator.
It was created (I think) by Perry Kaufman and you can use it to find entries or exits like you could with a simple moving average.
Now if that is almost the same, why should we prefer to use an Adaptive Moving Average?
Well Perry J. Kaufmann created this fascinating formula here for a reason. The Adaptive Moving Average has less lagging and generates less false signals. Let’s see if that is true.
Now how can we create an Adaptive Moving Average Expert Advisor for Meta Trader 5?
First please click on the little button here, that should bring up the MetaEditor.
And here you want to select File, New, Expert Advisor from template, Continue and we will call that Simple Adaptive Moving Average.
Now click on Continue, Continue, finish, and you can remove everything above the Ontick function here and I will also delete the two comment lines.
So let’s create an array that will hold our prices, I will call it my price array and it is a double array because double and float variables can hold floating point types.
And the adaptive moving average has six digits behind the dot and now we want to use the integrated MQL5 function iAMA for the current symbol on the chart and the current period, we want to calculate our prices based on the last nine candles.
The Fast Moving Average has a value of two because if you Insert, Indicators, Trend, Adaptive Moving Average, you will see the values here, these are the nine candles.
The fast EMA, that’s an Exponential Moving Average, is calculated for two candles and the slow EMA is calculated for thirty candles.
And we do not need any shift, so we will use the exact same values here: nine candles, two candles for the fast EMA, thirty candles for the slow EMA.
Zero for no shift and I would like to have the values calculated based on the close prices because the close price is also the default setting here.
Okay let’s move on and sort the price array from the current candle downwards by using the array set as Series function for the price array we have created here.
And now we want to fill it with data, that is done by using Copy Buffer for the Defined Moving Average, that’s the Adaptive Moving Average Definition we have created in this line.
We just need one line, that’s the zero here, and we want to copy values from the current candle that’s candle zero for three candles that’s the three here, and we want to store the results in our price array.
And now what we have done that, we want to get the value of the current candle, that’s candles zero of our price array and we use the normalized double function to get six digits behind the dot and the calculated value should be stored in a variable that is called Adaptive Moving Average Value.
And the last step is to create a chart output by using the comment function.
It will output the text AMA value and the calculated value for the Adaptive Moving Average Value we have extracted here.
Okay that’s it, now please click the Compile bottom and you should get no errors and no warnings here so now you can click the button or hit the F4 key to go back to MetaTrader.
And in MetaTrader you want to click on View, Strategy Tester, and in the Strategy Test panel you want to select the Simple Adaptive Moving Average.ex5 file.
Select any currency pair you like and please make sure to enable the check mark for the visualisation option before you start your test.
And here we go! if you move your mouse to the current value here, you’ll see that we have a value of 1.544031, that’s exactly what we have calculated here.
And in your Expert Advisor you would now compare the value of the adaptive moving average to the current price.
And if the price is above the moving average you would buy, and if the price is below the moving average you would sell.
Okay now you can create an Expert Advisor that uses the Adaptive Moving Average Indicator to find out if you want to buy or if you want to sell and you have created it in less than five minutes with a few lines of MQL5 code.

Download “MQL5 - Simple Adaptive Moving Average Robot”

SimpleAdaptiveMovingAverage.txt – Downloaded 471 times – 762.00 B