MACD colored_002

Author: mladen
MACD colored_002

Overview of the MACD Colored Indicator

This script is a specialized technical analysis tool designed for MetaTrader 4. It enhances the traditional Moving Average Convergence Divergence (MACD) indicator by providing visual color coding and versatile calculation options to help traders identify momentum shifts and trend directions more intuitively.

Core Functionality

1. Versatile Calculation Modes Unlike standard indicators that only look at price, this script allows you to apply the MACD logic to various different types of market data. You can choose from:

  • Regular MACD: The standard calculation based on moving averages.
  • ZeroLag MACD: A variation designed to reduce the "lag" (delay) typically found in traditional moving average calculations, reacting faster to price changes.
  • RSI-Based: Applies the logic to the Relative Strength Index.
  • Momentum-Based: Focuses on the speed of price movement.
  • CCI-Based: Utilizes the Commodity Channel Index for identification of overbought or oversold conditions.

2. Visual Color Coding (The Histogram) The primary feature of this script is how it handles the "Histogram" (the vertical bars). Instead of a single uniform color, the script dynamically changes colors based on the relationship between the current value and the previous value:

  • Bullish Momentum: When the indicator moves upward, the bars change to one color (e.g., Green) to signal strengthening positive momentum.
  • Bearish Momentum: When the indicator moves downward, the bars change to a different color (e.g., Red) to highlight weakening strength or negative movement.
  • Dynamic Switching: The script automatically switches these colors the moment the direction of the trend flips.

3. Optional Oscillator Overlay (OSMA) The script includes an option to display the On Balance Volume Analysis (OSMA). When enabled, it calculates the difference between two specific lines and applies the same color-changing logic described above, allowing for a more complex view of market volume and price relationship.

Alert System

The script is equipped with a notification engine that monitors the interaction between the "MACD Line" and the "Signal Line." It can trigger three types of notifications:

  • On-Screen Alerts: Pop-up windows appearing on your trading platform.
  • Audio Alerts: Sound files to notify you of a crossover.
  • Email Notifications: Automated emails sent to your linked account.

These alerts specifically trigger when the Signal Line crosses the MACD line, which is a common signal used by traders to identify potential entry or exit points in a trend.

Summary for the User

Essentially, this tool takes the complex math of the MACD and translates it into a high-contrast visual map. By using color changes to represent momentum shifts and offering various "input types" (like RSI or CCI), it allows you to see not just where the price is going, but how fast it is moving in different types of market conditions.

Indicators Used
Relative strength indexMomentum indicatorCommodity channel indexMoving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screenIt plays sound alertsIt sends emails
8 Views
0 Downloads
0 Favorites
MACD colored_002
//+------------------------------------------------------------------+
//|                                                 MACD colored.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers  7
#property indicator_color1   Green
#property indicator_color2   Green
#property indicator_color3   Red
#property indicator_color4   Red
#property indicator_color5   Gray
#property indicator_color6   Gold
#property indicator_color7   DimGray
#property indicator_width1   1
#property indicator_width2   2
#property indicator_width3   1
#property indicator_width4   2
#property indicator_width5   2
#property indicator_width6   1

extern string _            = "MACD settings";
extern int    FastEMA      = 12;
extern int    SlowEMA      = 26;
extern int    SignalEMA    = 9;
extern int    PriceType    = PRICE_CLOSE;

//
//
//
//
//

extern bool ShowHisto = true;
extern bool ShowOSMA  = false;
extern int  MACDType  = 0;
extern int  RSIPeriod = 14;
extern int  MOMPeriod = 14;
extern int  CCIPeriod = 10;

//
//
//
//
//

extern string  __              = "alerts";
extern bool    alertsOn        = false;
extern bool    alertsMessage   = true;
extern bool    alertsSound     = false;
extern bool    alertsEmail     = false;

extern string  note_MACD_Types_     = "0'regular'MACD 1ZeroLag MACD 2MACDofRSI  3MACDofMomentum 4MACDofCCI";

//
//
//
//
//

double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
double buffer5[];
double buffer6[];
double buffer7[];
double buffer8[];
string IndicatorName;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0,buffer1);
   SetIndexBuffer(1,buffer2);
   SetIndexBuffer(2,buffer3);
   SetIndexBuffer(3,buffer4);
   SetIndexBuffer(4,buffer5);
   SetIndexBuffer(5,buffer6);
   SetIndexBuffer(6,buffer7);
   SetIndexBuffer(7,buffer8);
   if (ShowOSMA)
   {
         ShowHisto = true;
         SetIndexStyle(6,DRAW_LINE);
   }         
   else  SetIndexStyle(6,DRAW_NONE);
   if (ShowHisto)
   {
      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);
   }      
   
   //
   //
   //
   //
   //
   
   string ShortName;
   if  (MACDType==1)
         ShortName = "ZeroLag MACD (";
   else  ShortName = "MACD (";
         ShortName = ShortName+FastEMA+","+SlowEMA+","+SignalEMA+")";
         switch (MACDType)
         {
            case 2: ShortName = ShortName+" of RSI("     +RSIPeriod+")"; break;
            case 3: ShortName = ShortName+" of momentum("+MOMPeriod+")"; break;
            case 4: ShortName = ShortName+" of CCI("     +CCIPeriod+")"; break;
         }
         IndicatorShortName(ShortName);

   //
   //
   //
   //
   //

   IndicatorName = WindowExpertName();
   return(0);
}
int deinit()
{
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   double signalAlpha = 2.0/(1.0+SignalEMA);
   int  counted_bars  = IndicatorCounted();
   int  i,limit; 
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
      limit = Bars - counted_bars;

   //
   //
   //
   //
   //

   if (_ == "DoingZeroLag")  { CalculateZeroLag(limit); return(0); }
   
   //
   //
   //
   //
   //
                  
   for(i = limit; i >= 0 ; i--)
   {
      switch (MACDType)
      {
         case 2:  buffer8[i] = iRSI(NULL,0,RSIPeriod,PriceType,i);      break;
         case 3:  buffer8[i] = iMomentum(NULL,0,MOMPeriod,PriceType,i); break;
         case 4:  buffer8[i] = iCCI(NULL,0,CCIPeriod,PriceType,i);      break;
         default: buffer8[i] = iMA(NULL,0,1,0,MODE_SMA,PriceType,i);    break;
      }               
   }
   for(i = limit; i >= 0 ; i--)
   {
      if (MACDType == 1)
      {
         buffer5[i] = iCustom(NULL,0,IndicatorName,"DoingZeroLag",FastEMA,SlowEMA,SignalEMA,PriceType,0,i);
         buffer6[i] = iCustom(NULL,0,IndicatorName,"DoingZeroLag",FastEMA,SlowEMA,SignalEMA,PriceType,1,i);
      }               
      else
      {
         buffer5[i] = iMAOnArray(buffer8,0,FastEMA,0,MODE_EMA,i)-
                      iMAOnArray(buffer8,0,SlowEMA,0,MODE_EMA,i);
         buffer6[i] = buffer6[i+1]+ signalAlpha*(buffer5[i]-buffer6[i+1]);
      }            

      //
      //
      //
      //
      //
                          
      buffer1[i]= EMPTY_VALUE;
      buffer2[i]= EMPTY_VALUE;
      buffer3[i]= EMPTY_VALUE;
      buffer4[i]= EMPTY_VALUE;
      if (!ShowOSMA)
         {
            if (buffer5[i]>0) 
               {
                  if (buffer5[i]>buffer5[i+1]) 
                        buffer2[i]=buffer5[i];
                  else  buffer1[i]=buffer5[i];
               }            
            else    
               {         
                  if (buffer5[i]<buffer5[i+1]) 
                        buffer4[i]=buffer5[i];
                  else  buffer3[i]=buffer5[i];
              }
         }
      else
         {
            buffer7[i] = buffer5[i]-buffer6[i];
            if (buffer7[i]>0) 
               {
                  if (buffer7[i]>buffer7[i+1]) 
                        buffer2[i]=buffer7[i];
                  else  buffer1[i]=buffer7[i];
               }            
            else    
               {         
                  if (buffer7[i]<buffer7[i+1]) 
                        buffer4[i]=buffer7[i];
                  else  buffer3[i]=buffer7[i];
              }
         }
   }

   //
   //
   //
   //
   //

   if (alertsOn)
   {
      //
      //
      //
      //
      //
      
      if (buffer6[0]>buffer5[0] && buffer6[1] < buffer5[1]) doAlert("signal line crossed macd line up"); 
      if (buffer6[0]<buffer5[0] && buffer6[1] > buffer5[1]) doAlert("signal line crossed macd line down"); 
   }   
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void CalculateZeroLag(int limit)
{
   int    i;
   double maf,mas;
   
   //
   //
   //
   //
   //
   
   for(i=limit;i>=0;i--)
   {
      buffer3[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PriceType,i);
      buffer4[i]=iMA(NULL,0,SlowEMA,0,MODE_EMA,PriceType,i);
   }
   for(i=limit;i>=0;i--)
   {
      maf=buffer3[i]+buffer3[i]-iMAOnArray(buffer3,0,FastEMA,0,MODE_EMA,i);
      mas=buffer4[i]+buffer4[i]-iMAOnArray(buffer4,0,SlowEMA,0,MODE_EMA,i);
          buffer1[i]=maf-mas;
   }
   
   //
   //
   //
   //
   //
   
   for(i=limit;i>=0;i--) buffer5[i]=                      iMAOnArray(buffer1,0,SignalEMA,0,MODE_EMA,i);
   for(i=limit;i>=0;i--) buffer2[i]=buffer5[i]+buffer5[i]-iMAOnArray(buffer5,0,SignalEMA,0,MODE_EMA,i);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void doAlert(string forWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
   if (previousAlert != forWhat || previousTime != Time[0]) {
       previousAlert  = forWhat;
       previousTime   = Time[0];
       message        = StringConcatenate(Symbol()," ",forWhat," at ",TimeToStr(TimeLocal(),TIME_SECONDS));
           if (alertsMessage) Alert(message);
           if (alertsSound)   PlaySound("alert2.wav");
           if (alertsEmail)   SendMail(StringConcatenate(Symbol()," MACD crossing"),message);
   }           
}

Comments