Код: Выделить всё
function Initialize()
{
IndicatorName = "ZZWL";        
PriceStudy = true;
AddInput("Input", Inputs.Candle);        
AddSeries("ZZWL", DrawAs.Line, Color.Blue);   
AddSeries("Lh_Ll", DrawAs.Custom, Color.Magenta, true, Axes.Parent);  
AddSeries("O", DrawAs.Custom, Color.Magenta);        
AddSeries("X", DrawAs.Line, Color.Gray);        
AddParameter("Delta", 0.2);
AddGlobalVariable("peakDetected", Types.Boolean,  false);        
AddGlobalVariable("troughDetected", Types.Boolean,  false);        
AddGlobalVariable("peakbar", Types.Int,  0);        
AddGlobalVariable("troughbar", Types.Int,  0);        
AddGlobalVariable("hi", Types.Double,  0);        
AddGlobalVariable("lo", Types.Double,  0);        
AddGlobalVariable("hibar", Types.Int,  0);        
AddGlobalVariable("lobar", Types.Int,  0);        
}
function Evaluate()
{
// AlfaDirect 2015.
// ZZWL (ZigZag WelthLab)
// Новый минимум, если Low бара вырос от текущего Low на %
// Новый максимум, если High бара снизился от текущего High на %
  if (CurrentIndex < 2)
  {
    lo = Input.Low[0];          //First min candle for Low
    hi = Input.High[0];         //First max candle for High
  }
  else
  {
      double delta = 0.01*Delta;
      //Detected Local max and Local min
            if(Input.High[0] > hi)  
                  {
                       hi = Input.High[0];
                       hibar = CurrentIndex;
                        }
                        if(Input.Low[0] < lo)
                        {
                        lo = Input.Low[0];
                        lobar = CurrentIndex;
                        }
                        if(!troughDetected)
                        {
                           X = (1 + delta)*lo;
                           if(Input.Low[0] > (1 + delta)*lo)
                            {
                             troughDetected = true;
                             peakDetected = false;
                             hi = Input.High[0];
                             hibar = CurrentIndex;
                             troughbar = lobar;
                             O[lobar-CurrentIndex] = lo;
                             ZZWL[lobar-CurrentIndex] = lo;
                            }
                        }
                        if(!peakDetected)
                        {
                           X = (1 - delta)*hi;
                           if(Input.High[0] < (1 - delta)*hi)
                           {
                            troughDetected = false;
                            peakDetected = true;
                            lo = Input.Low[0];
                            lobar = CurrentIndex;
                            peakbar = hibar;
                            O[hibar-CurrentIndex] = hi;
                            ZZWL[hibar-CurrentIndex] = hi;
                           }
                        }
                   O.DrawCircle();
                   if (CurrentIndex == MaxIndex)
                   {
                      if (troughbar > peakbar)
                       {
                         Lh_Ll[troughbar-CurrentIndex]=ZZWL[troughbar-CurrentIndex];
                         Lh_Ll[hibar-CurrentIndex] = hi;
                       }
                      if (troughbar < peakbar)
                       {
                         Lh_Ll[peakbar-CurrentIndex]=ZZWL[peakbar-CurrentIndex];
                         Lh_Ll[lobar-CurrentIndex] = lo;
                       }
                       
                   }
                   Lh_Ll.DrawLine(Color.Magenta, Line.Dot, 2);
  }
}