VininI_ZZ11_1

Author: Copyright � 2010, Victor Nicolaev.
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
VininI_ZZ11_1
//+------------------------------------------------------------------+
//|                                                VininI_ZZ11.1.mq4 |
//|                               Copyright © 2010, Victor Nicolaev. |
//|                                                    vinin@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Victor Nicolaev."
#property link      "vinin@mail.ru"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Yellow
#property indicator_color3 Yellow



extern int     BarsCount=15; // êîëè÷åñòâî áàðîâ äëÿ ðàñ÷åòà ZZ
extern int     ZZCount=1;

double ZZ[];

double MA_DN[], MA_UP[];

double ZZValue[][2];

int ZZTime[2]={0,0};
int Id;

int UP=0;
int DN=1;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init() {
   SetIndexBuffer(0, ZZ);
   SetIndexBuffer(1, MA_UP);
   SetIndexBuffer(2, MA_DN);
   // íåáîëüøàÿ çàùèòà äëÿ êîððåêòíîñòè ðàáîòû
   if (BarsCount<2) BarsCount=2;

   SetIndexStyle(0, DRAW_SECTION);   
   SetIndexStyle(1, DRAW_LINE);   
   SetIndexStyle(2, DRAW_LINE);   

   ArrayResize(ZZValue,ZZCount);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int counted_bars=IndicatorCounted();
   int limit=Bars-counted_bars-1;
   if(Bars-counted_bars > 2) {
      ArrayInitialize(ZZ,EMPTY_VALUE);
      limit=Bars-BarsCount-1;
      int posHigh = iHighest( NULL, 0, MODE_HIGH, BarsCount, limit);   
      int posLow  = iLowest(  NULL, 0, MODE_LOW,  BarsCount, limit);
      ZZ[posHigh]=High[posHigh];
      ZZ[posLow]=Low[posLow];
      ZZTime[0]=MathMax(Time[posHigh],Time[posLow]);
      ZZTime[1]=MathMin(Time[posHigh],Time[posLow]);
      Id=UP; if (posHigh>posLow) Id=DN;
   }
   
   for(int i=limit; i>=0; i--) {
      ZZWork(i);

      double tmp_UP=0, tmp_DN=0;
      for (int j=0;j<ZZCount;j++) {
         tmp_UP+=ZZValue[j, UP];
         tmp_DN+=ZZValue[j, DN];
      }
      MA_UP[i]=tmp_UP/ZZCount;
      MA_DN[i]=tmp_DN/ZZCount;
   }
   return(0);
}


void ZZWork(int pos){
   int posHigh = iHighest( NULL, 0, MODE_HIGH, BarsCount, pos);   
   int posLow  = iLowest(  NULL, 0, MODE_LOW,  BarsCount, pos);
   int prevpos1=iBarShift(NULL,0,ZZTime[0]);
   int prevpos2=iBarShift(NULL,0,ZZTime[1]);

   int posmax,posmin;
   double Valuemax, Valuemin;

   posmax=posLow;
   posmin=posHigh;
   Valuemin=High[posHigh];
   Valuemax=Low[posLow];

   if (posHigh>posLow) {
      posmax=posHigh;
      posmin=posLow;
      Valuemax=High[posHigh];
      Valuemin=Low[posLow];
   }

   if ((ZZ[prevpos2]-ZZ[prevpos1])*(ZZ[prevpos1]-Valuemin)<0) {
      ZZTime[1]=ZZTime[0];
      Id=UP;
      if (ZZ[prevpos1]-Valuemin<0) Id=DN;
      for (int j=ZZCount-1;j>0;j--) ZZValue[j, Id]=ZZValue[j-1,Id];
   } else    ZZ[prevpos1]=EMPTY_VALUE;
   ZZ[posmin]=Valuemin;
   ZZTime[0]=Time[posmin];
   ZZValue[0, Id]=Valuemin;
}

Comments