MT4 MACD Alert ?

Agent86

Sergeant
Messages
194
Hi

I have downloaded these two .mq4 files for MACD

One of them appears to be the fix for the MT4 because the current MACD does not appear to be correct or perhaps non standard compared to MACD of other charting packages.

Anyhow I want the (MACD Crossover alert) to match the (MACD True)

If anyone could tell me which part of the MACD Crossover I should change to make them correctly please?:

MACD TRUE:
//+------------------------------------------------------------------+

//| 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>0) counted_bars--;

limit = Bars - counted_bars;



for(int i=limit; i>=0; i--)

{

MACDLineBuffer = iMA(NULL,0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,i);

SignalLineBuffer = alpha*MACDLineBuffer + alpha_1*SignalLineBuffer[i+1];

HistogramBuffer = MACDLineBuffer - SignalLineBuffer;

}



//----

return(0);

}

//+------------------------------------------------------------------+



MACD Crossover:
//+------------------------------------------------------------------+
//| MACD-Crossover_Signal.mq4 |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2007, Robert Hill)"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LimeGreen
#property indicator_color2 Red

double CrossUp[];
double CrossDown[];
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexArrow(0, 233);
SetIndexBuffer(0, CrossUp);
SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 234);
SetIndexBuffer(1, CrossDown);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() {
int limit, i, counter;
double MACD_Main, MACD_Signal, MACD_MainPrevious, MACD_SignalPrevious;
double Range, AvgRange;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

for(i = 0; i <= limit; i++) {

counter=i;
Range=0;
AvgRange=0;
for (counter=i ;counter<=i+9;counter++) {
AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
}
Range=AvgRange/10;

MACD_Main = iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_MAIN, i);
MACD_MainPrevious = iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_MAIN, i+1);
MACD_Signal = iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, i);
MACD_SignalPrevious = iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, i+1);


if ((MACD_Signal < MACD_Main) && (MACD_SignalPrevious > MACD_MainPrevious)) {
CrossUp = Low - Range*0.5;
}
else if ((MACD_Signal > MACD_Main) && (MACD_SignalPrevious < MACD_MainPrevious)) {
CrossDown = High + Range*0.5;
}
}
return(0);
}




Basically the MACD Crossover appears to be a little off. The arrows do not appear in sync with the actual crossover. Many of them are correct, however some arrows appear when no MACD True is showing a crossover. ?

I wish the indicator would allow me to make changes from the panel instead of in the script part, but I don't know how. Perhaps as I learn the MQL4 I will know someday.

Please advise
Thanks
 
Last edited:
Back
Top