MQL4 TUTORIAL BASICS – 25 SIMPLE SHIFTED MOVING AVERAGE EXPERT ADVISOR

video
play-sharp-fill

In this video, we are going to create an Indicator that is acting like this one. It’s actually the same Simple Moving Average that is used to create buy and sell signals. We have a real value and a shifted value, so let’s find out how to do that with MQL4.
To get started please click on a little icon here or press F4 on your keyboard, now you should see the Metaeditor and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleShiftedSMAEA”, click on “Continue”, “Continue” and “Finish”.
Now we want to delete everything above the “OnTick” function and let’s remove the two comment lines here.
We start by creating a string for the signal but we don’t assign any value because we are going to find out that value later on.
First we want to create a Simple Moving Average; let’s call this one: “MyMovingAverage1”, we use the “iMA” function for the current symbol (_Symbol), we use “PERIOD_M1” or if you would prefer to automatically calculate the period on the chart you could also use “_Period”, the third parameter is for 20 candles, parameter 4 can be used for one of the two shift values, in this example we leave it as 0 (zero). In the next parameter we say that we want to use a Simple Moving Average so we use “MODE_SMA” – all in capital letters – and the result should be calculated based on the close price (PRICE_CLOSE) in this case for candle 1.
Now we do the same for my Moving Average 2 (MyMovingAverage2) but this time we use a shift value of 20.
So that’s basically everything, the name of the Moving Average is different and the shift value is 20 instead of 0 (zero).
And if my Moving Average 1 (myMovingAverage1) is bigger than my Moving Average 2 (myMovingAverage2) I would consider that to be a buy signal so I assign the word: “buy” to the signal.
Otherwise if my Moving Average 1 (myMovingAverage1) is below my Moving Average 2 (myMovingAverage2) I would consider that to be a sell signal and now I assign “sell” to our signal.
And if the signal equals buy and “OrdersTotal” equals 0 (zero) – that’s when we have no open orders and a buy signal – we use the “OrderSend” commend to buy 10 micro lot.
Otherwise, if the signal equals sell and if “OrdersTotal” equals 0 (zero) we want to use “OrderSend” to sell 10 micro lot.
Finally we use “Comment” to output the text: “The current signal is:” followed by the calculated signal.
If this was too fast for you or if you don’t know what all the code here does maybe you want to watch the other basic videos in this video series or maybe even the premium course is interesting for you, for now, we click on “Compile” and we don’t get any errors here and in that case we can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader, we now click on: “Insert/ Indicators/ Trend/Moving Average”, here we pick a period of 20 candles, a shift value of 0 (zero), the MA method should be “Simple” for Simple Moving Average, apply to close price, I will pick the colour red for this one and click on “OK”.
Here is our first Moving Average, now let’s repeat that, click on: “Insert/ Indicators/ Trend/Moving Average”, and this time we will change the shift value to 20 and the colour to green and when I click on “OK” I will see a second Moving Average.
Now let’s right click into the chart, select “Template/ Save Template” and save it as “tester.tpl”, you can replace the old one because “tester.tpl” is what is going to be used in the next strategy test.
So let’s actually do that by clicking on: “View/ Strategy Tester”, please click on: “SimpleShiftedSMAEA.ex4”, mark the visual mode here and start your test.
And here we are! We have a red and a green line, our current signal is sell so we have opened a sell trade and now we have a crossover and the current signal is buy so our little Expert Advisor works, and you have learned how to create a standalone version of a Simple Moving Average and a shifted Simple Moving Average and you have coded it yourself with a few lines of MQL4 code.