//+------------------------------------------------------------------+
//|                                                          CCI.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
/*
      Раскраска CCI по задумке НУФа http://forexsystems.ru/ruchnye-torgovye-strategii-i-taktiki/74292-haos-visual-ssrc_mtf-pipsovka-dlya-vseh-34.html#post861833
      
*/
#property copyright   ""
#property link        ""
#property description "Commodity Channel Index"
#property strict

//#include <MovingAverages.mqh>

#property indicator_separate_window
#property indicator_buffers    5
#property indicator_color1     LightSeaGreen
#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
input int      InpCCIPeriod   = 20; // CCI Period
extern double  level          = 100;
extern color   ClrUp          = clrLime;
extern color   ClrDn          = clrRed;
extern color   ClrNeutralUp   = clrDodgerBlue;
extern color   ClrNeutralDn   = clrOrange;
extern int     HistWidth      = 3;
extern int    Last_Bars = 2000;
//--- buffers
//--- buffers
//--- buffers
double BufCCI[];

double BufUp[];
double BufDn[];
double BufNeUp[];
double BufNeDn[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
//--- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
   SetIndexBuffer(0,BufUp);
   SetIndexBuffer(1,BufDn);
   SetIndexBuffer(2,BufNeUp);
   SetIndexBuffer(3,BufNeDn);
   SetIndexBuffer(4,BufCCI);

   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 ("+IntegerToString(InpCCIPeriod)+")";
   IndicatorShortName(short_name);
   IndicatorDigits(0);
   SetIndexLabel(0,short_name);
//   SetIndexLabel(0, "Up");
   for (int i = 0; i < indicator_buffers ; i++){    
     SetIndexLabel(i, " ");       
   }/**/
   
   
   
   
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrUp);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrDn);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrNeutralUp);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrNeutralDn);
   
   //SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,1, clrLavender);
   SetIndexStyle(4,DRAW_NONE);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
int start()
{
   int i,limit,counted_bars=IndicatorCounted();
   int uptrending=0,downtrending=0;
//---- 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;
   if (limit > Last_Bars)
     limit = Last_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;
   }
   //for(i=0; i<limit; i++)
   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] = 0;//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] = 0;//-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);
  }
//+------------------------------------------------------------------+
