//+------------------------------------------------------------------+
//|                                                          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_color6     Black
#property indicator_width6     2
#property indicator_level1    -100.0
#property indicator_level2     100.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
extern bool ПоказыватьЛиниюССИ = false;
input int      InpCCIPeriod   = 21; // CCI Period
extern double  level          = 100;
extern color   ClrUp          = clrRed;
extern color   ClrDn          = clrRed;
extern color   ClrNeutralUp   = clrDodgerBlue;
extern color   ClrNeutralDn   = clrOrange;
extern int     HistWidth      = 2;
extern bool    ShowCrossLevel = false;
extern bool    ShowCrossZero  = false;
extern bool ПоказыватьЗначенияCCI = false;

//--- buffers
//--- buffers
//--- buffers
color colorUP=clrBlue, colorDN=clrRed, color0, color1, color2;
double BufCCI[];
double BufUp[];
double BufDn[];
double BufNeUp[];
double BufNeDn[];
datetime key;
int Win;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
//--- 2 additional buffers are used for counting.
   SetIndexBuffer(0,BufUp);   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrUp);  SetIndexLabel(0,"Up");
   SetIndexBuffer(1,BufDn);   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrDn);  SetIndexLabel(1,"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(4,BufCCI);  if(ПоказыватьЛиниюССИ)    SetIndexStyle(4,DRAW_LINE);   else  SetIndexStyle(4,DRAW_NONE); SetIndexLabel(4,NULL);

   if(InpCCIPeriod<=1)
     {
      Print("Wrong input parameter CCI Period=",InpCCIPeriod);
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,InpCCIPeriod);
//--- name for DataWindow and indicator subwindow label
   short_name="CCI_НУФ_v4 ("+IntegerToString(InpCCIPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   




   
   //SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,1, clrLavender);
//--- initialization done
   if(ПоказыватьЗначенияCCI)  {
     Win=WindowFind(short_name);
     ObjectCreate("CCI0",OBJ_LABEL,Win,0,0); ObjectSet("CCI0",OBJPROP_XDISTANCE,10); ObjectSet("CCI0",OBJPROP_YDISTANCE,10);   ObjectSet("CCI0", OBJPROP_CORNER,1);
     ObjectCreate("CCI1",OBJ_LABEL,Win,0,0); ObjectSet("CCI1",OBJPROP_XDISTANCE,10); ObjectSet("CCI1",OBJPROP_YDISTANCE,27);   ObjectSet("CCI1", OBJPROP_CORNER,1);
     ObjectCreate("CCI2",OBJ_LABEL,Win,0,0); ObjectSet("CCI2",OBJPROP_XDISTANCE,10); ObjectSet("CCI2",OBJPROP_YDISTANCE,44);   ObjectSet("CCI2", OBJPROP_CORNER,1);
   }
   else  {  ObjectDelete("CCI0");  ObjectDelete("CCI1");  ObjectDelete("CCI2");  }
   return(INIT_SUCCEEDED);
  }
int start()
{
   int i,limit,counted_bars=IndicatorCounted();
   int uptrending=0,downtrending=0;
   int direct=0;
   double AO1, AO2;
//---- TODO: add your code here
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- main loop
   
   //limit = Bars-1;
   //for(i=limit; i>=0; i--)
   for(i=0; i<limit; i++)
   {
      BufCCI[i]=iCCI(NULL,0,InpCCIPeriod,PRICE_TYPICAL,i);
      BufNeUp[i] = 0;
      BufUp[i] = 0;
      BufDn[i] = 0;
   }
     AO2=iAO(NULL, 0, 2);
     AO1=iAO(NULL, 0, 1);
     if(AO2<0 && AO1<0 && AO1>AO2)  direct=1;
     if(AO2>0 && AO1>0 && AO1<AO2)  direct=-1;
/*     
   if(key!=Time[0])  {
     if(BufCCI[2]>=level  && BufCCI[1]<level && ShowCrossLevel)   Alert(Symbol(), "   ",_Period,"   Снижение CCI ниже уровня.");
     if(BufCCI[2]<=-level && BufCCI[1]>-level && ShowCrossLevel)  Alert(Symbol(), "   ",_Period,"   Подъём CCI выше уровня.");
     if((BufCCI[2]<=0 && BufCCI[1]>0) && direct== 1 &&  ShowCrossZero)  Alert(Symbol(), "   ",_Period,"   Переход CCI через 0 вверх");
     if((BufCCI[2]>=0 && BufCCI[1]<0) && direct==-1 &&  ShowCrossZero)  Alert(Symbol(), "   ",_Period,"   Переход CCI через 0 вниз");
     key=Time[0];
   }
*/     
     if(BufCCI[0]>=0) color0 = colorUP; else color0 = colorDN;
     if(BufCCI[1]>=0) color1 = colorUP; else color1 = colorDN;
     if(BufCCI[2] <0) color2 = colorDN; else color2 = colorUP;
     if(ПоказыватьЗначенияCCI)  {
       ObjectSetText("CCI0","0-ая :  " + DoubleToStr(BufCCI[0], 2), 10, "Arial Black", color0);
       ObjectSetText("CCI1","1-ая :  " + DoubleToStr(BufCCI[1], 2), 10, "Arial Black", color1);
       ObjectSetText("CCI2","2-ая :  " + DoubleToStr(BufCCI[2], 2), 10, "Arial Black", color2);
     }

   for(i=limit; i>=0; i--)
   {
      if(i<=Bars-3)
      {
            double curr = BufCCI[i];
            double prev = BufCCI[i+1];
            
            
            
            
            if(curr > 0)
            { 
            
                  if(curr >= level )
                  {
                     BufNeUp[i] = level;
                     BufNeDn[i] = 0;
                     BufUp[i] = curr;
                     BufDn[i] = 0;
                  }
                 
                  if(curr <= level )
                  {
                     BufNeUp[i] = curr;
                     BufNeDn[i] = 0;
                     BufUp[i] = 0;
                     BufDn[i] = 0;
                  }
                  
                 
                  
            
            }
            //---------------------------------------------------------------------------
            if(curr<= 0)
            { 
                     if(curr < -level )
                     {
                        BufNeUp[i] = 0;
                        BufNeDn[i] = -level;;
                        BufUp[i] = 0;
                        BufDn[i] = curr;
                     }
                     if(curr >= -level )
                     {
                        BufNeUp[i] = 0;
                        BufNeDn[i] = curr;
                        BufUp[i] = 0;
                        BufDn[i] = 0;
                     }
                    
            }
          
      }
     
     //if(i==0)
     //Print("  BufNeUP[i] = ", BufNeUP[i],  "  BufUp[i] = ", BufUp[i],"  BufDn[i] = ", BufDn[i]);
      
   }
  
   return(0);
  }
//+------------------------------------------------------------------+
