MQL5 TUTORIAL – ADVANCED ADAPTIVE MOVING AVERAGE

video
play-sharp-fill

In this video we are going to talk about the Adaptive Moving Average, it’s the little red line here and it looks like a Simple Moving Average but actually Mr. Perry J Kaufmann created this version to produce less false signals.

In this little video we are going to create an entry module that can be traded with the main module. In my case it’s the Forex Trading Framework and it will use the Adaptive Moving Average to calculate buy or sell signals.

To do that please click on the little button here or press F4 in your Metatrader.
This is the framework I’m using; basically it’s a collection of functions that can manage a trade once it is opened.
We want to create a file named: “CheckEntry_AMA.mq5” and use the… and use a function called: “CheckEntry” to return a signal. When a new tick comes in we pass the signal to the “CheckForTick” function to check if we want to open a new trade.

Now please click on “File/ New/ Expert Advisor” from template (template), “Continue” (Next). I will call this one: “CheckEntryAMA”. Now click on “Continue” (Next), “Continue” (Next) and “Finish”.
Now you can delete everything above the “OnTick” function and the two comment lines here.
Let’s adjust this one. First we need to create an array for the prices by using “MqlRates” and it will be called: “PriceInfo[]”.

Now we sort the price array from the current candle downwards by using “ArraySetAsSeries” and we fill the price array (PriceInfo[]) with price data by using “CopyRates” for the current symbol on the chart and the selected period.

We start with the first bar and this expression here will give us all the bars on chart and the result will be stored in the array: “PriceInfo[]”.

Let’s create a string variable for the signal; it’s of the type string and it will be called signal and it doesn’t contain any value now because that value will be calculated later.

We need another array for the price data of the Indicator that will be called: “myPriceArray[]” and it’s of the type double so it now can contain floating type values.

Let’s define the properties of the Adaptive Moving Average Expert Advisor; that can be done by using the “iAMA” function for the current symbol and the current period and here we have several numbers; a 9 a 2 and a 30. When you insert an Adaptive Moving Average on your chart you will see the 9 stands for the period – that’s the number of candles that are used for the calculation – the 2 is used for the Exponential Moving Average; the fast one, and there is also another Exponential Moving Average; the slow one, that has the value 30 and here is a shift value that is 0 – that’s this parameter – and the results are calculated based on the close prices, so we use “PRICE_CLOSE” for our last parameter.

Let’s use “ArraySetAsSeries” to store the second array from the current candle downwards, and with “CopyBuffer” we fill our price array for buffer 0 – that’s the first and only line of our Indicator – from candle 0 (that’s the current candle) for 3 candles and store the result in our price array (myPriceArray[]).

Now we can get the value of the current candle, all we need to do is to look at candle 0 in our price array and with “NormalizedDouble” and the 6 we cut the digits behind the dot. Actually a 5 would also be okay, in this case the value is 0.992601, that’s 6 digits, so let’s change that back to 6 and now that we have the value and the price we want to find out if the Adaptive Moving Average Value (AdaptiveMovingAverageValue) is bigger than the close price of the current candle in our price info array (myPriceInfo[]), so if it’s above our candles we assign the word: “sell” to our signal. In the other case if the Adaptive Moving Average Value (AdaptiveMovingAverageValue) is below the close price for candle 0 in our price info array (myPriceInfo[]) that would be a buy signal so we assign the word: “buy” to our signal variable.

The final step is to return the signal to the main module by using the “return” function for the signal.
Okay. Let’s save that file by clicking on this little icon, you could also use CTRL + S and now I need to recompile our framework, that worked without any errors and without any warnings so now I can click here or use F4 to go back to Metatrader.

In Metatrader I click on “View/ Strategy Tester” or press CTRL and R and in the Strategy Tester we select the Forex Trading Framework or you can select your own main module.

If you don’t use the framework pick a currency pair, mark the visualization mode here and start the test.

Here our little Expert Advisor is working and now you know how to create an entry module to calculate the Adaptive Moving Average and you have coded it yourself with a few lines of MQL5 code.

Download “MQL5 Tutorial - Advanced Adaptive Moving Average”

AdvancedAdaptiveMovingAverage.txt – Downloaded 541 times – 1.29 KB