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

video
play-sharp-fill

In this video we are going to calculate the values for a Simple Moving Average and we use the “Comment” function to output the current value on the chart.
To do that please click on the little button here or press F4 on your keyboard 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 file: ”SimpleMovingAverage”, click on “Continue”, “Continue” and “Finish” and now we can delete everything above the “OnTick” function and the two comment lines here.
We start by calculating a Simple Moving Average for 20 candles and we use the “iMA” function to do that. This one comes with MQL 4 and when you mark it and press F1 you will see that it takes several parameters; the first parameter here is for the current symbol and when we use “_Symbol” it will automatically find out what kind of symbol we have on our chart – in my case the current symbol here is the currency pair: British Pound against Canadian Dollar – the second parameter here is the period and “_Period” will automatically find out what kind of time frame you use on your current chart – in my case I’m using the M1 chart for one minute candles – the third parameter here is for the number of candles that we are going to calculate, we have chosen 20 candles and when I pause the “Strategy Tester” here and put my mouse over the Moving Average you will see that it says: it’s a “Moving Average (20)” for the last 20 candles and that means that the current value here is the value that you get when you take the last 20 candles and calculate the average value.
We have a shift parameter here and actually we have another one here, if you change this parameter here the drawing for the Moving Average will be moved to the right or to the left, if we would change the last value for the shift here that would change on which candle the current value should be calculated. For now just keep in mind that you use a 0 (zero) for both values. I actually almost never use any shifting values for any parameter. In parameter five we use “MODE_SMA” because we want to calculate a Simple Moving Average and for parameter six we use “PRICE_CLOSE” because the calculation should be based on the close price.
So let’s insert a “Comment” function that will output the text: “MyMovingAverage20” and the calculated value and afterwards we click on “Compile”, that should work without any errors here and if that is the case please 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, now please pick the new file:”SimpleMovingAverage.ex4”, mark the visual option here and start a test.
This is how it looks like, you don’t see a Moving Average here, we only have calculated values but when you click on stop, suddenly the red line appears and now we use a little trick; please right-click on the chart, select “Template/ Save Template” and save it as “tester.tpl” because this is the template that is going to be used in your next test.
So let’s start the test again and this time we see the red line for the Moving Average, here is our output, let’s zoom into the chart and when you put the mouse over the Moving Average of the last bar you will see that we have a value of: 1.6897 and that’s exactly what we have calculated here.
Now you could use the value for the Moving Average and for the price to create a simple system that is going to buy whenever the price is above the Moving Average and that is going to sell whenever the price is below the Moving Average and in this little video you have learned how to calculate the value for a Simple Moving Average of 20 candles and you have coded it yourself with a few lines of MQL4 code.
Download “CODE - SIMPLE MOVING AVERAGE” SimpleMovingAverage.mq4 – Downloaded 540 times – 282.00 B