MQL5 TUTORIAL BASICS – 25 HOW TO CODE A SHIFTED SMA EXPERT ADVISOR

video

In this video, we are going to create an Expert Advisor for this Indicator here; actually, it’s a Simple Moving Average and a shifted Simple Moving Average.
The Indicator produces buy and sell signals that can be automatically traded in Metatrader so let’s find out how to do that with MQL5.
First please click on the little button here or press F4 to open 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 you can remove everything above the “OnTick” function and the two comment lines here.
We start with an include statement to include the file: “Trade.mqh”, this one will give us some simple trade functions.
First we create an instance of “CTrade” that will be called: “trade” and we are going to use it later on to open buy and sell positions and to be able to do that we first need to find out the Ask and the Bid price that is done with “SymbolInfoDouble”, “_Symbol”, we use “SYMBOL_ASK” to calculate the Ask price and “SYMBOL_BID” to calculate the Bid price.
I also use “NormalizeDouble” and “_Digits” because that will automatically calculate the number of digits behind the dot.
Now let’s create a string variable for the signal. So far it has no value; we are going to calculate that later on.
First, we create a Moving Average array (myMovingAverageArray) and we also want to create a shifted Moving Average array (myShiftedAverageArray).
To calculate the value for the Moving Average array (myMovingAverageArray) we use the integrated “iMA” function.
The first parameter is for the current symbol on the chart (_Symbol),
The second one will give us the current period (_Period),
Parameter 3 is 20 for 20 candles,
Parameter 4 is 0 (zero) because we don’t use a shift value for this Moving Average,
The next parameter is “MODE_SMA” – that stands for Simple Moving Average –
And the last parameter is “PRICE_CLOSE” – in capital letters – will give us the calculation based on the close price.
Now let’s repeat the whole thing, this time for the shifted Moving Average. Everything is similar except for the shift value here. This value will move the current Simple Moving Average – that is the red one – 20 candles into the future, that’s the green line.
Now we use “ArraySetAsSeries” to sort both Moving Averages from the current candle downwards and with “CopyBuffer” we fill the Moving Average array (myMovingAverageArray) and the shifted Moving Average array (myShiftedAverageArray) according to the Moving Average definition (movingAverageDefinition) that we have created here or the shifted Average definition (shiftedAverageDefinition) that we have created here and fill both arrays for buffer 0 (zero) from the current candle 0 (zero) for 3 candles.
Now we are able to get the current value for the Moving Average array (myMovingAverageArray) by looking into candle 0 (zero) of our Moving Average array (myMovingAverageArray) and we get the shifted Moving Average value by looking into candle 0 (zero) in our shifted Moving Average array (myShiftedAverageArray).
And if the Moving Average value (myMovingAverageValue) is bigger than the shifted Moving Average value (myShiftedAverageValue) that would be a buy signal so we assign the word: “buy” to our signal.
In the other case if the Moving Average value (myMovingAverageValue) is below the shifted Moving Average value (myShiftedAverageValue) we would consider that to be a sell signal and now we assign the word: “sell” to our signal.
And whenever the signal equals sell and “PositionsTotal” is below 1 – or in other words, we don’t have any open positions – we use “trade.Sell” and sell 10 micro lot.
Otherwise, if the signal equals buy and we have no open position we use “trade.Buy” and buy 10 micro lot.
So that’s almost it. We also want to use the “Comment” statement to output the text: “The current signal is now:” followed by the calculated signal.
If you don’t understand what all the code here does or this was too fast for you maybe you want to watch the other videos in this basic series or maybe even the premium course is interesting for you, for now, please click on “Compile”, you should not see any errors and no warnings here and in that case you can click on the little button here or press F4 to go back to Metatrader.
In Metatrader, we want to create a template.
That’s done by using “Insert/ Indicators/ Trend/Moving Average”, we want to pick a period for 20 candles, a shift value of 0 (zero), the method should be “Simple” for Simple Moving Average, apply to close and the colour is red. So let’s click on “OK”, and here is our first SMA, so let’s repeat that and create the other one; “Insert/ Indicators/ Trend/Moving Average”, but this time we are going to use a shift value of 20 and the colour should be green. So let’s click on “OK” to get the second Moving Average here.
Now right click into the chart, select “Templates/ Save Template” and save it as “tester.tpl”, you can override the current one because “tester.tpl” is what is going to be used in the next strategy test.
To show the Strategy Tester please click on: “View/ Strategy Tester” or press CTRL and R, here you want to pick the new file: “SimpleShiftedSMAEA.ex5”, please enable the visualization option here and start a test.
…and here is our little Expert Advisor at work, it produces buy and sell signals, whenever the direction changes and we see a crossover and you have learned how to create a simple Expert Advisor that is able to automatically trade those signals and you have coded it yourself with a few lines of MQL5 code.