MQL5 TUTORIAL BASICS – 19 HOW TO TRADE A SIMPLE MOVING AVERAGE

In this video we are going to create a simple Expert Advisor that is going to trade this Moving Average. It’s a Moving Average for 20 candles, so let’s find out how to do that with MQL5.
First you need to 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: “SimpleMovingAverageEA”, click on “Continue”, “Continue” and “Finish”.
Now you can remove everything above the “OnTick” function and the two command lines here.
We start by including the file: “Trade.mqh” that comes with MQL5 and it provides easy trade functions, so let’s create an instance of “CTrade” that will be called: “trade” and we are going to use it to open our positions later on.
As we want to trade in both directions we need to get the Ask price and the Bid price, that is done by using the function “SymbolInfoDouble” for the current symbol, for the Ask price we use “SYMBOL_ASK” – all in capital letters – and for the Bid price we use “SYMBOL_BID” also in capital letters, I like to use “NormalizeDouble” and “_Digits” to automatically calculate if we have a currency pair with 3 digits or 5 digits.
Now let’s continue, we create an array for the price data that will be called: “PriceInfo“ and we use the function: “MqlRates” to create it, because “MqlRates” stores the information about prices, volumes and spread.
Now we want to fill the array (PriceInfo) with price data that is done by using “CopyRates” for the current symbol on the chart and the currently used period on that chart, actually this and this statement are equal, we could also use “_Symbol” and “_Period”, we start with the current candle 0 (zero) and we want to copy the price data for 3 candles and store it in our price info (PriceInfo) array.
Let’s create a string for the signal that will also be called: “signal” and we don’t assign any value here because we are going to calculate that later on.
Now we need another array, this time for the Moving Average it will be called: “myMovingAverageArray”, so let’s define the properties for the Moving Average (movingAverageDefinition). We use the included “iMA” function of MQL5 for the current symbol on the chart and the currently selected period on that chart, I want to calculate the Moving Average for 20 candles in the past, this is a shift value that could be used to move the Moving Average to the right or to the left, I don’t want to use it so I select 0 (zero), we want to calculate a Simple Moving Average so we use “MODE_SMA” – all in capital letters – and “PRICE_CLOSE” also in capital letters is used to calculate the value based on the close price.
So let’s use “CopyBuffer” to fill the array (myMovingAveargeArray) with data for the buffer 0 (zero) – that’s the red line here, this Indicator only has one buffer – we start with candle 0 (zero) and we want to copy the price data for 3 candles and store it in the my Moving Average array (myMovingAverageArray) according to the Moving Average definition (movingAverageDefinition) that we have created here.
Let’s define the buy conditions, we want to check if the close price for candle 1 is bigger than the value for the Moving Average for candle 1 and if the close prize for candle 2 was below the Moving Average value for candle 2 that would be a cross over and now we want to buy so we assign the word: “buy” to our signal.
In the other case if the close price for candle 1 is now below the Moving Average for candle 1 and the close price for candle 2 was above the Moving Average for candle 2 before that would be a sell signal and now we assign the word: “sell” to our signal, and if our signal equals sell and “PositionsTotal” is below 1 we want to open a sell trade for 10 micro lot, otherwise if the signal equals buy and we have no positions that would mean that we want to open a buy trade for 10 micro lot. If this is too fast for you I would suggest to go to the MQL5 basic videos that have been created before.
One thing is still missing, we need a chart output so we use the “Comment” function to output the text: “The signal is now:” and the calculated signal.
That’s it. When you are done you can click on the “Compile” button here you should not get any errors and in that case you can click on a little button here or press F4 to go back to Metatrader and in Metatrader you want to click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleMovingAverageEA.ex5”, mark the visualization option here and start your test.
Now you should see the red line here that is the Moving Average, we have signals on our chart; buy and sell and here we actually have opened our first position, let’s zoom in, here it is and now you have learned how to create an Expert Advisor to trade a Simple Moving Average for 20 candles and you have coded it yourself with a few lines of MQL5 code.

Download “CODE - SIMPLE MOVING AVERAGE EA” SimpleMovingAverageEA.mq5 – Downloaded 1 time – 2 KB