In this video we are going to trade this little Indicator; it’s the Fractal Adaptive Moving Average and we are going to create a Standalone Expert Advisor that is actually going to open positions.
Now how can we do that?
First please click on the little button here or press F4 in your Metatrader and now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this version: “SimpleIFRAMAStandaloneEA” click on “Continue”, “Continue” and “Finish”, now we delete everything above the “OnTick” function and the two comment lines here.
So first let’s use the “include” function to import the file: “Trade.mqh”, it comes with a few useful trading functions that we are going to use to open new positions. We use it to create an instance of “CTrade” that will be called: “trade”.
Inside of the “OnTick” function we just need to calculate the Ask price and the Bid price, those are needed to open new positions. We get the Ask price by using “SymbolInfoDouble“ for the current symbol on the chart and with “SYMBOL_ASK” we will get delivered the price we want. For the Bid price we would use “SYMBOL_BID” and “NormalizeDouble” and “_Digits” are used to calculate the number of digits behind the dot, which might be different from one currency pair to another.
We also create string for the signal; please don’t assign a value right now because that is going to be calculated later. Let’s use “MqlRates” to create a price array (myPriceArray), “MqlRates” is a structure that stores the information about prices, volumes and spread.
Let’s sort the array (myPriceArray) downwards by using “ArraySetAsSeries” and fill the array (myPriceArray) with data for 3 candles by using “CopyRates” for the current symbol and the current period. We start from the current candle 0 (zero) and we store the prices for 3 candles in our price array (myPriceArray).
Now we need another array for the Expert Advisor, this one will be called: “FramaArray”, our frama definition (FramaDefinition) can be created by using the internal function: “iFrAMA” that comes with MQL5, it needs a few parameters; the first one is for the current symbol on the chart, the second one for the period, it’s calculated based on 14 candles and if you click on “Insert/ Indicators/ Trend/ Fractal Adaptive Moving Average” you will see that we use 14 candles and we have a shift value of 0 (zero) – that’s what we use here – and the calculation is based on the close price.
We also need to sort this one from the current candle downwards once again by using “ArraySetAsSeries” for our frama array (FramaArray).
Let’s use “CopyBuffer” according to the frama definition (FramaDefinition) that we have created here for the first buffer in the Indicator – that’s the blue line – from the current candle 0 (zero) and we store the values for 3 candles in our frama array (FramaArray) and now we can calculate our frama value (FramaValue) by looking at candle 1 in the frama array (FramaArray) and we want to sell if the frama value (FramaValue) is above the last candle high – so if our frama value (FramaValue) is bigger than the high for candle 1 in our price array (myPriceArray) we assign the word “sell” to our signal.
There is also a buy signal; we want to buy if the frama value (FramaValue) is below the last candle low – so if frama value (FramaValue) is below the low of candle 1 in our price array (myPriceArray) we would assign the word “buy” to our signal.
Here comes the order; if the signal equals sell and “PositionsTotal” is less than 1 – in other words we don’t have any open positions – we use “trade.Sell” to sell 10 micro lot.
In the other case we buy 10 micro lot if the signal equals buy and we have no open positions; in this case we use “trade.Buy” to buy 10 micro lot.
Finally we create a chart output by using the “Comment” function, it will output the text: ”The current signal is:” followed by the “signal” and now if you are ready you can click on the “Compile” button here or press F7, that should work without any errors here and in that case please click here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, please pick the file: “SimpleIFRAMAStandaloneEA.ex5”, enable the visualization mode here and start your test.
…and here we are! We have just opened our first sell trade here, this is our first position and in this little video you learned how to output buy and sell signals for the Fractal Adaptive Moving Average and how to automatically open positions in a Standalone Expert Advisor and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 TUTORIAL - SIMPLE IFRAMA STANDALONE EXPERT ADVISOR”SimpleIFRAMAStandaloneEA.txt – Downloaded 336 times – 1.83 KB