MACD colored

Download script
Author: mladen
MACD colored

This tool is a specialized technical analysis indicator designed to visualize market momentum and trends through a modified version of the Moving Average Convergence Divergence (MACD) system. Unlike standard versions, this script focuses on visual clarity by using a multi-colored histogram to indicate the strength and direction of price movement in real-time.

Core Functionality

The primary logic of the script revolves around calculating the relationship between different moving averages to identify whether a market is accelerating or decelerating. It provides several distinct modes of operation depending on the user's preference:

  • Standard MACD Logic: The default mode calculates the difference between a "fast" and a "slow" moving average to determine current momentum.
  • Specialized Oscillators: The script can be configured to base its calculations on other popular market indicators, including RSI (Relative Strength Index), Momentum, or CCI (Commodity Channel Index). This allows users to apply MACD-style logic to different types of price data.
  • Zero-Lag Processing: It includes a specific "Zero-Lag" mode designed to reduce the delay typically found in traditional moving averages, aiming to provide a faster response to sudden price fluctuations.

Visual Logic (The Color System)

The standout feature of this script is its dynamic coloring logic. Instead of a single block of color, it splits the histogram into four distinct segments based on two criteria: Direction and Velocity.

  1. Trend Direction: The script identifies whether the current value is above or below the zero line (indicating bullish or bearish sentiment).
  2. Momentum Change: It compares the current bar's value against the previous bar's value.
    • If the momentum is moving in your favor and gaining strength, it uses one color.
    • If the momentum is moving in your favor but losing strength (slowing down), it switches to a different color.
    • The same logic applies to movements moving against you, using unique colors to highlight whether the "downward" or "upward" pressure is increasing or decreasing.

Alerts and Notifications

To ensure traders do not have to watch the screen constantly, the script includes an automated alert system. It monitors the "Signal Line" (a smoothed average of the MACD) and compares it to the "MACD Line." When these two lines cross each other—a classic signal for a potential change in trend—the script can trigger several types of notifications:

  • Visual Pop-ups: On-screen alerts.
  • Audio Signals: Sound files to notify you of a crossover.
  • Email Notifications: Direct messages sent to your registered email address.

Summary for the User

Essentially, this tool takes complex mathematical data and translates it into a color-coded "weather map" of market momentum. It tells you not just where the price is going, but how fast it is getting there, while providing automated alerts to catch significant turning points in the market.

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
15 Views
0 Downloads
0 Favorites
MACD colored
//+------------------------------------------------------------------+
//|                                                 MACD colored.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers  7
#property indicator_color1   Tomato
#property indicator_color2   Lime
#property indicator_color3   Green
#property indicator_color4   Red
#property indicator_color5   Aqua
#property indicator_color6   Red
#property indicator_color7   DimGray
#property indicator_width1   2
#property indicator_width2   4
#property indicator_width3   2
#property indicator_width4   4
#property indicator_width5   2
#property indicator_width6   2

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;

//
//
//
//
//

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 Colored (";
         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);
         IndicatorShortName("MACD Colored");

   //
   //
   //
   //
   //

   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