MACD_ColorHist_Alert
Download scriptOverview of the MACD Color Histogram Alert Script
This script is a specialized technical analysis tool designed for the MetaTrader 4 platform. It enhances the standard Moving Average Convergence Divergence (MACD) indicator by providing visual cues through a multi-colored histogram and an automated notification system to alert traders of specific market movements.
Core Functional Logic
The script processes market data to identify momentum and trend shifts, categorizing them into several distinct behaviors:
1. Visual Histogram Dynamics Unlike a standard MACD that typically shows a single line or a basic histogram, this tool splits the visual representation into multiple colors based on two factors: the value of the MACD relative to the zero line (positive or negative) and the current direction of the momentum (rising or falling).
- It identifies when the momentum is strengthening in its current direction.
- It highlights when the momentum is beginning to weaken or "decline," even if it hasn't crossed a significant threshold yet.
2. Trend Identification & Signal Tracking The logic continuously monitors three specific data points:
- The MACD Line: The primary trend indicator.
- The Signal Line: A secondary moving average used to smooth the MACD for identifying "crosses."
- The Zero Line: The equilibrium point used to determine whether the overall market sentiment is bullish (above zero) or bearish (below zero).
Automated Alert System
One of the primary features of this script is its ability to "watch" the charts and notify the user instantly when specific conditions are met. It can be configured to send both Sound Alerts and Email Notifications for the following events:
- Zero Line Crossings: Triggered when the MACD line moves from negative to positive (bullish) or positive to negative (bearish).
- Signal Line Crosses: Triggered when the MACD line crosses above or below its own Signal Line, often used by traders to time entry and exit points.
- Directional Changes: The script monitors the "slope" of the momentum. It can alert you specifically when:
- The momentum is rising/falling while remaining in positive territory.
- The momentum is rising/falling while remaining in negative territory.
Customization and User Control
The script includes several user-friendly toggles that allow for a tailored experience:
- Visibility Controls: Users can choose to turn on or off the Histogram, the MACD Line, and the Signal Line independently to declutter the chart.
- Alert Filters: Traders can selectively enable or disable specific types of alerts (e.g., only want Zero Line alerts but not Directional Change alerts).
- Confirmation Settings: There is a specific logic option to require confirmation from multiple bars before triggering a directional change alert, which helps in filtering out "market noise" or minor fluctuations.
//+------------------------------------------------------------------+
//| MACD_ColorHist_Alert.mq4 |
//| Copyright © 2006, Robert Hill |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Robert Hill"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 SteelBlue
#property indicator_color2 SteelBlue
#property indicator_color3 Red
#property indicator_color4 Red
#property indicator_color5 White
#property indicator_color6 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 1
#property indicator_width6 1
//---- indicator parameters
extern int FastEMA =5; //2
extern int SlowEMA =35; //8
extern int SignalSMA=5; //6
//extern int OsmaMultiplier =1;
extern bool ShowHistogram = true;
extern bool ShowMACDLine = true;//true
extern bool ShowSignalLine = true;//false
extern bool SoundAlert =true;
extern bool EmailAlert =false;
extern bool MACDzeroLineCrossAlert = true;
extern bool MACDSignalLineCrossAlert = true;
extern bool MACDDirectionChangeAlert = true;
extern bool TwoBarsDirChageConfirm = false;
extern bool ShowAlertStatus = true;
//---- indicator buffers
double ind_buffer1[];
double ind_buffer2[];
double HistogramBufferUp[];
double HistogramBufferDown[];
double HistogramBufferUp1[];
double HistogramBufferDown1[];
int flagval11 = 0;
int flagval12 = 0;
int flagval21 = 0;
int flagval22 = 0;
int flagval31 = 0;
int flagval32 = 0;
int flagval41 = 0;
int flagval42 = 0;
//---- variables
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- drawing settings
// IndicatorBuffers(3);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
SetIndexBuffer(0,HistogramBufferUp);//lime
SetIndexBuffer(1,HistogramBufferDown);//red
SetIndexBuffer(2,HistogramBufferUp1);//green
SetIndexBuffer(3,HistogramBufferDown1);//maroon
SetIndexBuffer(4,ind_buffer1);//aqua
SetIndexBuffer(5,ind_buffer2);//yellow
if(ShowMACDLine)
{
SetIndexStyle(4,DRAW_LINE);
SetIndexDrawBegin(4,SlowEMA);
}
else
{
SetIndexStyle(4,DRAW_NONE);
}
if(ShowSignalLine)
{
SetIndexStyle(5,DRAW_LINE);
SetIndexDrawBegin(5,SignalSMA);
}
else
{
SetIndexStyle(5,DRAW_NONE);
}
if(ShowHistogram)
{
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexStyle(2,DRAW_HISTOGRAM);
SetIndexStyle(3,DRAW_HISTOGRAM);
}
else
{
SetIndexStyle(0,DRAW_NONE);
SetIndexStyle(1,DRAW_NONE);
SetIndexStyle(2,DRAW_NONE);
SetIndexStyle(3,DRAW_NONE);
}
// SetIndexDrawBegin(2,SlowEMA + SignalSMA);
//---- name for DataWindow and indicator subwindow label
if (ShowAlertStatus)
{
string _SoundAlert="",_EmailAlert="";
if(SoundAlert) _SoundAlert="ON";
if(!SoundAlert)_SoundAlert="OFF";
if(EmailAlert) _EmailAlert="ON";
if(!EmailAlert)_EmailAlert="OFF";
}
IndicatorShortName("MACDHist_Alert ("+FastEMA+","+SlowEMA+","+SignalSMA+") ");
SetIndexLabel(4,"MACD");
SetIndexLabel(5,"Signal");
SetIndexLabel(0,"");
SetIndexLabel(1,"");
SetIndexLabel(2,"");
SetIndexLabel(3,"");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
int start()
{
int limit;
double temp,temp1;
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;
//---- macd counted in the 1-st buffer
for(int i=0; i<limit; i++)
ind_buffer1[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 buffer
for(i=0; i<limit; i++)
ind_buffer2[i]=iMAOnArray(ind_buffer1,Bars,SignalSMA,0,MODE_SMA,i);
for(i=0; i<limit; i++)
{
HistogramBufferUp[i] = 0.0;//lime
HistogramBufferDown[i] = 0.0;//red
HistogramBufferUp1[i] = 0.0;//green
HistogramBufferDown1[i] = 0.0;//maroon
// if (OsmaMultiplier<=0)OsmaMultiplier=1;
// temp = ind_buffer1[i]*OsmaMultiplier;
// temp1 = ind_buffer1[i+1]*OsmaMultiplier;
temp = ind_buffer1[i];
temp1 = ind_buffer1[i+1];
if (temp >0)
{
if (temp>temp1) //>0up
{
HistogramBufferUp[i] = temp;
}
else //>0dn
{
HistogramBufferUp [i] = 0.0;
HistogramBufferUp1[i] = temp;
}
}
else
{
if (temp>temp1) //<=0 up
{
HistogramBufferDown[i] = temp;
}
else //<=0 dn
{
HistogramBufferDown[i] = 0.0;
HistogramBufferDown1[i] = temp;
}
//---
}
//-
if (i == 1)
{
if ((ind_buffer1[i] > 0)&&(ind_buffer1[i+1] < 0))
{
// macd Cross 0 up
if (flagval11==0)
{
flagval11=1;
flagval12=0;
flagval32=1;
if ((SoundAlert)&&(MACDzeroLineCrossAlert)) Alert (Symbol()," M",Period(), " MACD is crossed Up 0");
if ((EmailAlert)&&(MACDzeroLineCrossAlert)) SendMail("MACD CrossedZero Up", "MACD CrossedZero Up, Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
}
}
//
else if ((ind_buffer1[i]<0 )&&(ind_buffer1[i+1] >0))
{
//macd Cross 0 down
if (flagval12==0)
{
flagval11=0;
flagval12=1;
if ((SoundAlert)&&(MACDzeroLineCrossAlert)) Alert (Symbol()," M",Period(), " MACD is crossed Down 0");
if ((EmailAlert)&&(MACDzeroLineCrossAlert)) SendMail("MACD Crossed Zero Down","MACD CrossedZero Down, Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
}
}
//
else if ((ind_buffer1[i] < ind_buffer2[i])&&(ind_buffer1[i+1] > ind_buffer2[i+1]))
{
//Macd xSigDn
if (flagval21==0)
{
flagval21=1;
flagval22=0;
flagval41=1;
if ((SoundAlert)&&(MACDSignalLineCrossAlert)) Alert (Symbol()," M",Period(), " MACD Signal Line crossed & going Down");
if ((EmailAlert)&&(MACDSignalLineCrossAlert)) SendMail("MACD xSignalLane & going Down", "MACD xSignalLane & going Down, Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
}
}
else if ((ind_buffer1[i] > ind_buffer2[i])&&(ind_buffer1[i+1] < ind_buffer2[i+1]))
{
//Macd xSigUp
if (flagval22==0)
{
flagval21=0;
flagval22=1;
if ((SoundAlert)&&(MACDSignalLineCrossAlert)) Alert (Symbol()," M",Period(), " MACD Signal Line crossed & going Up");
if ((EmailAlert)&&(MACDSignalLineCrossAlert)) SendMail("MACD xSignalLane & going Up", "MACD xSignalLane & going Up, Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
}
}
//
else if (((ind_buffer1[i]>0)&&(ind_buffer1[i] < ind_buffer1[i+1])&&(ind_buffer1[i+1] < ind_buffer1[i+2])&&(TwoBarsDirChageConfirm))||
((ind_buffer1[i]>0)&&(ind_buffer1[i] < ind_buffer1[i+1])&&(!TwoBarsDirChageConfirm)))
{
// >0dn
if (flagval31==0)
{
flagval31=1;
flagval32=0;
if ((SoundAlert)&&(MACDDirectionChangeAlert)) Alert (Symbol()," M",Period(), " MACD>0 is Declining...... ");
if ((EmailAlert)&&(MACDDirectionChangeAlert)) SendMail("MACD Declining...", "MACD fDeclining.., Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
}
}
else if (((ind_buffer1[i]>0)&&(ind_buffer1[i] > ind_buffer1[i+1])&&(ind_buffer1[i+1] > ind_buffer1[i+2])&&(TwoBarsDirChageConfirm))||
((ind_buffer1[i]>0)&&(ind_buffer1[i] > ind_buffer1[i+1])&&(!TwoBarsDirChageConfirm)))
{
// >0up
if (flagval32==0)
{
flagval31=0;
flagval32=1;
if ((SoundAlert)&&(MACDDirectionChangeAlert)) Alert (Symbol()," M",Period(), " MACD>0 is Rising...... ");
if ((EmailAlert)&&(MACDDirectionChangeAlert)) SendMail("MACD Rising...", "MACD Rising.., Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
}
}
else if (((ind_buffer1[i]<=0)&&(ind_buffer1[i] < ind_buffer1[i+1])&&(ind_buffer1[i+1] < ind_buffer1[i+2])&&(TwoBarsDirChageConfirm))||
((ind_buffer1[i]<=0)&&(ind_buffer1[i] < ind_buffer1[i+1])&&(!TwoBarsDirChageConfirm)) )
{
// <0dn
if (flagval41==0)
{
flagval41=1;
flagval42=0;
if ((SoundAlert)&&(MACDDirectionChangeAlert)) Alert (Symbol()," M",Period(), " MACD<0 is Declining...... ");
if ((EmailAlert)&&(MACDDirectionChangeAlert)) SendMail("MACD Declining...", "MACD fDeclining.., Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
}
}
//
else if (((ind_buffer1[i]<=0)&&(ind_buffer1[i] > ind_buffer1[i+1])&&(ind_buffer1[i+1] > ind_buffer1[i+2])&&(TwoBarsDirChageConfirm))||
((ind_buffer1[i]<=0)&&(ind_buffer1[i] > ind_buffer1[i+1])&&(!TwoBarsDirChageConfirm)))
{
// <0up
if (flagval42==0)
{
flagval41=0;
flagval42=1;
if ((SoundAlert)&&(MACDDirectionChangeAlert)) Alert (Symbol()," M",Period(), " MACD<0 is Rising...... ");
if ((EmailAlert)&&(MACDDirectionChangeAlert)) SendMail("MACD Rising...", "MACD Rising.., Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
}
}
//
}
//-
}
//---- done
return(0);
}
Comments