đ
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.
You can go back to the homepage right here: Homepage





