//+------------------------------------------------------------------+
//|                             CCI_CustomCandles_4.mq4 
//+------------------------------------------------------------------+
#property copyright "AHGDP"
#property link      "CCI_CustomCandles_4"
#property indicator_chart_window
#property indicator_buffers 12
#property indicator_color1  CLR_NONE
#property indicator_color2  CLR_NONE
#property indicator_color3  CLR_NONE
#property indicator_color4  CLR_NONE
#property indicator_color5  CLR_NONE
#property indicator_color6  CLR_NONE
#property indicator_color7  CLR_NONE
#property indicator_color8  CLR_NONE
#property indicator_color9  CLR_NONE
#property indicator_color10 CLR_NONE
#property indicator_color11 CLR_NONE
#property indicator_color12 CLR_NONE

extern int      CCI_Period         = 16 ;
extern int      CCI_Price          =  5 ;
extern int      TopLev             =  50 ;
extern int      BotLev             = -50 ;
extern int      ZeroLev            =   0 ;

extern color    Wickups            = DeepSkyBlue ;
extern color    Wickdns            = Magenta;  
extern color    Candleups          = DeepSkyBlue ;
extern color    Candledns          = Magenta;

extern color    Wickupm            = Green ;   
extern color    Wickdnm            = MediumVioletRed; 
extern color    Candleupm          = Green ;
extern color    Candlednm          = MediumVioletRed;

extern color    Wickupt            = LimeGreen ;  
extern color    Wickdnt            = Red ;   
extern color    Candleupt          = LimeGreen ;
extern color    Candlednt          = Red ; 

extern int      WickSize          = 1 ;
extern int      CandleSize        = 3 ;
extern int      CountBars         = 500 ;
            
bool   Deinitialized;  
int    Bar_Width, Chart_Scale;

double  L1dn, L2dn, L3dn , L4dn, L5dn, L6dn;

double Bar1s[];
double Bar2s[];
double Bar1m[];
double Bar2m[];
double Bar1t[];
double Bar2t[];
double Candle1s[];
double Candle2s[];
double Candle1m[];
double Candle2m[]; 
double Candle1t[];
double Candle2t[];    
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int init()
  {

  //============================================================ 
     Deinitialized = false; 
  Chart_Scale = ChartScaleGet();
                  
        if(Chart_Scale == 0) {Bar_Width = 1;  }
  else {if(Chart_Scale == 1) {Bar_Width = 1;  }      
  else {if(Chart_Scale == 2) {Bar_Width = 2;  } 
  else {if(Chart_Scale == 3) {Bar_Width = 3;  }
  else {if(Chart_Scale == 4) {Bar_Width = 6;  }
  else {Bar_Width = 13;}
   }}}}
       
      SetIndexStyle (0, DRAW_HISTOGRAM, STYLE_SOLID, WickSize  , Wickups  );
      SetIndexStyle (1, DRAW_HISTOGRAM, STYLE_SOLID, WickSize  , Wickdns  );
      SetIndexStyle (2, DRAW_HISTOGRAM, STYLE_SOLID, WickSize  , Wickupm  );
      SetIndexStyle (3, DRAW_HISTOGRAM, STYLE_SOLID, WickSize  , Wickdnm  );
      SetIndexStyle (4, DRAW_HISTOGRAM, STYLE_SOLID, WickSize  , Wickupt  );
      SetIndexStyle (5, DRAW_HISTOGRAM, STYLE_SOLID, WickSize  , Wickdnt  );

      SetIndexStyle (6, DRAW_HISTOGRAM, STYLE_SOLID, Bar_Width, Candleups );
      SetIndexStyle (7, DRAW_HISTOGRAM, STYLE_SOLID, Bar_Width, Candledns );
      SetIndexStyle (8, DRAW_HISTOGRAM, STYLE_SOLID, Bar_Width, Candleupm );
      SetIndexStyle (9, DRAW_HISTOGRAM, STYLE_SOLID, Bar_Width, Candlednm );
      SetIndexStyle (10,DRAW_HISTOGRAM, STYLE_SOLID, Bar_Width, Candleupt );
      SetIndexStyle (11,DRAW_HISTOGRAM, STYLE_SOLID, Bar_Width, Candlednt );
  
      SetIndexBuffer(0, Bar1s);
      SetIndexBuffer(1, Bar2s);
      SetIndexBuffer(2, Bar1m);
      SetIndexBuffer(3, Bar2m);
      SetIndexBuffer(4, Bar1t);
      SetIndexBuffer(5, Bar2t);
      SetIndexBuffer(6, Candle1s);
      SetIndexBuffer(7, Candle2s);
      SetIndexBuffer(8, Candle1m);
      SetIndexBuffer(9, Candle2m);
      SetIndexBuffer(10, Candle1t);
      SetIndexBuffer(11, Candle2t);
      

     IndicatorShortName("CCI");
   return(0);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+  
double CCIN     (int i=0)   {return(iCCI(NULL,0,CCI_Period,CCI_Price,i));}
double CCIP     (int i=0)   {return(iCCI(NULL,0,CCI_Period,CCI_Price,i+1));}
//+------------------------------------------------------------------+
void SetCandleColor(int col, int i)
  {
   double high,low,bodyHigh,bodyLow;
     {
      bodyHigh=MathMax(Open[i],Close[i]);
      bodyLow =MathMin(Open[i],Close[i]);
      high     =High[i];
      low     =Low[i];
     }
   Bar1s[i]=low;  Candle1s[i]=bodyLow;  
   Bar2s[i]=low;  Candle2s[i]=bodyLow;   
   Bar1m[i]=low;  Candle1m[i]=bodyLow;
   Bar2m[i]=low;  Candle2m[i]=bodyLow;
   Bar1t[i]=low;  Candle1t[i]=bodyLow;
   Bar2t[i]=low;  Candle2t[i]=bodyLow;
   
   switch(col)
     {
      case 1:    Bar1s[i]=high;   Candle1s[i]=bodyHigh;   break; 
      case 2:    Bar2s[i]=high;   Candle2s[i]=bodyHigh;   break;
      case 3:    Bar1m[i]=high;   Candle1m[i]=bodyHigh;   break;
      case 4:    Bar2m[i]=high;   Candle2m[i]=bodyHigh;   break;
      case 5:    Bar1t[i]=high;   Candle1t[i]=bodyHigh;   break; 
      case 6:    Bar2t[i]=high;   Candle2t[i]=bodyHigh;   break;
     }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+  
int start()
  { 
     SetIndexDrawBegin(0,Bars-CountBars);
     SetIndexDrawBegin(1,Bars-CountBars);
     SetIndexDrawBegin(2,Bars-CountBars);
     SetIndexDrawBegin(3,Bars-CountBars);
     SetIndexDrawBegin(4,Bars-CountBars);
     SetIndexDrawBegin(5,Bars-CountBars);
     SetIndexDrawBegin(6,Bars-CountBars);
     SetIndexDrawBegin(7,Bars-CountBars);
     SetIndexDrawBegin(8,Bars-CountBars);
     SetIndexDrawBegin(9,Bars-CountBars);
     SetIndexDrawBegin(10,Bars-CountBars);
     SetIndexDrawBegin(11,Bars-CountBars);
     
      L1dn = 1 ;  L2dn = 1 ; L3dn = 1 ;  L4dn = 1 ;  L5dn = 1 ; L6dn = 1 ;
    
   for(int i=MathMax(Bars-1-IndicatorCounted(),1); i>=0; i--)
     {    double   cciN  = CCIN(i);
          double   cciP  = CCIP(i);
         //----------- set candles between levels----- Bot up  Top dn --------------
        
         if ((( cciN > BotLev  ) && ( cciP < BotLev  ))  && (L1dn == 1))   { L1dn = 0; }
            if( L1dn == 0 )   { SetCandleColor(1,i); }
         if ((( cciN < BotLev  ) && ( cciP > BotLev  ))  && (L1dn == 0) )  { L1dn = 1; }                         
            if( L1dn == 0 )   { SetCandleColor(1,i); }
         if ((( cciN > ZeroLev ) && ( cciP < ZeroLev ))  && (L1dn == 0) )  { L1dn = 1; }                         
            if( L1dn == 0 )   { SetCandleColor(1,i); }     
              
         if((( cciN > ZeroLev ) && ( cciP < ZeroLev ))   && (L2dn == 1) )  { L2dn = 0; }
           if( L2dn == 0 )    { SetCandleColor(3,i); }
         if((( cciN < ZeroLev ) && ( cciP > ZeroLev ))   && (L2dn == 0) )  { L2dn = 1; }                         
           if( L2dn == 0 )    { SetCandleColor(3,i); }
         if((( cciN > TopLev )  && ( cciP < TopLev  ))   && (L2dn == 0) )  { L2dn = 1; }                         
           if( L2dn == 0 )    { SetCandleColor(3,i); }    
           
     //******         
              
         if((( cciN < TopLev  ) && ( cciP > TopLev  ))   && (L3dn == 1) )   { L3dn = 0; }
           if( L3dn == 0 )    { SetCandleColor(2,i); }
         if((( cciN > TopLev  ) && ( cciP < TopLev  ))   && (L3dn == 0) )   { L3dn = 1; }                         
           if( L3dn == 0 )    { SetCandleColor(2,i); }
         if((( cciN < ZeroLev ) && ( cciP > ZeroLev ))   && (L3dn == 0) )   { L3dn = 1; }                         
           if( L3dn == 0 )    { SetCandleColor(2,i); }        
              
         if((( cciN <  ZeroLev ) && ( cciP > ZeroLev ))  && (L4dn == 1) )   { L4dn = 0; }
           if( L4dn == 0 )   { SetCandleColor(4,i); }   
         if((( cciN >  ZeroLev ) && ( cciP < ZeroLev ))  && (L4dn == 0) )   { L4dn = 1; }                         
           if( L4dn == 0 )   { SetCandleColor(4,i); } 
         if((( cciN <  BotLev )  && ( cciP > BotLev  ))  && (L4dn == 0) )   { L4dn = 1; }                         
           if( L4dn == 0 )   { SetCandleColor(4,i); }         
                     
        //----------- set candles above toplev up & below Bot DN-------------------
       
        if(( cciN > TopLev ) && ( cciP < TopLev )  && (L5dn == 1) )  { L5dn = 0; }
        if(( cciN < TopLev ) && ( cciP > TopLev )  && (L5dn == 0) )  { L5dn = 1; }                         
         if( L5dn == 0 )   { SetCandleColor(5,i); }            
              
        if(( cciN < BotLev ) && ( cciP > BotLev )  && (L6dn == 1) )  { L6dn = 0; }
        if(( cciN > BotLev ) && ( cciP < BotLev )  && (L6dn == 0) )  { L6dn = 1; }                           
         if( L6dn == 0 )   { SetCandleColor(6,i); }    
            
     }
//----
   return(0);
  }
//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)                                                    
  {  Chart_Scale = ChartScaleGet();
     init();  
  }
//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+
int ChartScaleGet()
  { long result = -1;
   ChartGetInteger(0,CHART_SCALE,0,result);
  return((int)result);
  }  
//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+   


