finding last 2 zigzag price

 
Hi everyone I'm trying to get the last 2 price of zigzag and the direction which the zigzag drawn. This is the code:

double pZZ1=0,pZZ2=0;
int ZigZagPattern()
  {
   int result=0;
   int cnt=0;
   for(int i=0; i<Bars; i++)
     {
      double pZZ=zigzag(i);
      if(pZZ==0)
         continue;
      cnt++;
      if(cnt==1)
         pZZ1=pZZ;
      if(cnt==2)
        {
         pZZ2=pZZ;
         if(pZZ1>pZZ2)
            result=1;
         else
            result=2;
         break;
        }
     }
     return(result);
  }
Using this code I could get which direction the zigzag line drawn but failed to get the zigzag price. Since this part is working, variable pZZ1 and pZZ2 should filled with zigzag price but when I tried to retrieve it, it return 0.0. Is anyone knows why it return 0.0 when this part is works?
 

Maybe so

    

if(ZZPrice(0)>ZZPrice(1)){up=ZZPrice(0); dn=ZZPrice(1);}else{up=ZZPrice(1); dn=ZZPrice(0);}




double iGetArray(const int handle,const int buffer,const int start_pos,const int count,double &arr_buffer[])
             {
             bool result=true;
             if(!ArrayIsDynamic(arr_buffer)){
             Print("This a no dynamic array!");
             return(false);
             }
             ArrayFree(arr_buffer);
             ResetLastError();
             int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
             if(copied!=count){
             PrintFormat("Failed to copy data from the indicator, error code %d",GetLastError()); 
             return(false);
             }
             return(result);
             }            
double ZZPrice(int ne=0)
             {
             int bars;
             bars=MathMin(1000,Bars(_Symbol,PERIOD_CURRENT)-1);
             double zz;
             double ZigzagBuffer[];
             ArraySetAsSeries(ZigzagBuffer,true);
             int start_pos=0;
             if(!iGetArray(ZigzagHandle,0,start_pos,bars,ZigzagBuffer))
             return(0);
             int rr=0;
             for(int r=0;r<bars;r++){
             zz=ZigzagBuffer[r];
             if(zz!=0){
             rr++;
             if(rr>ne) return(zz);
             }
             }
             return(0);
             }

 
 
Your code
Simplified
double pZZ1=0,pZZ2=0;
int ZigZagPattern()
  {
   int result=0;
   int cnt=0;
   for(int i=0; i<Bars; i++)
     {
      double pZZ=zigzag(i);
      if(pZZ==0)
         continue;
      cnt++;
      if(cnt==1)
         pZZ1=pZZ;
      if(cnt==2)
        {
         pZZ2=pZZ;
         if(pZZ1>pZZ2)
            result=1;
         else
            result=2;
         break;
        }
     }
     return(result);
  }
double pZZ1=0,pZZ2=0;
int ZigZagPattern()
  {
   int i;
   for(i=0; i<Bars; i++) if( (pZZ1=zigzag(i)) != 0) break;
   for(++i; i<Bars; i++) if( (pZZ2=zigzag(i)) != 0) break;
   return pZZ1>pZZ2 ? 1 : 2;
  }

 
The for loops could be even more optimized.

for(++i; (i<Bars) && ((pZZ2=zigzag(i)) == 0); i++);
 
Dominik Christian Egert #: The for loops could be even more optimized.

What you wrote and what I did, compile to the same thing. Not more optimized, just easier to understand.

 
I see. Thank you.

Interesting.
Reason: