To do that please click on the little button here or press F4 on your keyboard, 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 file: “SimpleTripleSMAStandaloneEA” (Expert Advisor), click on: “Continue”, “Continue” and “Finish”.
Now we can remove everything that is above the “OnTick” function and the two comment lines here.
First I want to include the file: “Trade.mqh” and create an instance of “CTrade” that will be called: “trade” and we are going to use it later to open positions.
Now we calculate the Ask price and we calculate the Bid price by using “SymbolInfoDouble” for the current symbol, “SYMBOL_ASK” will give us the Ask price, “SYMBOL_BID” will give us the Bid price, ”NormalizeDouble” and “_Digits” will calculate the right number of digits behind the dot because we have different currency pairs here – this one has 3 digits behind the dot and this one 5 – and ”NormalizeDouble” and “_Digits” is taking care of that.
Let’s create an array by using “MqlRates”, this one is going to hold the price info (PriceInfo). We use “ArraySetAsSeries” to sort the price array (PriceInfo) from the current candle downwards and by using “CopyRates” we fill our price info array (PriceInfo) for the current symbol, and the currently selected period on the chart from the candle 0 (zero) – that’s the candle on the right side of the chart – for 3 candles.
Now we create an empty string for the signal that will be called: “signal” and we don’t assign any value here because we need to calculate it and because we are using three different Moving Averages here. This one is for 100 candles; this one is calculated for 50 candles and this one for 10 candles.
We also need to define three different SMA’s.
The first one will be the definition for the 10 candle SMA and we use the “iMA” function for the current symbol and the currently selected period for 10 candles, we don’t use a shift value, “MODE_SMA” is used to calculate the Simple Moving Average and that will be done based on the close prices. These two lines here are almost the same, except of the name for the definition and the number of candles.
All the three arrays need to be sorted from the current candle downwards by using “ArraySetAsSeries” and you might have guessed it… we use “CopyBuffer” for each of our arrays to fill them with data from the current candle for the first buffer from candle 0 (zero) for 10 candles and store the results in our price arrays (SMA10Array, SMA50Array, SMA100Array).
Now we can go ahead and calculate the long signal, so if the “SMA10Array” for candle 0 (zero) is bigger than the “SMA50Array” for candle 0 (zero) and if the “SMA50Array” is bigger than the “SMA100Array” that would be a buy signal so now we assign the word: “buy” to our signal.
Otherwise we calculate the short signal. When the “SMA10Array” for the current candle 0 (zero) is below the “SMA50Array” and when the “SMA50Array” also is below the “SMA100Array” that would be a sell signal so now we assign the word: “sell” to our signal.
To sell 10 micro lot we check if the signal equals “sell” and if “PositionsTotal” is less than 1 – that would mean that we don’t have any open positions here – and in that case we use “trade.Sell” to sell 10 micro lot.
In the other case we want to buy 10 micro lot if the signal equals “buy” and if we don’t have open positions, in that case we use trade.Buy and to buy 10 micro lot.
Finally we create a chart output by using the “Comment” function that will output the sentence: “The current signal is:” followed by the calculated “signal”.
Okay, that’s it. Please click on the “Compile” button or press F7… Oh, I made a mistake. I have used all these arrays here without any declaration so let’s correct that. Recompile and now it works. We don’t have any errors here, so now we can click here or press F4 to go back to Metatrader.
In Metatrader we click on “View/ Strategy Tester” or press CTRL and R, select the “SimpleTripleSMAStadaloneEA”, enable the visualization here and start a test.
..and here it is. It says: “sell” because the Moving Average for 10 candles is below the Moving Average for 50 candles and that one is below the Moving Average for 100 candles so our Expert Advisor is working and now you know how to create an Automated Expert Advisor for the Triple SMA Indicator that is actually going to open buy and sell trades and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 Tutorial - Simple Triple SMA Standalone Expert Advisor”SimplerTripleSMAStandaloneEA.txt – Downloaded 497 times – 2.46 KB