//+----------------------------------------------------------+
//|                              MACD Signal Cross Histo.mq4 |
//+----------------------------------------------------------+
#property copyright ""
#property link      ""
#property indicator_separate_window
#property indicator_buffers 8
#property strict

#property description "Displays the trend of the MACD Signal/Signal Ma Cross."
#property description "The trend strength, based on the Signal line's slope, is also shown."

// Input parameters for MACD calculation
extern int   FastEMA              = 12;                 // Period for fast EMA
extern int   SlowEMA              = 26;                 // Period for slow EMA
extern int   SignalEMA            = 6;                  // Period for signal line EMA
extern int   MaSignalPeriod       = 9;                  // Period for EMA of signal line
extern int   MaxBars              = 0;                  // Maximum Bars, 0=all bars, minimum=250
//+-----------------------------------------------------------------------------------------------------------+
extern int   HistoWidth           = 3;                  // Histogram width
extern color HistoColorStrongBuy  = clrMediumSlateBlue; // Histogram color strong trend up
extern color HistoColorWeakBuy    = clrDarkSlateBlue;   // Histogram color weak trend up
extern color HistoColorStrongSell = clrRed;             // Histogram color strong trend down
extern color HistoColorWeakSell   = clrMaroon;          // Histogram color weak trend down
//+-----------------------------------------------------------------------------------------------------------+

// Buffer arrays for indicator data
//--- visible buffers
double macdBuffer[];            // MACD line (Fast EMA - Slow EMA)
double signalBuffer[];          // Signal line (EMA of MACD)
double signalMaBuffer[];        // EMA of signal line
double trend[];                 // Trend based on the MACD_Signal/Signal Ma Cross 
double huu[],hud[],hdd[],hdu[]; // For Histogram display
int maxPeriod;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {
   // Set precision for indicator values
   IndicatorDigits(Digits + 1);
   
   // Allocate 8 buffers for the indicator
   IndicatorBuffers(8);

   IndicatorSetDouble(INDICATOR_MINIMUM,0);
   IndicatorSetDouble(INDICATOR_MAXIMUM,1);

   // Assign buffers to arrays & Set labels & drawing styles for each buffer
   SetIndexBuffer(0, huu,INDICATOR_DATA);   SetIndexLabel(0,"Strong Buy");  SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,HistoWidth,HistoColorStrongBuy);
   SetIndexBuffer(1, hud,INDICATOR_DATA);   SetIndexLabel(1,"Weak Buy");    SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,HistoWidth,HistoColorWeakBuy);
   SetIndexBuffer(2, hdd,INDICATOR_DATA);   SetIndexLabel(2,"Strong Sell"); SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,HistoWidth,HistoColorStrongSell);
   SetIndexBuffer(3, hdu,INDICATOR_DATA);   SetIndexLabel(3,"Weak Sell");   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,HistoWidth,HistoColorWeakSell);
   SetIndexBuffer(4, macdBuffer);           SetIndexLabel(4,"MACD");        SetIndexStyle(4,DRAW_NONE);   // MACD line (not drawn)
   SetIndexBuffer(5, signalBuffer);         SetIndexLabel(5,"Signal");      SetIndexStyle(5,DRAW_NONE);   // Signal line (not drawn)
   SetIndexBuffer(6, signalMaBuffer);       SetIndexLabel(6,"SignalMA");    SetIndexStyle(6,DRAW_NONE);   // EMA of signal line (not drawn)
   SetIndexBuffer(7, trend,INDICATOR_DATA); SetIndexLabel(7,"Trend");       SetIndexStyle(7,DRAW_NONE);   // Trend (not drawn)

   maxPeriod=MathMax(FastEMA,SlowEMA);
   maxPeriod=MathMax(maxPeriod,MaSignalPeriod);
   if(MaxBars>0 && MaxBars<250) MaxBars=250;
   
   // Set indicator name with parameters
   IndicatorShortName("MACD Signal/Signal Ma Cross (" + (string)FastEMA + "," + (string)SlowEMA + "," + (string)SignalEMA + "," + (string)MaSignalPeriod + ")");
   
   return (0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars;   // Number of bars already calculated
   int limit;          // Number of bars to process
   int i;              // Loop index for bars

   // Get number of bars already calculated
   counted_bars = IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit = fmin(Bars-counted_bars,Bars-maxPeriod);
   if(MaxBars>0 && limit>MaxBars) limit=MaxBars;
  
   // Populate histogram buffers based on the MACD_Signal/Signal Ma Cross trend 
   for (i=limit;i>=0 && !_StopFlag; i--)  macdBuffer[i]   = iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
   for (i=limit;i>=0 && !_StopFlag; i--)  signalBuffer[i] = iMAOnArray(macdBuffer, Bars, SignalEMA, 0, MODE_EMA, i);
   for (i=limit;i>=0 && !_StopFlag; i--)
   {
      signalMaBuffer[i] = iMAOnArray(signalBuffer, Bars, MaSignalPeriod, 0, MODE_EMA, i);

      if(signalBuffer[i]>signalMaBuffer[i] && signalBuffer[i]>=signalBuffer[i+1]) trend[i] = 2;
      else if(signalBuffer[i]<signalMaBuffer[i] && signalBuffer[i]<=signalBuffer[i+1]) trend[i] = -2;
      else if(signalBuffer[i]>signalMaBuffer[i]) trend[i] =  1;
      else if(signalBuffer[i]<signalMaBuffer[i]) trend[i] = -1;
      else trend[i]=trend[i+1];

      huu[i] = (trend[i] == 2) ? 1 : EMPTY_VALUE;
      hud[i] = (trend[i] == 1) ? 1 : EMPTY_VALUE;
      hdd[i] = (trend[i] == -2) ? 1 : EMPTY_VALUE;
      hdu[i] = (trend[i] == -1) ? 1 : EMPTY_VALUE;
   }
   return (0);
}
