MACD_ColorHist_Alert_001
Download scriptThis script is designed for the MetaTrader 4 platform to enhance the visual representation and notification system of the Moving Average Convergence Divergence (MACD) indicator. It transforms standard data into a color-coded histogram and provides real-time alerts based on specific price action movements.
Visual Enhancements
The primary role of this script is to provide a multi-layered, color-coded histogram that makes it easier to identify market momentum at a glance:
- Dynamic Coloring: Instead of a single uniform color, the histogram changes shades based on whether the MACD value is positive or negative and whether the current momentum is increasing or decreasing.
- Momentum Tracking: By comparing the current bar's value against the previous one, the script visually distinguishes between "Rising" and "Declining" trends in both bullish (above zero) and bearish (below zero) zones.
- Customizable View: Users can toggle the visibility of the main MACD line, the Signal line, and the Histogram itself to suit their preferred trading style.
Core Logic and Automations
The script monitors the relationship between the MACD line and the Signal line in real-time, triggering various notifications based on three distinct types of events:
1. Zero Line Crosses The script detects when the MACD line moves from a negative territory to a positive one (Crossing Up) or vice versa (Crossing Down). These are often viewed as primary trend shifts.
2. Signal Line Crosses It monitors the intersection between the MACD line and the Signal line. When the MACD crosses above the signal, it identifies upward momentum; when it crosses below, it identifies downward momentum.
3. Directional Changes The script tracks the "slope" of the histogram. It recognizes when the movement begins to shift—for example, identifying the exact moment the MACD starts to decline while still in positive territory. It includes a specific toggle to confirm these changes over two bars to filter out minor price fluctuations (noise).
Notification System
Whenever the logic detects one of the movements mentioned above, the script can proactively notify the trader through multiple channels:
- Sound Alerts: Audible beeps within the trading platform.
- Email Notifications: Automated emails containing the specific details of the cross (Symbol, Timeframe, and current Price).
- On-Screen Alerts: Pop-up messages clearly stating the type of movement detected (e.g., "MACD Rising," "Crossing Zero Up," etc.).
//+------------------------------------------------------------------+
//| 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 RoyalBlue
#property indicator_color2 RoyalBlue
#property indicator_color3 Red
#property indicator_color4 Red
#property indicator_color5 Aqua
#property indicator_color6 White
#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 = false;//true
extern bool ShowSignalLine = true;//false
extern bool SoundAlert =true;
extern bool EmailAlert =false;
extern bool MACDzeroLineCrossAlert = true;
extern bool MACDSignalLineCrossAlert = False;
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("MACD Hist_Alert "+_SoundAlert+" ("+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 ("MACD crossed Zero Up! ",Symbol()," TF M",Period()," @ ",Bid);
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 ("MACD crossed Zero Down! " ,Symbol()," TF M",Period()," @ ",Bid);
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 ("MACD crossed SignalLane & going Down! ",Symbol()," TF M",Period()," @ ",Bid);
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 ("MACD crossed SignalLane & going Up! ",Symbol()," TF M",Period()," @ ",Bid);
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 ("MACD>0 Declining... ",Symbol()," TF M",Period()," @ ",Bid);
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 ("MACD>0 Rising... ",Symbol()," TF M",Period()," @ ",Bid);
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 ("MACD<0 Declining... ",Symbol()," TF M",Period()," @ ",Bid);
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 ("MACD<0 Rising... ",Symbol()," TF M",Period()," @ ",Bid);
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