//+------------------------------------------------------------------+
//|                                       Candle_Histograms_Template |
//|   Plots candle bodies as histograms in Green (bull) / Red (bear) |
//+------------------------------------------------------------------+
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

#property indicator_label1  "Bullish Body"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrGreen
#property indicator_width1  3

#property indicator_label2  "Bearish Body"
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_color2  clrRed
#property indicator_width2  3

double BullishTop[];
double BearishTop[];

//+------------------------------------------------------------------+
// IMPORTANT! 
// In order for this to work, the candle buffers need to be first in order!!!
//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0, BullishTop);
   SetIndexBuffer(1, BearishTop);

   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexEmptyValue(1, EMPTY_VALUE);

   ArraySetAsSeries(BullishTop, true);
   ArraySetAsSeries(BearishTop, true);

   return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   ArraySetAsSeries(open, true);
   ArraySetAsSeries(close, true);

   int istart = (prev_calculated > 0) ? prev_calculated - 1 : 0;

   for (int i = istart; i < rates_total-1; i++)
   {
      BullishTop[i] = EMPTY_VALUE;
      BearishTop[i] = EMPTY_VALUE;

      double o = open[i];
      double c = close[i];
      bool   BullishHarami = close[i+1] <  open[i+1] && 
                             close[i]   >= open[i] &&
                             high[i]    <  open[i+1] &&
                             low[i]     >  close[i+1];
      bool   BearishHarami = close[i+1] >  open[i+1] && 
                             close[i]   <= open[i] &&
                             high[i]    <  close[i+1] &&
                             low[i]     >  open[i+1];
      if (BullishHarami)
      {
         BullishTop[i] = c; // Top of histogram
         BearishTop[i] = o; // Bottom value in other buffer to neutralize
      }
      else if (BearishHarami)
      {
         BearishTop[i] = o; // Top of histogram
         BullishTop[i] = c; // Bottom value in other buffer to neutralize
      }
   }

   return rates_total;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void OnChartEvent(const int id,         // Event identifier  
                  const long& lparam,   // Event parameter of long type
                  const double& dparam, // Event parameter of double type
                  const string& sparam) // Event parameter of string type
  {
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
     }
      WidthAuto();
  }
//+------------------------------------------------------------------+
void WidthAuto()
  {
   long result=-1;
   int a,Width_Histogram;
   result=ChartGetInteger(0,CHART_SCALE,0);

   a=(int)result;
   switch(a)
     {
      case 0: Width_Histogram=2;
      break;

      case 1: Width_Histogram=2;
      break;

      case 2: Width_Histogram=3;
      break;

      case 3: Width_Histogram=5;
      break;

      case 4: Width_Histogram=7;
      break;

      case 5: Width_Histogram=15;
      break;

      break;
      default : Width_Histogram=3;
     }

   SetIndexStyle(0,DRAW_HISTOGRAM,0,Width_Histogram);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,Width_Histogram);
   ChartRedraw();

  }
//+------------------------------------------------------------------+
