Forex
Google

Go Back   Forex Trading > Downloads > Indicators - Metatrader 4
Forex Forum Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-14-2007, 01:37 AM
Linuxser's Avatar
Linuxser Linuxser is offline
Moderator
 
Join Date: May 2006
Location: Helliconia (Autumn)
Posts: 2,162
Linuxser has disabled reputation
MACD Code - Question for programmers.

Hello.

I need the MACD indicator

Well, not really.

By accident I plotted two different MACD indicators on the same chart and what I saw was a little confusing. So, I added a third one.

As you can see in the picture.

From above to below.
MACD 1 is the normal code of Metaquotes MACD standar which comes with the software. With the addition of the histogram.

MACD 2 is a popular version called imacd or realmacd

MACD 3 is Metaquotes "pure" MACD.

If you take a little attention to the picture, the slope of signal line in MACD 2 (middle) is different.

After saw that the first thing comes to my mind was: wrong code.

But, I decided to compare with another charting software to discard any doubt and ouch. VTS have the slope like the middle MACD, also Accucharts, also TS2ki and other one.

Also, to confirm the question I move to a big TF and avoid possible differences in the close of the candles.

Still in doubt Iīd take little research. I donīt have the phone of Mr Appel, so I have to search in google.

In this link Iīve found nice material to start.

http://tadoc.org/indicator/MACD.htm

Then I was to Investopedia and found some articles, hereīs a cut:

Quote:
To completely understand the MACD indicator one needs to recognize that the relationship between the fast line and the slow line is of the utmost importance. The fast line results from the difference between the 26-day EMA of the closing prices subtracted from the 12-day EMA of the closing prices giving the calculation for the fast line. The slow signal is a moving average of the fast line. You can determine the slow signal by calculating the nine-day EMA of the fast line, and this is often referred to as the moving average of the fast line. The calculation for the fast line is 12 and 26 days, or in some cases, weeks, but you will find that all software programs available will use days rather than weeks. The defaults are easily changed if the technician wishes to changes the time period.
But lucky, I have the article The MACD indicator revisited. And whatīs Mr Ehlers says:

Quote:
A 26-day EMA is the first moving average and a 13-day EMA is the second moving average in a traditional MACD. The MACD line is formed by subtracting the long (first) moving average from the short (second) moving average. A signal line is formed by smoothing the MACD line with a third EMA. The third moving average is usually a 10-day EMA
Also, the Whealt-lab guys says:

Quote:
Calculation

MACD is constructed by subtracting the value of a 26 day Exponential Moving Average (EMA) from a 12 period EMA. The shorter EMA is constantly converging toward, and diverging away from, the longer EMA. This causes MACD to oscillate around the zero level.

MACD line = EMA(12, close) – EMA(26, close)

MACD Signal = EMA(9, MACD Line)

EMA= Exponential Moving Average

MACD line = MACD fast line, displayed as a solid line on the chart

MACD Signal = MACD signal line or slow line, displayed as a dashed line on the chart

Also, take a look at the code of IMACD.

PHP Code:
//+------------------------------------------------------------------+
//|                                                         MACD.mq4 |
//|                                Copyright Đ 2005, David W. Thomas |
//|                                           mailto:davidwt@usa.net |
//+------------------------------------------------------------------+
// This is the correct computation and display of MACD.
#property copyright "Copyright Đ 2005, David W. Thomas"
#property link      "mailto:davidwt@usa.net"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Green

//---- input parameters
extern int       FastMAPeriod=12;
extern int       SlowMAPeriod=26;
extern int       SignalMAPeriod=9;

//---- buffers
double MACDLineBuffer[];
double SignalLineBuffer[];
double HistogramBuffer[];

//---- variables
double alpha 0;
double alpha_1 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
   
//---- indicators
   
SetIndexStyle(0,DRAW_LINE);
   
SetIndexBuffer(0,MACDLineBuffer);
   
SetIndexDrawBegin(0,SlowMAPeriod);
   
SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   
SetIndexBuffer(1,SignalLineBuffer);
   
SetIndexDrawBegin(1,SlowMAPeriod+SignalMAPeriod);
   
SetIndexStyle(2,DRAW_HISTOGRAM);
   
SetIndexBuffer(2,HistogramBuffer);
   
SetIndexDrawBegin(2,SlowMAPeriod+SignalMAPeriod);
   
//---- name for DataWindow and indicator subwindow label
   
IndicatorShortName("MACD("+FastMAPeriod+","+SlowMAPeriod+","+SignalMAPeriod+")");
   
SetIndexLabel(0,"MACD");
   
SetIndexLabel(1,"Signal");
   
//----
    
alpha 2.0 / (SignalMAPeriod 1.0);
    
alpha_1 1.0 alpha;
   
//----
   
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   
//---- 
   
   //----
   
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   
int limit;
   
int counted_bars IndicatorCounted();
   
//---- check for possible errors
   
if (counted_bars<0) return(-1);
   
//---- last counted bar will be recounted
   
if (counted_bars>0counted_bars--;
   
limit Bars counted_bars;

   for(
int i=limiti>=0i--)
   {
      
MACDLineBuffer[i] = iMA(NULL,0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,i);
      
SignalLineBuffer[i] = alpha*MACDLineBuffer[i] + alpha_1*SignalLineBuffer[i+1];
      
HistogramBuffer[i] = MACDLineBuffer[i] - SignalLineBuffer[i];
   }
   
   
//----
   
return(0);
}
//+------------------------------------------------------------------+ 
Attached Images
File Type: gif macd.gif (17.0 KB, 265 views)

Last edited by Linuxser : 03-14-2007 at 02:26 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-14-2007, 02:47 AM
Linuxser's Avatar
Linuxser Linuxser is offline
Moderator
 
Join Date: May 2006
Location: Helliconia (Autumn)
Posts: 2,162
Linuxser has disabled reputation
Ok, now, letīsee the a couple of things.

One big difference in the code of iMACD (Iīm trying to contact them) is the way to calculate the signal line.

First, he add two vars

PHP Code:
    alpha 2.0 / (SignalMAPeriod 1.0);
    
alpha_1 1.0 alpha
and then he use this values to multiply the signal line.

Why? hereīs the answer

PHP Code:
     SignalLineBuffer[i] = alpha*MACDLineBuffer[i] + alpha_1*SignalLineBuffer[i+1]; 
The technical explanation is: because

Mmm, wait a minute. Appel and all explanations says EMA, but I saw an SMA somewhere.

Where? In the original Metaquotes MACD, check by yourself.

Quote:
ind_buffer2[i]=iMAOnArray(ind_buffer1,Bars,SignalSMA,0,MODE_SMA, i);
Again the same question, who is right? who is wrong?

So, if we change just one letter we can obtain a line like this

Quote:
ind_buffer2[i]=iMAOnArray(ind_buffer1,Bars,SignalSMA,0,MODE_EMA, i);

And the slope of the signal line start to be close with the aforementioned software.

Take a look at the next picture

Programmers, what you think?

Who is right? Who is wrong? Iīm right? Iīm wrong?

Itīs bug in the Metaquotes MACD code?

macdg.gif
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-14-2007, 05:24 AM
4x4ever 4x4ever is offline
Junior Member
 
Join Date: Feb 2007
Posts: 1
4x4ever is on a distinguished road
Looks like a bug in MetaQuotes MACD. Why don't you go to MQL4.com and submit this bug on their forum? Having said thata - it doesn't mean they will fix it. I have submitted at least 5 bugs with them and they refused to fix any of them...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-14-2007, 11:42 PM
Linuxser's Avatar
Linuxser Linuxser is offline
Moderator
 
Join Date: May 2006
Location: Helliconia (Autumn)
Posts: 2,162
Linuxser has disabled reputation
Quote:
Originally Posted by 4x4ever
Looks like a bug in MetaQuotes MACD. Why don't you go to MQL4.com and submit this bug on their forum? Having said thata - it doesn't mean they will fix it. I have submitted at least 5 bugs with them and they refused to fix any of them...
Well. I have not much luck posting on that forum.

What other bugs did you find?

My solution is my own macd, see picture

First MACD in the picture is the original, other is mine.

macdlnx.gif.

Last edited by Linuxser : 03-14-2007 at 11:46 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to code? samahdi Metatrader Programming 1068 Yesterday 10:07 PM
Question for programmers onelesstick General Discussion 2 10-27-2006 08:09 PM


All times are GMT. The time now is 02:52 AM.