MQL4 TUTORIAL BASICS - 21 HOW TO CODE A MACD EXPERT ADVISOR - YouTube

Channel: MQL4Tutorial

[0]
In this video, we are going to code an Expert Advisor that is able to calculate the MACD
[8]
Oscillator, it does not only generate signals it actually trades them, so let's find out
[15]
how to do that with MQL4.
[18]
To get started please click on the little symbol here or press F4, now you should see
[24]
the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor”
[29]
from template, “Continue”, I will call this file: “SimpleMacDEA”, click on “Continue”,
[37]
“Continue” and “Finish”.
[40]
Now you can remove everything above the “OnTick” function and let's also delete the two comment
[46]
lines here.
[48]
We start by creating a signal, that will be a string variable. Right now we don't assign
[55]
any value here, because we need to calculate that later on. And to do that we use the included
[62]
“iMACD” function that comes with MQL4, it uses a few parameters so let's go through
[70]
the parameters one by one.
[72]
The first one is for the current symbol; “NULL” is a placeholder, it will actually calculate
[78]
the current symbol, you could also use “_Symbol“ and for the time frame we could use “_Period”.
[87]
I would like to calculate the values based on the close price, “MODE_MAIN” is used
[97]
to calculate the base Indicator line, the last value here is a shift value. That would
[104]
be used to calculate the value for a different candle but in our case we use 0, because
[112]
we want to calculate the value for the current candle.
[116]
So what's left are these 3 values here. Let's open a new chart window, click on: “Insert/
[124]
Indicators/ Oscillators/ MACD”. Now you should see the values: fast EMA 12, slow EMA
[133]
26, MACD SMA 9, apply to close. That's exactly what we are using here, so let's click on
[144]
“OK” and the MACD should appear in the lower part of your chart.
[150]
Let’s right-click select “Template/ Save Template” and save it as “tester.tpl”
[160]
because this is the template that is going to be used in the backtest. But before we can
[166]
do that, we need to finish our Expert Advisor here.
[170]
If the current value for the MACD is greater than 0 – zero is this line here – so
[180]
if the signal line is above the zero line, we would expect it to return to the zero line.
[187]
That would be a sell signal so we assign the word: “sell” to our signal.
[193]
Otherwise if the MACD is below the zero line, that would be a buy signal so we assign the
[200]
word: “buy” to our signal. And if the signal equals buy and “OrdersTotal” equals
[208]
zero, that would mean we have no open orders - we use the “OrderSend” command to buy
[216]
10 micro lot.
[217]
Otherwise, if the signal equals sell and we have no open orders, we use “OrderSend”
[225]
to sell 10 micro lot.
[228]
Finally, we use “Comment” to output the text: “The current signal is:” and the
[234]
signal that we have calculated on the chart.
[237]
Let’s click on the “Compile” button here or press F7, you shouldn't get any errors
[243]
here. But if it was too fast for you, you might want to watch the other videos in this basic
[249]
series first. Or maybe even the premium course is interesting for you.
[255]
If everything is okay, you can click on the little button here or press F4 to go back
[259]
to Metatrader. And in Metatrader you want to click on: “View/ Strategy Tester” or press
[266]
CTRL and R, please pick the new file: “SimpleMacDEA.ex4”, enable the visual mode here and start a test.
[277]
Here we are!
[278]
Our Expert Advisor is actually working, we also have open positions and in this little
[287]
video you have learned how to automate the MACD using MQL4 to open buy and sell trades
[296]
on your chart. And you have coded it yourself with a few lines of MQL4 code.