- Introduction to Creating a Shifted SMA Expert Advisor in MQL5 (00:00 – 00:10)
- Introduction to the tutorial on creating an Expert Advisor for a Simple Moving Average and a shifted Simple Moving Average.
- Opening MetaEditor and Creating a New Expert Advisor File (00:10 – 00:44)
- Instructions on opening MetaEditor and creating a new Expert Advisor file named “SimpleShiftedSMAEA”.
- Setting Up the Code Structure and Including Trade.mqh (00:44 – 01:12)
- Removing unnecessary code and including the ‘Trade.mqh’ file for trading functions.
- Calculating Ask and Bid Prices (01:12 – 01:43)
- Using ‘SymbolInfoDouble’ to calculate the Ask and Bid prices and normalizing them for currency pair precision.
- Creating a Variable for the Signal and Moving Average Arrays (01:43 – 02:50)
- Initializing a variable for the trading signal and creating arrays for the Moving Average and shifted Moving Average.
- Calculating Moving Average and Shifted Moving Average Values (02:50 – 03:56)
- Using the ‘iMA’ function to calculate values for the Moving Average and shifted Moving Average.
- Determining Buy and Sell Signals Based on Moving Averages (03:56 – 04:19)
- Setting conditions for buy and sell signals based on the comparison of the Moving Average and shifted Moving Average values.
- Executing Trades Based on Signals (04:19 – 04:49)
- Using ‘trade.Sell’ and ‘trade.Buy’ to execute trades based on the determined signals.
- Displaying the Current Signal on the Chart (04:49 – 05:12)
- Using the ‘Comment’ function to display the current trading signal on the chart.
- Compiling the Code and Testing in MetaTrader (05:12 – 06:33)
- Compiling the code and testing the Expert Advisor in MetaTrader using the Strategy Tester.
- Creating a Template and Testing the EA in MetaTrader (06:33 – 07:10)
- Creating a template with the Moving Averages and testing the Expert Advisor in MetaTrader.
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” 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 and we also want to create a shifted Moving Average array.
To calculate the value for the Moving Average array we use the integrated “iMA” function.
The first parameter is for the current symbol on the chart.
The second one will give us the current period.
Parameter 3 is 20 for 20 candles,
Parameter 4 is 0 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 and the shifted Moving Average array according to the Moving Average definition that we have created here or the shifted Average definition that we have created here and fill both arrays for buffer 0 from the current candle 0 for 3 candles.
Now we are able to get the current value for the Moving Average array by looking into candle 0 of our Moving Average array and we get the shifted Moving Average value by looking into candle 0 in our shifted Moving Average array.
And if the Moving Average value is bigger than the shifted Moving Average value that would be a buy signal so we assign the word: “buy” to our signal.
In the other case if the Moving Average value is below the shifted Moving Average value 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, 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.