MQL5 TUTORIAL – SIMPLE FRACTAL ADAPTIVE MOVING AVERAGE

video
play-sharp-fill

In this video we want to create an Expert Advisor for the Fractal Adaptive Moving Average Indicator. Whenever the blue line is below the candles here that would be a buy signal and when the line is above the candles like here; that’s a sell signal.
Now how can we create an Expert Advisor that is able to output our buy or sell signals directly on the Forex chart for Metatrader 5?
To do that please click on the little button here or press F4 on your keyboard and now you should see the Metaeditor window.
Here you want to click on file “File /New /Expert Advisor” from template (template), Continue (Next). I will call this one: “Simple Fractal Adaptive MA” (SimpleFractalAdaptiveMA). Continue, continue and finish.
Now you can delete everything above the “OnTick” function and the two comment lines here.
Let’s start by creating a string variable for the signal – we will also call it signal – and we don’t assign any values here.
We also need to create a price array, that is done by using “MqlRates”.
“MqlRates” is an MQL5 function and it stores information about the prices, volumes and the spread so whenever you need to get an open, high, low or close price just build a price array with “MqlRates”. Afterwards this array needs to be sorted by using “ArraySetAsSeries”, that will sort the price array from the current candle downwards and we use “CopyRates” to fill the price array with data for 3 candles. “CopyRates” takes a few parameters, the first one is the symbol parameter; that’s for the currency pair on the chart, the second one is for the currently selected period on the chart; like a 1-minute chart or an hourly chart. The zero here is the current candle zero – that’s always the candle on the right side of your chart – this would be candle 1, this would be candle 2, and so on.
We just need the data for 3 candles and store the result in our price array (PriceArray).
Now let’s create another array for the Expert Advisor, this one will be called: “FramaArray” for Fractal Adaptive Moving Average and we use the “iFrAMA” function that comes with MQL5 for the current symbol on the chart and the current period on the chart. It is calculated based on 14 candles – that’s also what you see when you right click on the indicator and select properties of the Fractal Adaptive Moving Average. Here are the 14 candles and the calculation is based on the close price.
This parameter would be for the shift – that’s also 0 in the default settings – and in the last parameter we use “PRICE_CLOSE”.
Now we have used all the default parameters here. This array also needs to be sorted, so we use “ArraySetAsSeries” once again and with “CopyBuffer” we fill our Frama array based on the definition we have created here for just one line; that’s buffer 0, from the current candle; that’s candle 0. We need the data for 3 candles – that’s the three here – and we store it in the Frama array.
To get the signal we need to calculate the value for the last candle – that would be candle one in the Frama array and we would like to sell if the Frama value is above the last candle high. Like in this case. In MQL5 we do it by using an “if” statement, so if the Frama value is above the high of candle one in our price array we want to assign the word: “sell” to the signal. In the other case, if the Frama value is below the last candle low; so, if Frama value (FramaValue) is below the low price of candle one (PriceArray[1].low) in our price array we want to assign the word: “buy” to our signal. We also want to create a chart output directly on our Forex chart that is done by using the “comment” function.
It will output the word: “FramaValue” for the Fractal Adaptive Moving Average value, followed by the calculated value. And in a new line we want to output the word: “Signal” followed by the calculated signal.
When you are done you can click on the compile button here or press F7, and that should work without any errors or warnings and if that is the case please click on a little button here or press F4 to go back to Metatrader.
In Metatrader you want to click on “View/Strategy Tester” or press CTRL and R on your keyboard.
Now you should see the Strategy Tester panel, and here you want to select the file: SimpleFractalAdaptiveMA.ex5. Mark the visualization option here and start your test.
And this is how it looks like. It creates buy or sell signals directly on your Forex chart and you have coded it yourself with a few lines of MQL5 code.

Download “MQL5 TUTORIAL - Simple Fractal Adaptive Moving Average”

SimpleFractalAdaptiveMA.txt – Downloaded 478 times – 1.33 KB