MQL5 TUTORIAL BASICS – 11 HOW TO CALCULATE A SIMPLE MOVING AVERAGE

In this video want to calculate the value for a Simple Moving Average and output the value on the chart, so let’s find out how to do that with MQL5.
Please click on the little button here or press F4 in Metatrader, 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: ”SimpleMovingAverage”, click on “Continue”, “Continue” and “Finish”.
Now you can delete everything above the “OnTick” function and the two comment lines here.
We start by creating an array for the Simple Moving Average (myMovingAverageArray) and you can think of an array like a raw of boxes that contain values.
Let’s continue with the definition of the Moving Average (movingAverageDefinition), we use the integrated MQL5 function: “iMA”, if you highlight it and press F1 you will see that it needs a few parameters; the first parameter “_Symbol” is used to automatically find out what kind of symbol is used on the chart – in my case it’s the currency pair: Australian Dollar against Yen – the second parameter “_Period” contains the value of the time frame that is used on the current chart – I’m using the M1 chart for one minute candles right now – the third parameter is the number of candles that we are going to use to calculate the Moving Average, in our case we use 20 candles.
When you put your mouse over the Moving Average you will see 20 in round braces behind the Moving Average behind here (Moving Average (20)) that stands for 20 candles.
This parameter here is the starting point; we will start the calculation based on the current candle 0 (zero), we use “MODE_SMA” to calculate a Simple Moving Average and we use “PRICE_CLOSE” in the last parameter because we want to calculate the Moving Average values based on the close price.
Now we use “CopyBuffer” to fill our Moving Average array (myMovingAverageArray) with data based on the Moving Average Definition (movingAverageDefinition) that we have created here, we start from candle 0 (zero), we do it for buffer 0 (zero) – that’s the red line here, the only Indicator line that we have for a Moving Average. The Bollinger Bands Indicator for example would have 3 lines, so that would be buffer 0 (zero), 1 or 2, we start with the current candle 0 (zero) and we copy the price data for three candles and store it in our array (myMovingAverageArray).
To calculate the Moving Average for candle 1 – that’s the last complete candle that appears on the chart – we can use the value for candle 1 in our Moving Average array (myMovingAverageArray) and store it in a variable called: “myMovingAverage20”.
Finally we want to use the “Comment” function to create an output on the chart, saying: “MyMovingAverage20:” followed by the value that we have calculated, that’s it so far.
Now you can click on the “Compile” button or press F7 on your keyboard – I’m getting a warning here: “possible loss of data due to type conversion” because I am using a floating type (float), that means we have less digits behind the dot but for our simple example that should be enough – so when you are done and when you don’t have any errors, you can click on the little button here or press F4 to go back to Metatrader.
In Metatrader we want to click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: ”SimpleMovingAverage.ex5”, mark the visualization option here and start a test.
Now you should see the Moving Average, it’s the red line here on the chart and in the left upper corner we have the calculated value, so let’s have a look at the value for the Moving Average – remember we have calculated it for a candle 1 – in my case it is 83.4845, that’s exactly what we have here in our value and in this little video you have learned how to calculate the values for a Moving Average and how to create an output on the chart and you have done it with a few lines of MQL5 code.

Download “CODE - SIMPLE MOVING AVERAGE CALCULATION” SimpleMovingAverage.mq5 – Downloaded 1 time – 599 B