MQL4 TUTORIAL – SIMPLE MOVING AVERAGE SHIFT

video
play-sharp-fill

In this video we are going to talk about a simple SMA shift function that is included in MQL4. You see a red and a green line here, this one is a Moving Average for 20 candles. Actually, if you pause the system and zoom in by hitting the minus key on your keyboard you will see that both SMA’s are identical except one of those is shifted. Whenever the green one is above the red one that’s a buy signal for us and when we have a crossover and the red one is above the green one it will change to sell here.
Now how can we create an Expert Advisor that is able to calculate one and the same Simple Moving Average and create a signal out of the shifted second SMA?
To do that please click on the little button here or press a F4 on your keyboard and now you should see the Metaeditor window and here you want to select “File/ New/ Expert Advisor (template)” from template. Continue. I will call the file: Simple SMA Shift (SimpleSMAShift). Click on continue, continue and finish. Now you can delete everything that is above the “OnTick” function and please also remove the two comment lines here.
First we need to create a string variable for the signal, that will also be called signal and it will not be assigned any value here because we are going to calculate that later on.
Now we want to calculate the Simple Moving Average for 20 minutes and we can do that by using the “iMA” function. This “iMA” will be calculated for the current symbol on the chart, it will be calculated based on the 1-minute period and we use 20 candles to calculate it. Now if you mark the “iMA” function and press F1 you will see that this parameter is a shift value and the last parameter also is a shift value!
I found that to be extremely difficult to understand when I was starting out, so let me explain here. This shift value here is used to move the red line here 20 candles into the future. If I click the pause button here, move the mouse above the red SMA and select MA properties you will see that I have selected a shift value of 20 here. Let’s change that to 5 for testing purposes and you will see that the red line now is much closer to the green line. I will change that back now, the shift value is 20 again and when I click on “OK” it is drawn 20 candles into the future once again. So, that’s what you can do with parameter 4 here. The last parameter here is the second shift value and this one is used to tell MQL4 which candle should be calculated. We use the shift value of 1 to calculate the close price of candle 1. Now if I change this shift value here to a higher value the indicator wouldn’t be calculated for the last candle but for a candle long ago. We use a Simple Moving Average here and the calculation is based on the close prices so this parameter here is: “MODE_SMA” for a Simple Moving Average and we use: “PRICE_CLOSE” to do a calculation that is based on the close price of candle 1 here.
Okay! This was our first Simple Moving Average.
Let’s calculate the second one! It will also be calculated for 20 minutes but we use a shift value of 20 because we want the red Simple Moving Average line to be drawn 20 candles ahead.
If My Moving Average1 (MyMovingAverage1) is above My Moving Average2 (MyMovingAverage2) – in MQL4 that’s; if (MyMovingAverage1>MyMovingAverage2) we want to set the signal to buy, so we assign the word: “buy” to the variable called signal. In the other case if My Moving Average1 (MyMovingAverage1) is below My Moving Average2 (MyMovingAverage2) – let’s assign the word: “sell” to the signal. And last but not least we create an output for the signal directly on the chart by using the “Comment” function and that will output the words: “The current signal is:” followed by the calculated value for the signal.
Okay, if you have done everything you can click on the compile button here or press F7 on your keyboard and now your Expert Advisor should be compiled without any errors and without any warnings, and if it worked you can click on the little button here or press F4 to go back to Metatrader.
In Metatrader we are going to use a little trick, please click on “Insert/ Indicators/ Trend/ Moving Average”, select a period value of 20, the shift will be 0, you can leave the rest as it is, just select a green color here and click on “OK”.
This is Moving Average 1, let’s add another one by clicking on “Insert/ Indicators/ Trend/ Moving Average”. This time we are going to use a shift of 20, the period remains 20 candles and the color will be red.
Let’s click on “OK” and now right-click into the template, select template, save template and save it as: “tester.tpl” because the tester TPL template is used by the strategy tester. You can also save the template as default TPL and overwrite the old value here because the default TPL is what Metatrader4 is going to use when you open a new chart.
To start the Strategy Tester please click on “View/ Strategy Tester” or press CTRL and R and select the file: SimpleSMAShift.ex4, please set the mark for the visual mode here and start your test.
Here we go! The green line is above the red one and now we have a cross over and the signal is changing here, so your Expert Advisor actually works and create signals and now you know how to create an Expert Advisor and output signals for a Simple Moving Average that has been shifted and you have done it yourself with a few lines of MQL4 code.