In this video today we are going to learn about the Adaptive Moving Average. It almost looks like the Simple Moving Average – as you can see the red line here on the chart, but the Adaptive Moving Average (AMA) was created by Mr. Perry J Kaufmann with a purpose of getting less false signals.
In the present video we want to build an entry module which can be used by the main module for placing trades. My main module is the Forex Trading Framework and we generate buy and sell signals based on the calculations done by the Adaptive Moving Average.
Let’s begin coding by starting the Metaeditor with the button here or by hitting the F4 key.
Here you see my framework. The framework contains several functions which can open, close and manage trades.
First we will create a new file, which we will call: “CheckEntry_AMA.mq5”. We shall then use the function: “CheckEntry” which will return a signal to the main module.
We base our calculations on new ticks, so when a new tick comes in we pass the signal to the “CheckForTick” function, this will either trigger a new trade or skip till conditions are met for opening a trade.
Let’s begin by clicking on “File/ New/ Expert Advisor” from template (template), click “Next”, let’s use the name: “CheckEntryAMA”, click on “Next”, “Next” again then “Finish”. Please remove everything above the “OnTick” function including the two comment lines.
This one needs a little adjustment. As we begin with the actual code we first create an array which will hold the values of the prices. We use the function “MqlRates” and we name our array: “PriceInfo[]”. Next we continue with a function: “ArraySetAsSeries” and we sort the price array in descending order starting with the current candle and downwards. With our next function: “CopyRates” we fill the price array (PriceInfo[]) with price data for the current symbol and the currently selected period on the chart.
Beginning with the first bar the expression here is going to give us the values of all the bars on our chart and it will store the result in the array called: “PriceInfo[]”.
Now let’s continue with the signal, for that we use a string variable (type string), we also name it signal. We leave it empty without any value for it shall come from the Adaptive Moving Average Indicator’s calculation and it will be done later in the code.
First we need a second array; this will hold the price data for the Indicator. We call it:
“myPriceArray[]”. It is of the data type double so it stores floating type values.
Okay, now we need to get the properties of the Adaptive Moving Average Expert Advisor.
For that we use the “iAMA” function for the current symbol and the current period. Here you see some more properties: a 9 a 2 and a 30. You shall see the same properties if you insert the Adaptive Moving Average on your chart. The 9 means we calculate the Indicator’s values for a period of 9 candles, beside this the 2 is for the fast Moving Average (Exponential) and the 30 is for the slow Moving Average (also Exponential).
Now the next parameter is a 0 and it is for the shift value. The last parameter sets all the price calculations to be based on the close prices (PRICE_CLOSE).
After these we use “ArraySetAsSeries” once more to store the Indicator’s price data in our second array also in descending order starting form the current candle and downwards.
With “CopyBuffer” we retrieve the Indicator’s data then fill it into the price array for buffer 0. Our Indicator only has one line so it is using only one buffer, so it’s buffer 0. The calculation starts from the current candle (that’s candle 0) and for 3 candles only. The result is being stored in our price array (myPriceArray[]).
At this point we can now get the values of the current candle both for the price and the Adaptive Moving Average then compare them to see if we have all criteria met for opening a trade. What we do is we look for the values of candle 0, we minimize the digits behind the dot to 6 with “NormalizedDouble” (even 5 would be enough). Now the value is 0.992601, that’s 6 digits.
Next we want to know if the Adaptive Moving Average Value (AdaptiveMovingAverageValue) is greater than the close price of the current candle in our price info array (myPriceInfoto[])? If it is then the red line is above the candles and we have a sell signal so we assign the word: “sell” to our signal. If the opposite is true and in our price info array (myPriceInfo[]) the Adaptive Moving Average Value (AdaptiveMovingAverageValue) is below the close price for candle 0 then we have a buy signal so we assign the word: “buy” to our signal variable.
Now we are coming to our final step which is: we return the signal to the main module. That is done by the “return” function.
Alright, now we save the file, please click on this icon here or press CTRL + S. Next I need to recompile the framework. It worked fine, no errors and no warnings. Now we can go back to Metatrader, please click on this icon here or use F4 key.
When in Metatrader let’s start Strategy Tester, please click on “View/ Strategy Tester” or press CTRL and R.
In Strategy Tester please select the Forex Trading Framework or your own main module.
If you don’t have a framework, just pick any currency pair, enable the visualization mode and start the backtest.
The Expert Advisor is running and working fine here and you just leaned how to code an entry module which calculates the Adaptive Moving Average and you did it by yourself with only a few lines of MQL5 code.

Download “Forex Trading Framework - Advaptive Moving Average Module” AdvancedAdaptiveMovingAverage.txt – Downloaded 140 times – 1 KB