//+------------------------------------------------------------------+
//|                                        Dadas_Counter_Candles.mq4 |
//|                                            Copyright 2016, Dadas |
//|                                http://www.forexfactory.com/Dadas |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Dadas"
#property link      "http://www.forexfactory.com/Dadas"

#property indicator_chart_window
#property indicator_buffers 8

input bool                Plot_HLC_Chart              = true;
input color               HLC_Chart_Up_Color          = clrNavy;
input color               HLC_Chart_Dwn_Color         = clrMaroon;
input string              __________________          = "Chart Default Settings";
input color               Chart_Bar_Up_Color          = clrNavy;
input color               Chart_Bar_Dwn_Color         = clrMaroon;
input color               Chart_Candle_Up_Color       = clrNavy;
input color               Chart_Candle_Dwn_Color      = clrMaroon;
input color               Chart_Line_Color            = clrLightGray;

double UpBuffOpen[];
double UpBuffClose[];
double DwnBuffOpen[];
double DwnBuffClose[];

double UpBuffH[];
double UpBuffL[];
double DwnBuffH[];
double DwnBuffL[];

int Width_Histogram;

int init() {
   SetIndexBuffer(0, UpBuffOpen);
   SetIndexBuffer(1, UpBuffClose);
   SetIndexBuffer(2, DwnBuffOpen);
   SetIndexBuffer(3, DwnBuffClose);
   SetIndexBuffer(4, UpBuffH);
   SetIndexBuffer(5, UpBuffL);
   SetIndexBuffer(6, DwnBuffH);
   SetIndexBuffer(7, DwnBuffL);
   SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID,0,HLC_Chart_Up_Color);
   SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID,0,HLC_Chart_Up_Color);
   SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_SOLID,0,HLC_Chart_Dwn_Color);
   SetIndexStyle(3, DRAW_HISTOGRAM, STYLE_SOLID,0,HLC_Chart_Dwn_Color);
   SetIndexStyle(4, DRAW_HISTOGRAM, STYLE_SOLID,2,HLC_Chart_Up_Color);
   SetIndexStyle(5, DRAW_HISTOGRAM, STYLE_SOLID,2,HLC_Chart_Up_Color);
   SetIndexStyle(6, DRAW_HISTOGRAM, STYLE_SOLID,2,HLC_Chart_Dwn_Color);
   SetIndexStyle(7, DRAW_HISTOGRAM, STYLE_SOLID,2,HLC_Chart_Dwn_Color);
   SetIndexEmptyValue(0,EMPTY_VALUE);
   SetIndexEmptyValue(1,EMPTY_VALUE);
   SetIndexEmptyValue(2,EMPTY_VALUE);
   SetIndexEmptyValue(3,EMPTY_VALUE);
   SetIndexEmptyValue(4,EMPTY_VALUE);
   SetIndexEmptyValue(5,EMPTY_VALUE);
   SetIndexEmptyValue(6,EMPTY_VALUE);
   SetIndexEmptyValue(7,EMPTY_VALUE);

   return (0);
}

void OnDeinit(const int reason)
{
         ChartSetInteger(0,CHART_COLOR_CANDLE_BEAR,Chart_Candle_Dwn_Color);
         ChartSetInteger(0,CHART_COLOR_CANDLE_BULL,Chart_Candle_Up_Color);
         ChartSetInteger(0,CHART_COLOR_CHART_DOWN,Chart_Bar_Dwn_Color);
         ChartSetInteger(0,CHART_COLOR_CHART_UP,Chart_Bar_Up_Color);
         ChartSetInteger(0,CHART_COLOR_CHART_LINE,Chart_Line_Color);    
}

int start() 
  {
        
   int nCountedBars = IndicatorCounted();
//---- check for possible errors
   if(nCountedBars<0) 
   return(-1);
//---- last counted bar will be recounted
   if(nCountedBars>0) 
   nCountedBars--;
   int nLimit = Bars-nCountedBars;

//---- main loop
        int BckgCol=ChartGetInteger(0,CHART_COLOR_BACKGROUND,0);

        if(Plot_HLC_Chart)
         {
         ChartSetInteger(0,CHART_COLOR_CANDLE_BEAR,BckgCol);
         ChartSetInteger(0,CHART_COLOR_CANDLE_BULL,BckgCol);
         ChartSetInteger(0,CHART_COLOR_CHART_DOWN,BckgCol);
         ChartSetInteger(0,CHART_COLOR_CHART_UP,BckgCol);
         ChartSetInteger(0,CHART_COLOR_CHART_LINE,BckgCol);
         }
         else
         {
         ChartSetInteger(0,CHART_COLOR_CANDLE_BEAR,Chart_Candle_Dwn_Color);
         ChartSetInteger(0,CHART_COLOR_CANDLE_BULL,Chart_Candle_Up_Color);
         ChartSetInteger(0,CHART_COLOR_CHART_DOWN,Chart_Bar_Dwn_Color);
         ChartSetInteger(0,CHART_COLOR_CHART_UP,Chart_Bar_Up_Color);
         ChartSetInteger(0,CHART_COLOR_CHART_LINE,Chart_Line_Color);
         }

for(int i=0; i<Bars; i++)
{

 UpBuffOpen[i]=EMPTY;
 UpBuffClose[i]=EMPTY;
 DwnBuffOpen[i]=EMPTY;
 DwnBuffClose[i]=EMPTY;
 UpBuffH[i]=EMPTY;
 UpBuffL[i]=EMPTY;
 DwnBuffH[i]=EMPTY;
 DwnBuffL[i]=EMPTY;

 if(Close[i]>Open[i]&&Plot_HLC_Chart)
  {
 UpBuffOpen[i]=Close[i];
 UpBuffClose[i]=Close[i];
 DwnBuffOpen[i]=EMPTY;
 DwnBuffClose[i]=EMPTY;
 UpBuffH[i]=High[i];
 UpBuffL[i]=Low[i];
 DwnBuffH[i]=EMPTY;
 DwnBuffL[i]=EMPTY;
  }
 if(Close[i]<=Open[i]&&Plot_HLC_Chart)
  {
 UpBuffOpen[i]=EMPTY;
 UpBuffClose[i]=EMPTY;
 DwnBuffOpen[i]=Close[i];
 DwnBuffClose[i]=Close[i];
 UpBuffH[i]=EMPTY;
 UpBuffL[i]=EMPTY;
 DwnBuffH[i]=High[i];
 DwnBuffL[i]=Low[i];
  }
}
   return (0);
}
//+------------------------------------------------------------------+
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;
   result=ChartGetInteger(0,CHART_SCALE,0);

   a=result;
   switch(a)
     {
      case 0: Width_Histogram=0;
      break;

      case 1: Width_Histogram=1;
      break;

      case 2: Width_Histogram=2;
      break;

      case 3: Width_Histogram=7;
      break;

      case 4: Width_Histogram=10;
      break;

      case 5: Width_Histogram=20;
      break;

      break;
      default : Width_Histogram=3;
     }

   SetIndexStyle(0,DRAW_HISTOGRAM,0,Width_Histogram);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,Width_Histogram);
   SetIndexStyle(2,DRAW_HISTOGRAM,0,Width_Histogram);
   SetIndexStyle(3,DRAW_HISTOGRAM,0,Width_Histogram);

  }
//+------------------------------------------------------------------+
