In this new video we shall create an Expert Advisor with the use of the Indicator called: Fractal Adaptive Moving Average. We have a buy signal whenever the blue line is below the candles, like here, and we have a sell signal when the line is above the candles, like this one here.
So, how can we code an Expert Advisor in Metatrader 5 which is able to print (display) the buy or sell signals directly on the Forex chart?
Let’s begin by clicking on the little button here or you can also press F4 on your keyboard. You should see the Metaeditor window now.
Next you click on file “File /New /Expert Advisor” from template (template), Continue (Next). Give it a name like: “Simple Fractal Adaptive MA” (SimpleFractalAdaptiveMA). Continue, continue and finish.
In the Metaeditor window delete everything above the “OnTick” function including the two comment lines.
First of all, we are creating a string variable for the signal – we also name it signal – and we are going to assign the values later.
Next you need to create a price array. For this we use “MqlRates”.
“MqlRates” is a function in MQL5 which stores information about prices, volumes and the spread. Therefore, whenever you need to get the values for an open, high, low or a close price just use “MqlRates” to build a price array. Then the array needs to be sorted. We do that by the command: “ArraySetAsSeries”. Next, we are sorting the price array from the current candle in descending order. We need data for 3 candles only and we use “CopyRates” to fill the price array with the data. For “CopyRates” you need to define a few parameters. First comes the parameter for the symbol which is for the currency pair on the chart. Second is the currently selected period on the chart. For example: a 1-minute chart or an hourly chart. The zero here stands for the current candle. The current candle is always named candle zero and it is on the right side of your chart. This one here called candle 1, and this one is candle 2, and so on.
We only need data for 3 candles, then we store the result in our price array (PriceArray).
For the Expert Advisor let’s create another array. Let’s name it: “FramaArray” for the Fractal Adaptive Moving Average and we do that with the use of the built-in function “iFrAMA” which comes with MQL5. Let’s use the current symbol on the chart and the current period on the chart. The calculation of Frama (Fractal Adaptive Moving Average) is based on 14 candles, which is the default settings for the indicator. This you can see also when you right click on the indicator and select properties. Here it is: 14 candles and the calculation is based on the close price.
The next parameter here is for the shift and we leave it zero – that’s also 0 in the default settings. The last parameter to be set here is “PRICE_CLOSE”.
We have only used the default parameters here. This array needs to be sorted as well. Once again, we use “ArraySetAsSeries” to do that. Next we fill our Frama array with data by using CopyBuffer” based on the definition we have created here. Frama has only one line here; that’s buffer 0. We calculate values from the current candle; that’s candle 0. We only take data for 3 candles, the number 3 here stands for that. Then we store the data in the Frama array.
Afterwards we need to calculate the value for the last candle – which is candle 1 in the Frama array – to get the signal. If the Frama value is above the last candle high we would like to sell. (Sell signal) In this case here for example. If the Frama value is below the last candle low we would like to buy. (Buy signal). In MQL5 we define this conditions by using an “if” statement. For example, 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 and if the Frama value is below the last candle low, that is; if Frama value (FramaValue) is below the low price of candle one (PriceArray.low) in our price array we want to assign the word: “buy” to our signal. Now we want to display the buy or sell signal values directly on our Forex chart. We can do that by using the “comment” function.
This will output the word: “FramaValue” for the Fractal Adaptive Moving Average value, then adding and printing the calculated value. Next stands for the outputting of the word: “Signal” in a new line followed by the calculated signal.
Please click on the compile button or press F7 on your keyboard if you are done. The compilation should work without any errors or warnings. If that so, let’s go back to Metatrader by clicking on the little button here or press F4. To start a test please click on “View/Strategy Tester” or press CTRL and R on your keyboard in Metatrader.
When you see the Strategy Tester panel, please select the file: SimpleFractalAdaptiveMA.ex5. Turn on the visualization option by clicking in the checkbox here then start the test.
This is how it looks. We can see the buy or sell signals displayed directly on your Forex chart and you have created it by yourself with a few simple lines of MQL5 code.
Incoming search terms:
- mql5 ea example