count number of bars from last ZigZag High and Low

 

Hello everyone,

The following code draws arrows of the latest high and low from the ZigZag indicator.

The arrows will not be repainted and thus stay on the chart.

The code snipped presented here is from someone else.

I wanted to kindly ask if someone can built a loop or counter to find the number of bars until the latest high and low presented on the chart.

The latest low is alway the latest green up and vice versa.

//---- draw sty
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 clrCrimson
#property indicator_color4 clrLawnGreen
#property indicator_width1 2

//---- input parameters
extern int      ExtDepth       = 13;
extern int      ExtDeviation   = 8;
extern int      ExtBackstep    = 5;
extern double   distance       = 0.00018;      

//---- buffers
double UpperZigzag[];
double LowerZigzag[];
int oldTime;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()  {
   IndicatorBuffers(4);
   SetIndexBuffer(1,LowerZigzag);
   SetIndexBuffer(2,UpperZigzag);
   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,indicator_width1,indicator_color4);
   SetIndexArrow(1,200);
   SetIndexStyle(2,DRAW_ARROW,STYLE_SOLID,indicator_width1,indicator_color1);
   SetIndexArrow(2,202);   
   SetIndexEmptyValue(2,0.0);
   SetIndexEmptyValue(1,0.0);
   SetIndexLabel(1,"LowerZigzag");
   SetIndexLabel(2,"UpperZigzag");   
   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() {
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit, i, tf = Period();
   string cur = Symbol();
   int counted_bars = IndicatorCounted(); 
   if (counted_bars<0) return(-1);
   if (counted_bars>0) counted_bars--;
   limit = Bars - counted_bars; 
   //---
   double dHighPrev2,dLowPrev2;
   double prevbarZigzagprice,prevbar2Zigzagprice;

// ------- only doSomething after candleclose of current TF
if(oldTime != Time[0]){

   // --------
   for (i=limit; i>=0; i--)  {
      dHighPrev2           = iHigh(cur,tf,i+2);
      dLowPrev2            = iLow(cur,tf,i+2);
      prevbarZigzagprice   = iCustom(cur,tf,"ZigZag",ExtDepth,ExtDeviation,ExtBackstep,0,i+1); 
      prevbar2Zigzagprice  = iCustom(cur,tf,"ZigZag",ExtDepth,ExtDeviation,ExtBackstep,0,i+2);

      if (prevbar2Zigzagprice != 0) {

         if (prevbar2Zigzagprice == dHighPrev2) { // upperZZ
            UpperZigzag[i+2] = dHighPrev2;
         }
      }
      if (prevbar2Zigzagprice == dLowPrev2) { // lowerZZ
         LowerZigzag[i+2] = dLowPrev2;         
      }     
      
   }// ------- end for

} oldTime = Time[0]; //---------- end if statement

return(0);
} // ende start
//+------------------------------------------------------------------+

I am not good at coding and every attempt ended in complete failure :-(

Thanks in advance for any help.

 
 

OKay thanks.

I am using the code from your link:

double SwingValue[2]={0,0};
      int Found = 0;
      int k = 0;
      while(Found <= 2){
         if(iCustom(NULL, 0, "ZigZag", Depth, 5, 3, 0, k) != 0){
            SwingValue[Found] = iCustom(NULL , 0, "ZigZag", Depth, 5, 3, 0, k);
            Found++;
         }
         k++;
      }

Now to count the number of bars until last ZZ high, i increament my counter until High[i] == ZZ high:

for (int v=1; v<Bars; v++)
{
 if(High[v] == SwingValue[0]) // ZZ High
 {
 break;
 }
 else
 {
 counter_for_zzhigh = counter_for_zzhigh + 1;
 }
}

Works fine but on rare occasions numerous bars have the same High and thus the counter wont "increase enough"

Anyone knows a better way for counting bars until last ZZ high?

 
for (int v=1; v<Bars; v++)
{
 if(High[v] != SwingValue[0]) { counter_for_zzhigh = counter_for_zzhigh + 1; }
 else break; 
}

Something like that.

 

Thanks Sir,

Now it seems that the indexes of SwingValue[Found] do flip.

I assumed that SwingValue[0] has always the latest ZZ high and SwingValue[1] is always latest ZZ low.

But in backtesting the indexes sometimes change positions and therefore the counter will show the number of Bars of current chart.