//+------------------------------------------------------------------+
//|                                                          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      = 8;              // Период 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 string  ss               = "------------ Текстовые метки ----------- ";
extern string  FontName         = "Arial Black";
extern int     FontSize         = 12; 
//extern color   FontColor        = Blue;

extern color   Цвет_Больше_Level    = Green;
extern color   Цвет_Больше_Нуля     = clrDeepSkyBlue;
extern color   Цвет_Около_нуля      = clrDarkGray;
extern color   Цвет_Меньше_Нуля     = clrViolet;
extern color   Цвет_Меньше_Level    = Red;

extern double  Уровень_Около_нуля   = 10;

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

//extern int     NBars          = 1000;               // Толщина гистограммы

//--- 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[];

string MyName = "CCI_2in1_Dig_";
//============================================================================
//    deinit
//============================================================================
int deinit()   
{   
   //UninitR = UninitializeReason() ;
   DelMyObj(MyName); 
return(0);   
} 
//
//+------------------------------------------------------------------+
//| 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_2in1_DC (" + 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);
   
   DelMyObj(MyName);
   
//--- 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;
   
   for(i=0; i<limit; i++)
   {
      
      BufNe_1[i] = 0;
      BufUp_1[i] = 0;
      BufDn_1[i] = 0;
      
         BufCCI_1[i] = iCCI(NULL,0,CCIPeriod_1,PRICE_TYPICAL,i);
         if(BufCCI_1[i] >= Level)                                 {BufUp_1[i] = BufCCI_1[i];   BufNe_1[i] = Level;}
         if(BufCCI_1[i] <= Level && BufCCI_1[i] >0)               BufNe_1[i] = BufCCI_1[i]; 
         if(BufCCI_1[i] >= -Level && BufCCI_1[i] <0)              BufNe_1[i] = BufCCI_1[i]; 
         if(BufCCI_1[i] < -Level && BufCCI_1[i] <0)               {BufDn_1[i] = BufCCI_1[i];   BufNe_1[i] = -Level;}
         
       BufNe_2[i] = 0;
       BufUp_2[i] = 0;
       BufDn_2[i] = 0;  
         
         
         BufCCI_2[i] = iCCI(NULL,0,CCIPeriod_2,PRICE_TYPICAL,i);
         if(BufCCI_2[i] >= Level)                                 {BufUp_2[i] = BufCCI_2[i];  BufNe_2[i] = Level;}
         if(BufCCI_2[i] <= Level && BufCCI_2[i] >0)               BufNe_2[i] = BufCCI_2[i]; 
         if(BufCCI_2[i] >= -Level && BufCCI_2[i] <0)              BufNe_2[i] = BufCCI_2[i]; 
         if(BufCCI_2[i] < -Level && BufCCI_2[i] <0)               {BufDn_2[i] = BufCCI_2[i];  BufNe_2[i] = -Level;}
      
      
      
   }
   
   DrawLabel();
   return(0);
}


  
 //=========================================================================
 //    DrawLabel  
 //=========================================================================
 void DrawLabel()
 {
     int subWin =ChartWindowFind();
     string name11, name12;         // первая строка;
     string name21, name22;         // вторая строка;
     int x11, y11, x12, y12;          // первая строка;
     int x21, y21, x22, y22;          // вторая строка;
     
     
     long current_chart_id=ChartID();
     string text11, text12;            // первая строка;
     string text21, text22;            // вторая строка;
     
     color clr1 = CLR_NONE, clr2 = CLR_NONE;
     name11 =MyName + "Lab11";
     name12 =MyName + "Lab12";
     name21 =MyName + "Lab21";
     name22 =MyName + "Lab22";
   
   if(BufCCI_1[0] >= Level)                                                      clr1 = Цвет_Больше_Level;
   if(BufCCI_1[0] <= Level && BufCCI_1[0] >= Уровень_Около_нуля)                 clr1 = Цвет_Больше_Нуля;
   if(BufCCI_1[0] <  Уровень_Около_нуля && BufCCI_1[0] > -Уровень_Около_нуля)    clr1 = Цвет_Около_нуля;
   if(BufCCI_1[0] >  - Level && BufCCI_1[0] <= -Уровень_Около_нуля)              clr1 = Цвет_Меньше_Нуля;
   if(BufCCI_1[0] <= -Level)                                                     clr1 = Цвет_Меньше_Level;
   
   
   if(BufCCI_2[0] >= Level)                                                      clr2 = Цвет_Больше_Level;
   if(BufCCI_2[0] <= Level && BufCCI_2[0] >= Уровень_Около_нуля)                 clr2 = Цвет_Больше_Нуля;
   if(BufCCI_2[0] <  Уровень_Около_нуля && BufCCI_2[0] >  -Уровень_Около_нуля)   clr2 = Цвет_Около_нуля;
   if(BufCCI_2[0] >  - Level && BufCCI_2[0] <= -Уровень_Около_нуля)              clr2 = Цвет_Меньше_Нуля;
   if(BufCCI_2[0] <= -Level)                                                     clr2 = Цвет_Меньше_Level;
   
       
    //x1 = 40; y1 = 20;
    //x2 = 40; y2 = 40;
    
    x11 = 80; y11 = 20;
    x12 = 40; y12 = 20;
    
    x21 = 80; y21 = 40;
    x22 = 40; y22 = 40;
    
    
    //text1 = "GGG";
    
    text11 = "CCI(" + IntegerToString(CCIPeriod_1) + ") =  " ;
    text12 = DoubleToString(BufCCI_1[0],0);
    
    text21 = "CCI(" + IntegerToString(CCIPeriod_2) + ") =  ";
    text22 = DoubleToString(BufCCI_2[0],0);
    
    ObjectCreate(current_chart_id,name11,OBJ_LABEL,subWin,0,0);
    ObjectSetInteger(current_chart_id,name11,OBJPROP_XDISTANCE,x11);
    ObjectSetInteger(current_chart_id,name11,OBJPROP_YDISTANCE,y11);
    ObjectSetInteger(current_chart_id,name11,OBJPROP_CORNER,1);
    ObjectSetString(current_chart_id,name11,OBJPROP_TEXT, text11);

    ObjectSetString(current_chart_id,name11,OBJPROP_FONT,FontName);
    ObjectSetInteger(current_chart_id,name11,OBJPROP_FONTSIZE,FontSize);
    ObjectSetInteger(current_chart_id,name11,OBJPROP_COLOR,clr1);
    ObjectSetInteger(current_chart_id,name11,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
    
    ObjectCreate(current_chart_id,name12,OBJ_LABEL,subWin,0,0);
    ObjectSetInteger(current_chart_id,name12,OBJPROP_XDISTANCE,x12);
    ObjectSetInteger(current_chart_id,name12,OBJPROP_YDISTANCE,y12);
    ObjectSetInteger(current_chart_id,name12,OBJPROP_CORNER,1);
    ObjectSetString(current_chart_id,name12,OBJPROP_TEXT, text12);

    ObjectSetString(current_chart_id,name12,OBJPROP_FONT,FontName);
    ObjectSetInteger(current_chart_id,name12,OBJPROP_FONTSIZE,FontSize);
    ObjectSetInteger(current_chart_id,name12,OBJPROP_COLOR,clr1);
    ObjectSetInteger(current_chart_id,name12,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
    
    //----------------------- Вторая строка --------------------------------------
    ObjectCreate(current_chart_id,name21,OBJ_LABEL,subWin,0,0);
    ObjectSetInteger(current_chart_id,name21,OBJPROP_XDISTANCE,x21);
    ObjectSetInteger(current_chart_id,name21,OBJPROP_YDISTANCE,y21);
    ObjectSetInteger(current_chart_id,name21,OBJPROP_CORNER,1);
    ObjectSetString(current_chart_id,name21,OBJPROP_TEXT, text21);

    ObjectSetString(current_chart_id,name21,OBJPROP_FONT,FontName);
    ObjectSetInteger(current_chart_id,name21,OBJPROP_FONTSIZE,FontSize);
    ObjectSetInteger(current_chart_id,name21,OBJPROP_COLOR,clr2);
    ObjectSetInteger(current_chart_id,name21,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
    
    ObjectCreate(current_chart_id,name22,OBJ_LABEL,subWin,0,0);
    ObjectSetInteger(current_chart_id,name22,OBJPROP_XDISTANCE,x22);
    ObjectSetInteger(current_chart_id,name22,OBJPROP_YDISTANCE,y22);
    ObjectSetInteger(current_chart_id,name22,OBJPROP_CORNER,1);
    ObjectSetString(current_chart_id,name22,OBJPROP_TEXT, text22);

    ObjectSetString(current_chart_id,name22,OBJPROP_FONT,FontName);
    ObjectSetInteger(current_chart_id,name22,OBJPROP_FONTSIZE,FontSize);
    ObjectSetInteger(current_chart_id,name22,OBJPROP_COLOR,clr2);
    ObjectSetInteger(current_chart_id,name22,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
    
    
    /*
    
    //-----------------------------------------------------------------------------------------
    
    ObjectCreate(current_chart_id,name2,OBJ_LABEL,subWin,0,0);
    ObjectSetInteger(current_chart_id,name2,OBJPROP_XDISTANCE,x2);
    ObjectSetInteger(current_chart_id,name2,OBJPROP_YDISTANCE,y2);
    ObjectSetInteger(current_chart_id,name2,OBJPROP_CORNER,1);
    ObjectSetString(current_chart_id,name2,OBJPROP_TEXT, text2);

    ObjectSetString(current_chart_id,name2,OBJPROP_FONT,FontName);
    ObjectSetInteger(current_chart_id,name2,OBJPROP_FONTSIZE,FontSize);
    ObjectSetInteger(current_chart_id,name2,OBJPROP_COLOR,clr2);
    ObjectSetInteger(current_chart_id,name2,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
     */ 

//ANCHOR_RIGHT_UPPER

 
 }
 
 
 
//===============================================================================
//   Функция удаляет объекты, созданные в этом индюке
//   аргумент- начало имени
//===============================================================================
void DelMyObj(string myName)
{
int pos;
string objName;
int    obj_total=ObjectsTotal();
         
      for(int i=obj_total-1;i>=0; i--)
      {
         objName = ObjectName(i);
         pos = StringFind(objName, myName, 0);
         if(pos!=-1) 
         {
            ObjectDelete(objName);
         }
      }
//return(0);
}