In this video, we are going to code an Expert Advisor that is able to calculate the MACD Oscillator, it does not only generate signals it actually trades them, so let’s find out how to do that with MQL4.
Download “CODE - SIMPLE MACD EA”
To get started please click on the little symbol here or press F4, 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 file: “SimpleMacDEA”, click on “Continue”, “Continue” and “Finish”.
Now you can remove everything above the “OnTick” function and let’s also delete the two comment lines here.
We start by creating a signal that will be a string variable, right now we don’t assign any value here because we need to calculate that later on and to do that we use the included “iMACD” function that comes with MQL4, it uses a few parameters so let’s go through the parameters one by one.
The first one is for the current symbol; “NULL” is a placeholder, it will actually calculate the current symbol, you could also use “_Symbol“ and for the time frame we could use “_Period”, I would like to calculate the values based on the close price, “MODE_MAIN” is used to calculate the base Indicator line, the last value here is a shift value that would be used to calculate the value for a different candle but in our case we use 0 (zero) because we want to calculate the value for the current candle.
So what’s left are these 3 values here; let’s open a new chart window, click on: “Insert/ Indicators/ Oscillators/ MACD”, now you should see the values: fast EMA 12, slow EMA 26, MACD SMA 9, apply to close, that’s exactly what we are using here, so let’s click on “OK” and the MACD should appear in the lower part of your chart. Let’s right-click select “Templates/ Save Template” and save it as “tester.tpl” because this is the template that is going to be used in the backtest but before we can do that we need to finish our Expert Advisor here.
If the current value for the MACD is greater than 0 (zero) – zero is this line here – so if the signal line is above the zero line we would expect it to return to the zero line, that would be a sell signal so we assign the word: “sell” to our signal.
Otherwise if the MACD is below the zero line that would be a buy signal so we assign the word: “buy” to our signal, and if the signal equals buy and “OrdersTotal” equals zero that would mean we have no open orders we use the “OrderSend” command to buy 10 micro lot.
Otherwise, if the signal equals sell and we have no open orders we use “OrderSend” to sell 10 micro lot.
Finally, we use “Comment” to output the text: “The current signal is:” and the signal that we have calculated on the chart.
Let’s click on the “Compile” button here or press F7, you shouldn’t get any errors here but if it was too fast for you you might want to watch the other videos in this basic series first or maybe even the premium course is interesting for you.
If everything is okay you can click on the little button here or press F4 to go back to Metatrader and in Metatrader you want to click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleMacDEA.ex4”, enable the visual mode here and start a test.
Here we are! Our Expert Advisor is actually working we also have open positions and in this little video you have learned how to automate the MACD using MQL4 to open buy and sell trades on your chart and you have coded it yourself with a few lines of MQL4 code.
Download “CODE - SIMPLE MACD EA”SimpleMacDEA.mq4 – Downloaded 1143 times – 766.00 B