BB_MACD_v1.03

Download script
BB_MACD_v1.03

Overview of the BB_MACD Indicator Logic

The BB_MACD is a custom technical analysis tool designed to provide traders with a visual representation of momentum and volatility. It works by merging two popular trading concepts: the Moving Average Convergence Divergence (MACD) and Bollinger Bands. Instead of applying these tools directly to price, this script applies them to the MACD values themselves, creating a specialized secondary layer of analysis.

Core Components of the Logic

1. The Foundation: MACD Calculation

At its base, the indicator identifies the difference between two Exponential Moving Averages (EMAs). One is "fast" and the other is "slow." By subtracting the slow average from the fast average, the script determines the current strength and direction of the market's momentum.

2. The Volatility Layer: Bollinger Bands

Unlike a standard MACD which shows a single line, this script takes those resulting MACD values and wraps them in a "band" system. It calculates:

  • Averages: A smoothed average of the MACD values over a specific period.
  • Standard Deviation: A measure of how much the MACD is fluctuating.
  • Upper and Lower Bands: These are drawn at a set distance from the average based on the standard deviation. These bands act as "boundaries," helping traders see when momentum is reaching extreme levels or becoming overextended.

3. Trend Detection and Dynamic Visualization

The script features an intelligent logic system to differentiate between upward and downward trends:

  • Trend Tracking: The tool continuously compares the current MACD value to the previous one. If it rises, it registers as an upward trend; if it falls, it marks a downward trend.
  • Smart Coloring & Display: Depending on your settings, the indicator can automatically toggle the display of lines and dots. It identifies whether the momentum is moving up or down and adjusts the visual output accordingly (for example, hiding specific buffers to highlight the dominant trend).
  • Gap Filling: The logic includes a feature to smooth out the visual transition between these trends, ensuring that the change from an "up" signal to a "down" signal is represented clearly on your chart without unnecessary clutter.

Summary for Traders

Essentially, this tool tells you not just where the price is going, but how aggressively it is moving. By placing Bollinger Bands around the MACD, it allows you to see when momentum is accelerating toward a breakout or when it is cooling off into a period of consolidation. It translates complex mathematical fluctuations into clear visual boundaries and trend signals.

Indicators Used
Moving average indicatorMoving average indicatorStandard Deviation indicator
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
BB_MACD_v1.03
//+------------------------------------------------------------------+
//|                                               Custom BB_MACD.mq4 |
//|                                     Copyright © 2005, adoleh2000 |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

#property  copyright "Copyright © 2005, adoleh2000"
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 4
#property  indicator_color1  Blue    //Upperband
#property  indicator_color2  Red     //Lowerband
#property  indicator_color3  Lime    //bbMacd up
#property  indicator_color4  Magenta //bbMacd up

//
//
//
//
//---- indicator parameters
extern int    FastLen         = 12;
extern int    SlowLen         = 26;
extern int    Length          = 10;
extern double StDv            = 2.5;
extern int    barsToCalculate = 300;
extern bool   drawDots        = True;
extern bool   fillGaps        = True;

//
//
//
//
//---- indicator buffers
double ExtMapBuffer1[];  // bbMacd
double ExtMapBuffer2[];  // bbMacd
double ExtMapBuffer3[];  // Upperband Line
double ExtMapBuffer4[];  // Lowerband Line
double bbMacd[];
double trend[];

//
//
//
//
//

double   sDev;
datetime lastBarTime;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
         
   SetIndexBuffer(2, ExtMapBuffer1); // bbMacd line
   SetIndexBuffer(3, ExtMapBuffer2); // bbMacd line
   if (drawDots)
      {
         SetIndexStyle(2, DRAW_ARROW);
         SetIndexArrow(2, 108);
         SetIndexStyle(3, DRAW_ARROW);
         SetIndexArrow(3, 108);
      }         
   else      
      {
         SetIndexStyle(2, DRAW_LINE);
         SetIndexStyle(3, DRAW_LINE);
      }         
   SetIndexBuffer(0, ExtMapBuffer3); // Upperband line
   SetIndexBuffer(1, ExtMapBuffer4); // Lowerband line

   //
   //
   //
   //
   //
   
   lastBarTime     = EMPTY_VALUE;
   barsToCalculate = MathMax(barsToCalculate,150);
      ArrayResize(bbMacd ,barsToCalculate);
      ArrayResize(trend  ,barsToCalculate);
      ArraySetAsSeries(bbMacd,true);
      ArraySetAsSeries(trend ,true);
   

   //
   //
   //
   //
   //
            
   IndicatorShortName("BB MACD (" + FastLen + "," + SlowLen + "," + Length+")");
   SetIndexLabel(0, "Upperband");
   SetIndexLabel(1, "Lowerband");  
   SetIndexLabel(2, "upTrend");
   SetIndexLabel(3, "downTrend");
   SetIndexDrawBegin(0,barsToCalculate-1);
   SetIndexDrawBegin(1,barsToCalculate-1);
   SetIndexDrawBegin(2,barsToCalculate-1);
   SetIndexDrawBegin(3,barsToCalculate-1);
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   return(0);
}
//+------------------------------------------------------------------+
//| Custom BB_MACD                                                   |
//+------------------------------------------------------------------+
int start()
{
  
   double avg;
   int    limit;
   int    counted_bars = IndicatorCounted();

   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
   if(lastBarTime != iTime(NULL,0,0))
      {
         limit = barsToCalculate;
         lastBarTime = iTime(NULL,0,0);
      }
   else  limit = MathMin(Bars - counted_bars,barsToCalculate);


   //
   //
   //
   //
   //
   
   for(int i = 0; i < limit; i++)
       bbMacd[i] = iMA(NULL, 0, FastLen, 0, MODE_EMA, PRICE_CLOSE, i) - 
                   iMA(NULL, 0, SlowLen, 0, MODE_EMA, PRICE_CLOSE, i);


   //
   //
   //
   //
   //
   
   for(i = limit; i > 0 ; i--)
     {
       avg              = iMAOnArray(bbMacd, 0, Length, 0, MODE_EMA, i);
       sDev             = iStdDevOnArray(bbMacd, 0, Length, MODE_EMA, 0, i);  
       ExtMapBuffer1[i] = bbMacd[i];
       ExtMapBuffer2[i] = bbMacd[i];
       ExtMapBuffer3[i] = avg + (StDv * sDev);
       ExtMapBuffer4[i] = avg - (StDv * sDev);
            trend[i] = trend[i+1];
               if (bbMacd[i]> bbMacd[i+1]) trend[i] = 1;
               if (bbMacd[i]< bbMacd[i+1]) trend[i] =-1;

               
            //
            //
            //
            //
            //
               
               if (!drawDots && fillGaps)
                  {
                    if (trend[i]>0)
                       { 
                          ExtMapBuffer2[i] = EMPTY_VALUE;
                          if (trend[i+1]<0) ExtMapBuffer1[i+1] = bbMacd[i+1]; 
                       }
                    if (trend[i]<0)
                       { 
                          ExtMapBuffer1[i] = EMPTY_VALUE;
                          if (trend[i+1]>0) ExtMapBuffer2[i+1] = bbMacd[i+1]; 
                       }
                  }
               else                                       
                  {
                     if(trend[i] >  0) ExtMapBuffer2[i] = EMPTY_VALUE;
                     if(trend[i] <  0) ExtMapBuffer1[i] = EMPTY_VALUE;
                  }                     
 
 
          //----
          //----
          
     }
   return(0);
}
//+------------------------------------------------------------------+
 

Comments