//+------------------------------------------------------------------+
//|                                                          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
      3.1 вставляем сигнал
      3.2 исправлена ошибка с недорисовкой сигнала при работе онлайн
      CCI_НУФ_Два в одном   Два CCI наложены друг на друга
*/
#property copyright   ""
#property link        ""
#property description "Commodity Channel Index"
#property strict

//#include <MovingAverages.mqh>

#property indicator_separate_window
#property indicator_buffers    10
#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_minimum    -300
//#property indicator_maximum     300
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
//--- input parameter

input  string  cci1             = "Параметры старшего CCI (фон)";
input int      CCIPeriod_1      = 210;             // Период CCI
extern color   ClrUp_1          = PaleGreen;       // Цвет вверх
extern color   ClrDn_1          = Thistle;         // Цвет вниз
extern color   ClrNeutral_1     = PowderBlue;      // Нейтральный цвет
extern color   ClrSignal_1      = PowderBlue;      // Цвет сигнала
extern color   ClrLine_1        = clrDeepSkyBlue;  // Цвет линии CCI
extern int     HistWidth_1      = 4;               // Толщина гистограммы

input  string  cci2             = "Параметры младшего CCI (передний план)";
input int      CCIPeriod_2      = 14;              // Период CCI
extern color   ClrUp_2          = Green;           // Цвет вверх
extern color   ClrDn_2          = Red;             // Цвет вниз
extern color   ClrNeutral_2     = clrDodgerBlue;   // Нейтральный цвет
extern color   ClrSignal_2      = Yellow;          // Цвет сигнала
extern color   ClrLine_2        = CLR_NONE;        // Цвет линии CCI
extern int     HistWidth_2      = 2;               // Толщина гистограммы



extern double  Level          = 100;
extern double  SigDeviat      = 100;

//--- buffers

double BufCCI_1[];
double BufUp_1[];
double BufDn_1[];
double BufNe_1[];
double BufSignal_1[];

double BufCCI_2[];
double BufUp_2[];
double BufDn_2[];
double BufNe_2[];
double BufSignal_2[];



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
//--- 2 additional buffers are used for counting.
   IndicatorBuffers(10);
   SetIndexBuffer(0,BufUp_1);
   SetIndexBuffer(1,BufDn_1);
   SetIndexBuffer(2,BufNe_1);
   SetIndexBuffer(3,BufCCI_1);
   SetIndexBuffer(4,BufSignal_1);
   

   if(CCIPeriod_1<=1)
     {
      Print("Wrong input parameter CCI Period=",CCIPeriod_1);
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,CCIPeriod_1);
//--- name for DataWindow and indicator subwindow label
   short_name="CCI_НУФ Два в одном (" + IntegerToString(CCIPeriod_1) + ";" +  IntegerToString(CCIPeriod_2)  +")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   
   SetIndexBuffer(0,BufUp_1);
   SetIndexBuffer(1,BufDn_1);
   SetIndexBuffer(2,BufNe_1);
   SetIndexBuffer(3,BufCCI_1);
   SetIndexBuffer(4,BufSignal_1);
   
   
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth_1,ClrUp_1);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth_1,ClrDn_1);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth_1,ClrNeutral_1);
   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,1, ClrLine_1);
   SetIndexStyle(4,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth_1,ClrSignal_1);
   
   SetIndexBuffer(5,BufUp_2);
   SetIndexBuffer(6,BufDn_2);
   SetIndexBuffer(7,BufNe_2);
   SetIndexBuffer(8,BufCCI_2);
   SetIndexBuffer(9,BufSignal_2);
   
   
   SetIndexStyle(5,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth_2,ClrUp_2);
   SetIndexStyle(6,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth_2,ClrDn_2);
   SetIndexStyle(7,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth_2,ClrNeutral_2);
   SetIndexStyle(8,DRAW_LINE,STYLE_SOLID,1, ClrLine_2);
   SetIndexStyle(9,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth_2,ClrSignal_2);
   
   
   
//--- initialization done
   return(INIT_SUCCEEDED);
  }

//=======================================================================
//
//=======================================================================  
int start()
{
   GetCCI(BufCCI_1,BufUp_1,BufDn_1,BufNe_1,BufSignal_1, CCIPeriod_1); 
   GetCCI(BufCCI_2,BufUp_2,BufDn_2,BufNe_2,BufSignal_2, CCIPeriod_2); 
   return(0);
}

  
//=======================================================================
//
//=======================================================================  
int GetCCI(
            double &BufCCI[],
            double &BufUp[],
            double &BufDn[],
            double &BufNe[],
            double &BufSignal[],
            int     CCIPeriod 
          
          )
{
   double curr, prev;
   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;
   
   bool precondBuy = false, precondSell = false;
   
//---- main loop
   
   //limit = Bars-1;
   //for(i=limit; i>=0; i--)
   for(i=0; i<limit; i++)
   {
      BufCCI[i]=iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,i);
      BufNe[i] = 0;
      BufUp[i] = 0;
      BufDn[i] = 0;
   }
   //for(i=0; i<limit; i++)
   for(i=limit; i>=0; i--)
   {
      if(i<=Bars-3)
      {
             curr = BufCCI[i];
             prev = BufCCI[i+1];
            
            
            
            
            if(curr > 0)
            { 
            
                  if(curr >= Level )
                  {
                     BufNe[i] = Level;
                     BufUp[i] = curr;
                     BufDn[i] = 0;
                  }
                 
                  if(curr <= Level )
                  {
                     BufNe[i] = curr;
                     BufUp[i] = 0;
                     BufDn[i] = 0;
                  }
                  
                 
                  
            
            }
            //---------------------------------------------------------------------------
            if(curr<= 0)
            { 
                     if(curr < -Level )
                     {
                        BufNe[i] = -Level;
                        BufUp[i] = 0;
                        BufDn[i] = curr;
                     }
                     if(curr >= -Level )
                     {
                        BufNe[i] = curr;
                        BufUp[i] = 0;
                        BufDn[i] = 0;
                     }
                    
            }
          
            
      }
    //if(i==0)
     //Print("  BufNe[i] = ", BufNe[i],  "  BufUp[i] = ", BufUp[i],"  BufDn[i] = ", BufDn[i]);
      
   }
   //***************************
   limit = Bars-2;
   for(i=limit; i>=0; i--)
   {
             BufSignal[i] = 0;
             curr = BufCCI[i];
             prev = BufCCI[i+1];
      if(curr >= Level + SigDeviat)
            {
               precondSell = true;
            }
            if(precondSell && prev >= Level && curr < Level)
            {
               BufSignal[i] = curr;
               precondSell = false;
            }
            
            if(curr <= -Level - SigDeviat)
            {
               precondBuy = true;
            }
            if(precondBuy && prev <= -Level && curr > -Level)
            {
               BufSignal[i] = curr;
               precondBuy = false;
            }
      
      
             
   }   
   return(0);
  }