MQL5 Tutorial – Simple Moving Average Calculation


This red line here is a simple moving average!


And in this video, we are going to create a simple expert advisor that is able to calculate the value for this moving average and show it on the chart. Let’s see how we can do that with MQL5. Introduction to calculating a simple moving average in MQL5. In this video, we 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. Opening Meta Editor and Creating a New File. Please click on the little button here or press F4 in Meta Trader. Now you should see the Meta Editor window, and here you want to click on File, New Expert Advisor, Template from Template, Continue. I will call this file, Simple Moving Average. Click on Continue, Continue and Finish.


Setting up the code structure!


Now you can delete everything above the onTick function and the two comment lines that remain. Defining the moving average with IMA function. Let’s continue with the definition of the moving average. 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 is used to automatically find out what symbol is used on the chart. In my case, it’s the currency pair, Australian dollar against Yen. The second parameter 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, configuring Parameters for Moving Average Calculation. When you put your mouse over the moving average, you will see behind the moving average that stands for 20 candles. This parameter here is the starting point. We will start the calculation based on the current candle zero. We use Mode Underscore 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 closing as price. Filling the moving average array with data.

Now we use copy buffer, fill our moving average array with data based on the moving average definition that we have created here. We start from Candle 0. We do it for Buffer 0. 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 three lines, so that would be Buffer 0, 1 or 2. We start with the current Candle 0, and we copy the price data for three candles and store it in our array. Calculating the moving average for a specific candle. To calculate the moving average for candle one, that’s the last complete candle that appears on the chart, we can use the value for candle one in our moving average array and store it in a variable called my Moving Average 20, outputting the moving average value on the chart. Finally, we want to use the comment function to create an output on the chart saying, My Moving Average 20, followed by the value that we have calculated.


Compiling the code and testing in MetaTrader!


Now we can press F7 to compile the code in Metaeditor, and then we can test the ExpertAdvisor in MetaTrader using the strategy tester. Observing the moving average and output on the chart. If you have any trouble understanding the code or need more basic information, you can check out our premium course on our website or watch some of our beginner videos first. Otherwise, Let’s go back to MetaTrader by pressing F4 or clicking the icon, then press CTRL+R to start the strategy tester. Select the ExpertAdvisor we just created, enable the visual mode, and start the test. You should now see the ExpertAdvisor and the calculated simple moving average on the chart. If you’re already a premium member and have an idea for a video like this, feel free to send us an email. We’ve just created a simple moving average expert advisor using a few lines of MQL code. Thanks for watching, and I’ll see you in the next video. Okay, if everything went well, you should see the expert advisor on your chart now. If you have any trouble understanding all that stuff, maybe the premium course on our website might be interesting for you. For now, I say thank you for watching, and I will see you in the next video.