- Introduction to Shifted SMA Expert Advisor (00:00 – 00:14)
- Introduction to creating an Expert Advisor in MQL5 that generates buy or sell signals based on the Simple Moving Average (SMA). Explanation of the SMA signal for 50 candles and its prediction into the future.
- Creating the Expert Advisor in MetaEditor (00:14 – 01:13)
- Instructions on opening MetaEditor in MetaTrader and creating a new Expert Advisor file named ‘SimpleShiftedSMA’. Initial setup of the code environment.
- Setting Up Signal String and Arrays (01:13 – 01:40)
- Creating a string for the signal and setting up arrays for the current and shifted Moving Average.
- Defining Moving Average Properties (01:40 – 02:27)
- Using the ‘iMA’ function in MQL5 to define properties for the current and shifted Moving Average based on 50 candles.
- Sorting Arrays and Filling with Data (02:27 – 03:35)
- Sorting the Moving Average and shifted average arrays using ‘ArraySetAsSeries’ and filling them with data using ‘CopyBuffer’.
- Calculating and Assigning Signals (03:35 – 04:20)
- Determining the current and future SMA values and assigning ‘buy’ or ‘sell’ signals based on their comparison.
- Displaying Signal on Chart and Testing (04:20 – 05:00)
- Using the ‘Comment’ function to display the current signal on the chart and instructions for compiling and testing the Expert Advisor in MetaTrader.
- Conclusion and Demonstration of Signals (05:00 – 05:30)
- Conclusion of the tutorial with a demonstration of the Expert Advisor in action, showing buy and sell signals based on the SMA crossover.
In this video we are going to create an Expert Advisor that is going to output buy or sell signals based on the SMA (Simple Moving Average) here, it’s a Moving Average signal that is created for 50 candles, this one is the current signal and this one is the signal predicted into the future and whenever those two lines cross we either get a buy or a sell signal.
When the future SMA is below the other one, we have a buy signal and when the current SMA crosses the future SMA and is below it – like here – we have a sell signal.
To create the Expert Advisor please click on the little button here or press F4 in your Metatrader, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, please click on “Continue”, I will call this one: “SimpleShiftedSMA”, click on “Continue”, “Continue” and “Finish”.
Now we can delete everything above the “OnTick” function and the two comment lines here.
We start by creating a string for the signal, this one will also be called: “signal” and we don’t assign a value right now because this one needs to be calculated later on.
Let’s create two arrays; one for the current Moving Average and one for the shifted Moving Average, afterwards we define the properties for the Moving Average by using the built in “iMA” function that comes with MQL5 for the current symbol on the chart and the currently selected period, the calculation will be based on 50 candles, here we use a shift value of 0 (zero), the calculation will be for an SMA so we use “MODE_SMA”, that stands for Simple Moving Average and the result will be based on the close price.
The second calculation looks almost like the first one, we have two differences here; the first difference is that this one is for the shifted average definition (ShiftedAverageDefinition) and the second one is that we use a shift value of 20 here, this will move our SMA 20 candles into the future, so when we pass this you will see that this is the current candle, this is the current value and this one is the predicted value that is shifted into the future.
For both of our arrays we use “ArraySetAsSeries” to sort the current array from the current candle downwards here for the Moving Average array (myMovingAverageArray) and here for the shifted average array (myShiftedAverageAttay) and with “CopyBuffer” we fill our array according to the definition that we have created here for the current buffer 0 (zero) – that’s the first line for our Indicator – from the current candle 0 (zero) for 3 candles and we store the result in our Moving Average array (myMovingAverageArray).
For the shifted average it’s the same, the only difference is that the value will be stored in the shifted average array (myShiftedAverageAttay).
To get both values we look at candle 0 (zero) either in the Moving Average array (myMovingAverageArray) or in the shifted Average array (myShiftedAverageAttay) and if the average array value (myMovingAverageValue) is above the shifted average array value (myShiftedAverageValue) we would assign the word: “buy” to our signal.
In the other case if the average array value (myMovingAverageValue) is below the shifted average value (myShiftedAverageValue) that would be a sell signal so now we assign the word: “sell” to our signal.
Last but not least we use the “Comment” function to output the words: “The current signal is:” followed by our current “signal” directly on the chart.
Okay, when you’re done you can click on the “Compile” button or press F7, that should work without any errors and if that is the case you can click here or press F4 to go back to Metatrader and in Metatrader we click on: “View/ Strategy Tester”, here we pick the file: “SimpleShiftedSMA.ex5”, mark the visualization option and start our test.
…and here we are, currently we have an uptrend, the shifted value is below the current value so that’s a buy signal and as soon as we see a crossover here – like right now – we get a sell signal. So now you know how to create an Expert Advisor that is going to calculate a shifted SMA value and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 TUTORIAL - SIMPLE SHIFTED SMA STANDALONE EXPERT ADIVSOR”SimpleShiftedSMA.txt – Downloaded 170 times – 1.81 KB