Forex Factory
  • Login

  • Username: Password:
  • 9:32pm

  • Search
  • Home

  • Forums

  • Trades

  • Calendar

  • News

  • Market

  • Brokers

Options

Search

Subscribe to Thread

Bookmark Thread

First Page First Unread Last Page Last Post

Printable Version

Similar Threads

Help with MACD & MA of MACD Cross indicator 0 replies

no MACD line on MT4 MACD indicator 1 reply

MACD + Bollinger EA (with Custom Indicators). Need HELP Coding! 0 replies

Coding MACD / Laguerre Arrow Signal?? HELP 5 replies

Does anybody has Traditional MACD ? 2 replies

  • Platform Tech
  • /
  • Reply to Thread
  • 1

Traditional MACD histograms vs MT4's MACD Coding

  • Post# 1
  • Quote
  • First Post: Feb 3, 2011 4:47pm
  • dogberry
    Joined Jul 2007 | 275 Posts | Status: Member
Hi, everyone,

I am trying to code an EA for a trading system in a classical book, and need to use the Traditional MACD histograms. However, the backtest seems to be very slow when I use the Traditional MACD histograms. The code is

Inserted Code
double MACDLineBuffer[100];
double SignalLineBuffer[100];
double HistogramBuffer[100];
alpha = 2.0 / (SignalMAPeriod + 1.0);
alpha_1 = 1.0 - alpha;
for(int i=100; i>=0; i--)
   {
      MACDLineBuffer[i] = iMA(NULL,10080,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,10080,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,i);
      SignalLineBuffer[i] = alpha*MACDLineBuffer[i] + alpha_1*SignalLineBuffer[i+1];
      HistogramBuffer[i] = MACDLineBuffer[i] - SignalLineBuffer[i];
   }
Could someone let me know which is better for predicting a trend, traditional MACD histograms or MT4's MACD?

Many thanks!
  • Post# 2
  • Quote
  • Feb 3, 2011 5:24pm
  • Xaphod
    Joined Mar 2010 | 910 Posts | Status: Member
Quoting dogberry
I am trying to code an EA for a trading system in a classical book, and need to use the Traditional MACD histograms. However, the backtest seems to be very slow when I use the Traditional MACD histograms. The code is ....
This is how you can call the MACD in MT4:
PHP Code:
dMACD[i]=iMACD(NULL,0,FastEMA,SlowEMA,Signal,PRICE_CLOSE, MODE_MAIN,i);
dSignal[i]=iMACD(NULL,0,FastEMA,SlowEMA,Signal,PRICE_CLOSE, MODE_SIGNAL,i); 
The iMACD is a traditional MACD, it is only the presentation in the chart that differs. Ie MT4 does not show the signal line in the chart.
  • Post# 3
  • Quote
  • Feb 3, 2011 6:21pm
  • dogberry
    Joined Jul 2007 | 275 Posts | Status: Member
You mean dMACD[i] i.e. MODE_MAIN is the traditional MACD?
Many thanks!


Quoting Xaphod
This is how you can call the MACD in MT4:
PHP Code:
dMACD[i]=iMACD(NULL,0,FastEMA,SlowEMA,Signal,PRICE_CLOSE, MODE_MAIN,i);
dSignal[i]=iMACD(NULL,0,FastEMA,SlowEMA,Signal,PRICE_CLOSE, MODE_SIGNAL,i); 
The iMACD is a traditional MACD, it is only the presentation in the chart that differs. Ie MT4 does not show the signal line in the chart.
  • Post# 4
  • Quote
  • Feb 3, 2011 6:48pm
  • Xaphod
    Joined Mar 2010 | 910 Posts | Status: Member
Did you read the links I posted explaining the MACD?
Attached Image (click to enlarge)
Click to Enlarge

Name: macd-exp.png
Size: 35 KB
  • Post# 5
  • Quote
  • Feb 4, 2011 4:18am | Edited at 6:10am
  • dogberry
    Joined Jul 2007 | 275 Posts | Status: Member
What I need is the mainstream MACD Histogram. Correct me if I am wrong. Many thanks!

Inserted Code
dMACD[i]=iMACD(NULL,0,FastEMA,SlowEMA,Signal,PRICE_CLOSE, MODE_MAIN,i);
dSignal[i]=iMACD(NULL,0,FastEMA,SlowEMA,Signal,PRICE_CLOSE, MODE_SIGNAL,i);  
Histogram[i]=dMACD[i]-dSignal[i];
Quoting Xaphod
Did you read the links I posted explaining the MACD?
  • Post# 6
  • Quote
  • Feb 4, 2011 5:04am
  • hanover
    Joined Sep 2006 | 5,002 Posts | Status: Gone AWOL for a few months.....
Info on MACD calculation here - how MT4 version differs from the mainstream. Not sure if it answers your question, however.

[EDIT] There's a (factory supplied) MT4 indy called 'Moving Average of Oscillator', which attempts to plot the mainstream MACD histogram.
I'm taking a rest from forums. Please don't expect replies to your posts.
  • Post# 7
  • Quote
  • Feb 6, 2011 11:53am
  • Xaphod
    Joined Mar 2010 | 910 Posts | Status: Member
As pointed out by hanover (see links in the above post) the MT4 MACD's signal uses the SMA method and the Traditional which uses the EMA method. So try this for a traditional MACD:
PHP Code:
dMACD[i]=iMACD(NULL,0,FastEMA,SlowEMA,Signal,PRICE_CLOSE, MODE_MAIN,i);
dSignal[i]=iMAOnArray(dMACD,Bars,Signal,0,MODE_EMA,i);
dHisto=dMACD[i]-dSignal[i]; 
  • Post# 8
  • Quote
  • Oct 1, 2011 1:08am
  • mt4fx
    Joined Oct 2011 | 1 Post | Status: Junior Member
Quoting Xaphod
As pointed out by hanover (see links in the above post) the MT4 MACD's signal uses the SMA method and the Traditional which uses the EMA method. So try this for a traditional MACD:
PHP Code:
dMACD[i]=iMACD(NULL,0,FastEMA,SlowEMA,Signal,PRICE_CLOSE, MODE_MAIN,i);
dSignal[i]=iMAOnArray(dMACD,Bars,Signal,0,MODE_EMA,i);
dHisto=dMACD[i]-dSignal[i]; 
I'm trying to use this code in an EA to represent a traditional MACD Histogram. While I've done a lot of programming, I've recently got involved with MT4.

The built-in OsMA indicator executes each of "those" lines within a separate For loop. Namely,

PHP Code:
//---- macd counted in the 1-st additional buffer
   
for(int i=0; i<limit; i++)
      
MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd additional buffer
   
for(i=0; i<limit; i++)
      
SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
//---- main loop
   
for(i=0; i<limit; i++)
      
OsmaBuffer[i]=MacdBuffer[i]-SignalBuffer[i]; 
Do I need the 3 loops in the EA as well? It seems terribly inefficient to recompute over the whole chart for each EA update. It seems like I ought to be able to just update the most recent value.

What am I missing?

Any insights greatly appreciated.
  • Post# 9
  • Quote
  • Last Post: Oct 2, 2011 2:22am | Edited at 2:58am
  • Xaphod
    Joined Mar 2010 | 910 Posts | Status: Member
Quoting mt4fx
I'm trying to use this code in an EA to represent a traditional MACD Histogram. While I've done a lot of programming, I've recently got involved with MT4.

The built-in OsMA indicator executes each of "those" lines within a separate For loop. Namely
...
Do I need the 3 loops in the EA as well? It seems terribly inefficient to recompute over the whole chart for each EA update. It seems like I ought to be able to just update the most recent value.
What am I missing?
Any insights greatly appreciated.
Yes, each of the loops is dependant on the data calculated in the previous loop. How else would one do it?

MT4 uses the indicator buffers to store previously calculated data, however one does not have access them in an EA. You can emulate the indicator buffers using the Array functions. ArraySetAsSeries(), ArrayResize() etc. That way you do not have to recalculate data. See this article for more help:
http://articles.mql4.com/501

Otherwise, the 'limit' variable in the for loop is used to calculate ahead of time the amount of bars you will be processing. You can limit this to only what is needed if you are using only the most recent bar(s) in the EA. For example, a 21 MA will need at least 21 bars to be correct.
Thread Tools Search this Thread
Show Printable Version Show Printable Version
Email This Thread Email This Thread
Search this Thread:

Advanced Search

  • Platform Tech
  • /
  • Traditional MACD histograms vs MT4's MACD Coding
  • Reply to Thread
0 traders viewing now

©2013 Forex Factory, Inc. / Terms of Use / Privacy Policy

Forex Factory® is a registered trademark.

Connect

  • Facebook
  • Twitter
  • RSS

Company

  • About FF
  • FF Blog
  • Careers at FF
  • Advertising
  • Contact FF

Products

  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer

Website

  • Homepage
  • Search
  • User Guide
  • Member List
  • Online Now
  • Report a Bug