Simple code for MACD within EA, self contained

 

Hi all

 I'm new to MQL. I apologise in advance if I'm asking a question that has probably been answered lots of times already, I have looked around on the forum trying to find what I need.

Simply, I'd like a snippet of code the calculates MACD and the Signal properly using EMA not the function built in to MQL that uses SMA and gives slightly different output. Is there a way to quickly do this within my own EA script without referencing out to someone elses MACD indicator?  

or is it possible to do it slightly differently, and code a MACD indicator in my own script so something is displayed, and I can use the MACD variables from that, and use the rest of my EA script to do the other stuff such as order placement etc.

 

thanks

 
idh:

Hi all

 I'm new to MQL. I apologise in advance if I'm asking a question that has probably been answered lots of times already, I have looked around on the forum trying to find what I need.

Simply, I'd like a snippet of code the calculates MACD and the Signal properly using EMA not the function built in to MQL that uses SMA and gives slightly different output. Is there a way to quickly do this within my own EA script without referencing out to someone elses MACD indicator?  

or is it possible to do it slightly differently, and code a MACD indicator in my own script so something is displayed, and I can use the MACD variables from that, and use the rest of my EA script to do the other stuff such as order placement etc.

iMACD() implies that it uses EMA . .  otherwise take a look at this:  https://www.metatrader5.com/en/terminal/help/indicators/oscillators/macd
 
From iMACD - MQL4 Documentation
double iMACD( string symbol, int timeframe, int fast_ema_period, int slow_ema_period, int signal_period, int applied_price, int mode, int shift)
 

hi guys

 

thanks for replies. I was trying to avoid iMACD as I had read around various places saying that it wasn't right (which surprised me!) and plenty of people saying they use different calculation, its disappointing as I'd like to use the built in functions as much as possible

 

I have this code: MACD = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);

this gives me a MACD of -4.1935 on a 15 minute chart

if I look in another charting package, exact same price and based on 15 minute chart I get -23.1266 as the macd!

so something isn't quite right there.

Id like to pull out:

MACD
MACD Signal
MACD histogram (e.g. positive or negative number above or below zero line) 

 

I tried the code snippet found in https://www.metatrader5.com/en/terminal/help/indicators/oscillators/macd but the compiler complained that CLOSE was an undeclared variable, do I need to do an include on Momentum.mq4 somehow, to make use of the code?

  

thanks,
Ian

(UK) 

 
idh:

hi guys

I tried the code snippet found in https://www.metatrader5.com/en/terminal/help/indicators/oscillators/macd but the compiler complained that CLOSE was an undeclared variable, do I need to do an include on Momentum.mq4 somehow, to make use of the code?

That is the wrong code,  looks like a typo in the webpage . . . I think this is the correct code:  https://www.mql5.com/en/code/7774
 

thanks, I got it working. the calculated numbers are slightly different to that of macd in another charting package, but it seems small enough not to be too concerned about. thanks for help

 

I'm still having some trouble with this, could someone paste some code I could use please? I know you pointed out that link, but Im unsure how to use it as it's an indicator, and Im not too troubled about displaying anything (but would look better I suppose), but how can I access the MACD calculation function from my own code?

 

I wish metaquotes iMACD gave the same output as other MACD charting does, would make this a lot easier. Cant see why the numbers are slightly out on their implementation.

 
idh:

I'm still having some trouble with this, could someone paste some code I could use please? I know you pointed out that link, but Im unsure how to use it as it's an indicator, and Im not too troubled about displaying anything (but would look better I suppose), but how can I access the MACD calculation function from my own code?

If the history data is different then the MACD numbers will be different.
Reason: