
/*
      
      ZZ - добавляем зигзаг
*/
#property copyright   ""
#property link        ""
#property description "Commodity Channel Index"
#property strict

//#include <MovingAverages.mqh>

#property indicator_separate_window
#property indicator_buffers    4
#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
enum modes
{
   Не_Рисовать,
   Зигзаг,
   Точки
};

input int      InpCCIPeriod   = 14; // CCI Period
extern double  Deviation      = 100;
extern color   ClrUp          = clrLime;
extern color   ClrDn          = clrRed;
extern int     HistWidth      = 3; // Толщина гистограммы
extern string  s              = "Отображение зигзага";
extern modes   Режим_Зигзага  = Не_Рисовать;
extern color   ClrZZ          = clrBlue;
extern int     ZZWidth        = 2;
//--- buffers
//--- buffers
//--- buffers
double BufCCI[];
double BufUp[];
double BufDn[];
double BufZZ[];
//-- Начальные значения для зигзага
int LastHiBarNum=0, LastLoBarNum =0;
datetime LastHiBarTime, LastLoBarTime;
double LastHi = 0, LastLo = 0;
bool findTrough = false,findPeak = true;

datetime TBar;
double OldCCI1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;

   IndicatorBuffers(4);
   
   SetIndexBuffer(0,BufUp);
   SetIndexBuffer(1,BufDn);
   SetIndexBuffer(2,BufCCI);
   SetIndexBuffer(3,BufZZ);
   

   if(InpCCIPeriod<=1)
     {
      Print("Wrong input parameter CCI Period=",InpCCIPeriod);
      return(INIT_FAILED);
     }
     
     if(Deviation<=0) Deviation = 100;
   short_name="CCI_ZZ ("+IntegerToString(InpCCIPeriod) + ", " + DoubleToString(Deviation,0) + ")";
   IndicatorShortName(short_name);
   
   SetIndexLabel(0,"BufUp");
   SetIndexLabel(1,"BufDn");
   SetIndexLabel(2,"CCI");
   SetIndexLabel(3,"ZZ");
   
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrUp);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,HistWidth,ClrDn);
   SetIndexStyle(2,DRAW_NONE);
   
   if(Режим_Зигзага  == Точки)
   {
      SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,ZZWidth,ClrZZ);
      SetIndexArrow(3,159);
   }
   if(Режим_Зигзага  == Зигзаг)
      SetIndexStyle(3,DRAW_SECTION,STYLE_SOLID,ZZWidth,ClrZZ);
   if(Режим_Зигзага  == Не_Рисовать)
      SetIndexStyle(3,DRAW_NONE);   
   
   
   
   
  // LastHiBarNum = Bars-1;
  // LastLoBarNum = Bars-1;
   return(INIT_SUCCEEDED);
  }
//=========================================================================
//    start
//=========================================================================
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]=ND(iCCI(NULL,0,InpCCIPeriod,PRICE_TYPICAL,i));
      BufUp[i] = 0;
      BufDn[i] = 0;
      BufZZ[i] = EMPTY_VALUE;
   }
      for(int j=500; j>0; j--)
      {
           if(j>=Bars-2) continue;
           BufZZ[j] = EMPTY_VALUE;
           BufUp[j] = EMPTY_VALUE;
           BufDn[j] = EMPTY_VALUE;
            //----------------------------------------------- Поиск пиков ---------------
            
            
            if(TimeDay(Time[1]) == 26 && TimeHour(Time[1]) == 19 && TimeMinute(Time[1]) >= 44)
            Print(" findPeak = ", findPeak, " findTrough = ", findTrough);
            if(findPeak)
            {
                  if (BufCCI[j]>=LastHi)
      		      {
         		      LastHi = BufCCI[j];
         		      LastHiBarTime =Time[j];
         		      BufUp[j] = BufCCI[j];
         		   }
      		      if (BufCCI[j] < LastHi) 
      		      {
      		      
      			      if ((BufCCI[j] < LastHi - Deviation))
      			      {
      			           LastLo = BufCCI[j];
      			           
      			           LastHiBarNum = iBarShift(NULL, 0, LastHiBarTime);
      			           BufZZ[LastHiBarNum] = LastHi;
         			        LastLoBarTime = Time[j];
            			     findPeak=false;
            			     findTrough=true;
      			           BufDn[j] = BufCCI[j];
      			      }
      			      
      			       if ((BufCCI[j] >= LastHi - Deviation))
      			         BufUp[j] = BufCCI[j];		
      		      }
      		      continue;
		      }
		      //----------------------------------------------- Поиск ям ------------------
            if(findTrough)
            {
                  if (BufCCI[j]<=LastLo )
      		      {
         		      LastLo = BufCCI[j];
         		      LastLoBarTime = Time[j];
         		      BufDn[j] = BufCCI[j];
      		      }
      		      
      		      if (BufCCI[j] > LastLo) 
      		      {
      		      
      			      if ((BufCCI[j] > LastLo + Deviation))
      			      {
         			      LastHi = BufCCI[j];
         			      LastLoBarNum = iBarShift(NULL, 0, LastLoBarTime);
         			      BufZZ[LastLoBarNum] = LastLo;
         			      LastHiBarTime = Time[j];
         			      findPeak = true;
         			      findTrough = false;
         			      BufUp[j] = BufCCI[j];
      			      }
      			      if ((BufCCI[j] <= LastLo + Deviation))
      			       BufDn[j] = BufCCI[j];
      			      		
      		      }
		      }
      }

   return(0);
  }

double ND(double a){ return (NormalizeDouble(a,2));}
