//+------------------------------------------------------------------+
//|                                                          CCI.mq4 |
//+------------------------------------------------------------------+
#property strict
#property indicator_separate_window
#property indicator_buffers    6
#property indicator_color1     LightSeaGreen
#property indicator_color5     DimGray
#property indicator_width5     2
#property indicator_level1    -90.0
#property indicator_level2     90.0
#property indicator_level3     0.0
#property indicator_level4     200.0
#property indicator_level5     -200.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
//--- input parameter
input int      InpCCIPeriod   = 34; // CCI Period
input ENUM_APPLIED_PRICE Price = 5;
extern double  level          = 90;
extern double  level2         = 200;
extern color   ClrUpUp        = Green;
extern color   ClrDnDn        = Crimson;
extern color   ClrUp          = Green;
extern color   ClrDn          = Crimson;
extern color   ClrNeutralUp   = LightSkyBlue;
extern color   ClrNeutralDn   = Pink;
extern int     HistWidth      = 2;
//--- buffers
double BufCCI[], BufUpUp[], BufDnDn[], BufUp[], BufDn[], BufNeUp[], BufNeDn[];
//+------------------------------------------------------------------+
int OnInit(void)   {
   IndicatorBuffers(7);
//--- 2 additional buffers are used for counting.
   SetIndexBuffer(0,BufUpUp);  SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrUpUp);       SetIndexLabel(0,"UpUp");
   SetIndexBuffer(1,BufDnDn);  SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrDnDn);       SetIndexLabel(1,"DnDn");
   SetIndexBuffer(4,BufUp);    SetIndexStyle(4,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrUp);         SetIndexLabel(4,"Up");
   SetIndexBuffer(5,BufDn);    SetIndexStyle(5,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrDn);         SetIndexLabel(5,"Dn");
   SetIndexBuffer(2,BufNeUp);  SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrNeutralUp);  SetIndexLabel(2,"N_Up");
   SetIndexBuffer(3,BufNeDn);  SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrNeutralDn);  SetIndexLabel(3,"N_Dn");
   SetIndexBuffer(6,BufCCI);

   if(InpCCIPeriod<=1)   {
     Print("Wrong input parameter CCI Period=",InpCCIPeriod);
     return(INIT_FAILED);
   }
//---
   SetIndexDrawBegin(0,InpCCIPeriod);
   string short_name="CCI_ΝΣΤ_v5a ("+IntegerToString(InpCCIPeriod)+")";
   IndicatorShortName(short_name);
//--- initialization done
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
int start()  {
   int i,limit,counted_bars=IndicatorCounted();
   int uptrending=0,downtrending=0;
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   for(i=0; i<limit; i++)   {
     BufCCI[i]=iCCI(NULL,0,InpCCIPeriod,Price,i);
     BufNeUp[i] = 0;
     BufNeDn[i] = 0;
     BufUp[i] = 0;
     BufDn[i] = 0;
     BufUpUp[i] = 0;
     BufDnDn[i] = 0;
   }
   for(i=limit; i>=0; i--)   {
     if(i<=Bars-3)   {
       double curr = BufCCI[i];
       if(curr > 0)  { 
         if(curr >= level2 )   {
           BufNeUp[i] = level;        BufNeDn[i] = 0;
           BufUp[i]   = level2;       BufDn[i]   = 0;
           BufUpUp[i] = curr;         BufDnDn[i] = 0;
         }
         if(curr >= level && curr < level2)    {
           BufNeUp[i] = level;        BufNeDn[i] = 0;
           BufUp[i]   = curr;         BufDn[i]   = 0;
           BufUpUp[i] = 0;            BufDnDn[i] = 0;
         }
         if(curr <= level )    {
           BufNeUp[i] = curr;         BufNeDn[i] = 0;
           BufUp[i]   = 0;            BufDn[i]   = 0;
           BufUpUp[i] = 0;            BufDnDn[i] = 0;
         } 
       }
      //---------------------------------------------------------------------------
       if(curr<= 0)   { 
         if(curr < -level2 )   {
           BufNeUp[i] = 0;            BufNeDn[i] = -level;;
           BufUp[i] = 0;              BufDn[i] = -level2;
           BufUpUp[i] = 0;            BufDnDn[i] = curr;
         }
         if(curr < -level && curr > -level2)    {
           BufNeUp[i] = 0;            BufNeDn[i] = -level;;
           BufUp[i] = 0;              BufDn[i] = curr;
           BufUpUp[i] = 0;            BufDnDn[i] = 0;
         }
         if(curr >= -level )    {
           BufNeUp[i] = 0;            BufNeDn[i] = curr;
           BufUp[i] = 0;              BufDn[i] = 0;
           BufUpUp[i] = 0;            BufDnDn[i] = 0;
         }
       }
     }
   }
   return(0);
  }
//+------------------------------------------------------------------+
