This video is about the MacD.
The MacD is an old indicator, it was created in 1979 by a man called Gerald Appel.
And when the MacD is above this line here, this could be a signal for a long trend and when the signal is below the line it is trending downwards.
You also might notice that the crossing of these two lines here also is a good indication that it might go into the opposite direction now.
We will use that in the advanced strategies but for now, we want to create a simple MacD Expert Advisor that will tell us if it is going up or if it is going down.
This is the very complicated MacD example in the MQL5 reference, but we will create a very simplified version now.
To create the Expert Advisor now please click on the little button here and now you should see the Meta Editor and here you want to select File, New, Expect Advisor from template, Continue.
I will call it Simple MacD, Continue, Continue, Finish.
Now you can remove everything above the Ontick function and I will delete the two comment lines here.
So let’s create an array that can hold several prices and I will call it my price array.
And we also need to create a MacD definition by using the function iMacD.
It will use the current currency pair on the chart and currently on the chart selected period.
And if you click on Insert, Indicators, Oscillators, MacD, you will see that it is calculated for a fast E.M.A.
That is an Exponential Moving Average for twelve candles, that is what we use here.
It also calculates a slow Exponential Moving Average for twenty six candles so we use twenty six for the next parameter.
The MacD Simple Moving Average is calculated for nine candles, that’s the nine here and we want to calculate our value for the close price so we use Price_Close for the last parameter.
Okay let’s sort the price array from the current candle downwards by using the ArraysetasSeries function for the price array we have created here.
And now we use CopyBuffer for the defined moving average in our MacD definition.
We want to calculate one line, so we use a zero for the first buffer here and from the current candle that’s candle zero, for three candidates that’s the three here we want to store our results in the price array.
And now we want to get the MacD value of the current candle, that is candle zero in our price array.
And we will assign the value to the variable called MacD value.
I use a float instead of a double value here because a float type will save us some memory and it’s precise enough to find out if we are above or below the zero line.
So let’s create a chart output depending on the value of the MacD, and if our calculated MacD value is greater than zero, we want to use the comment function to print the text, “Trending Upwards” and the MacD value directly on our chart.
In the other case the MacD value is less than zero; we want our output to be “Trending Downwards” followed by the MacD value we have calculated here.
Okay that’s it, please click on the compile button here or press the F7 key on your keyboard.
That should compile your Export Advisor.
If everything is okay please press F4 on your keyboard or click on the little button here to go back to Meta Trader.
And in MetaTrader you want to click on View, Strategy Tester or press control and R to bring up the strategy tester panel.
And here we want to select the simple MacD.ex5 file, pick any currency pair and mark the checkbox for the visualisation option before you start your test.
The MacD is below the zero line when we have a bearish trend and it is above the line when we have a bullish trend.
Depending on the value it will show you the words “Trending Downwards” below the line or “Trending Upwards” above the line.
Okay now you know how to create an Expert Advisor for the MacD Indicator and you have created it yourself with just a few lines of MQL5 code.
Download “MQL5 - Simple MacD Trading Robot from MQL5 Tutorial”SimpleMacD.txt – Downloaded 470 times – 779 B