In this video we want to talk about an Exponential Moving Average that is creating buy or sell signals, the signals are based on the direction.
If the Moving Average is above or below the candles; whenever the candles are below the Moving Average this would be a sell signal and when the candles are above the Moving Average this creates a buy signal.
Now how can we create an Expert Advisor in MQL4 that is able to output the buy and sell signals based on an Exponential Moving Average?
To do that please click on the little button here or press F4 on your keyboard and now you should see the Metaeditor window and here you want to click on “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this one: “SimpleEMA” for Exponential Moving Average, click on “Continue”, “Continue” and “Finish” and now you can delete everything that is above the “OnTick” function and let’s remove the two comment lines here.
Okay, we start by creating a string variable for the signal, we call it: “signal” and we don’t assign a value because we are going to calculate it in the next step and this is how it’s done: we use the built-in “iMA” function that comes with MQL4.
It needs to have a few parameters; the first one is the current symbol on the chart – in our case the symbol is the British Pound against the Australian Dollar (GBP/AUD) – the second parameter is the period that we have selected on our chart, we are using the 1-minute chart here and these two variables: “_Symbol” and “_Period” are able to find out what kind of currency pair you trade and which of the timeframes you use.
The next parameter is for 20 candles, so the value here is calculated based on the last 20 candles here and this parameter is a shift value. If you mark the “iMA” function and press F1 you will see that we have a shift value here that is called: “ma_shift” and another one here that is also called: “shift”.
Let’s open a new chart window: click on “Insert/ Indicators/ Moving Average”, let’s ad a Moving Average with a shift value of 0 and another one in yellow with a shift value of 20 and now you see what it does, it moves the drawing of the Moving Average into the future. I could also use -20 here and now it was moved to the past, this expression: “MODE_EMA” actually lets you select between a Simple Moving Average and Exponential Moving Average, a Smooth Moving Average and a Linear Weighted Moving Average.
To find out what the differences are you might maybe want to look up the 4 types on investopedia or vikkipedia.
The Exponential Moving Average basically is a simple Moving Average but the last few current candles play a bigger role in the calculation. You could play around with the 4 types here to see the differences on your chart, for now we go with the Exponential Moving Average and it should be calculated based on the close price, so we use “PRICE_CLOSE” here. This is the offset shift and this is the shift that we have, and this is the shift value we have changed in this example to move the drawing of the Moving Average to the right or to the left. It might take you some time to get used to it but for now both values are 0 and if the SMA is below the bid price – that would be the case if my Moving Average (MyMovingAverage) is smaller than “Bid” – that would create a buy signal so we assign the word: “buy” to our signal.
Of course we also want to calculate the other case. If our SMA is above the ask price; so if my Moving Average (MyMovingAverage) is bigger than “Ask” that would be a sell signal so we assign the word: “sell” to our signal variable.
In the last step we want to create an output on our chart so here we use the “Comment” function that will output the text: “The current signal is:” followed by our “signal” and when you’re done you can click on the “Compile” button here or press F7, that worked without any errors and now you can click here or press F4 to go back to Metatrader.
In Metatrader we need to use a little trick; we click on “Insert/ Indicators/ Trend/ Moving Average”, set the period to 20, we want to use a shift value of 0, the Moving Average method should be exponential and it should be applied to the close price.
That’s it, now you can click on “OK”, here is our Moving Average, now right-click on the chart, select “Template/ Save Template” and save it as: “tester.tpl” because tester tpl is what the Strategy Tester will use in the next back test otherwise you wouldn’t see the Moving Average.
Now click on “View/ Strategy Tester” or press CTRL and R, select the “SimpleEMA.ex4” file and start your test and here is our little Expert Advisor at work, as you can see it’s now using the yellow color and now you know how to create an Expert Advisor that is able to create buy or sell signals based on an Exponential Moving Average and you have coded it yourself with a few lines of MQL4.
Download “MQL4 TUTORIAL - SIMPLE EMA EXPERT ADVISOR”SimpleEMA.txt – Downloaded 10205 times – 626.00 B