zigzag direction return false result

 

Hi everyone, I tried to get the direction of the last zigzag and I found out sometimes it returns a false result. This is the code that I used:

double pZZ1=0,pZZ2=0;
int ZigZagPattern(int shift=0)
  {

   int result=0;
   for(shift; shift<Bars; shift++)
      if((pZZ1=zigzag(shift)) != 0)
         break;
   for(++shift; shift<Bars; shift++)
      if((pZZ2=zigzag(shift)) != 0)
         break;
   if(pZZ1>pZZ2)
      result=1;
   else
      result=2;
   return result;

on the screenshot shows (part with red dot), it shows that the last zigzag is downtrend where as on the chart is uptrend. Can anyone tell me which part of the code that makes it return false result?

Files:
zigzag.PNG  206 kb
 

Shouldn't it be pZZ1==zigzag(shift)  pZZ2==zigzag(shift) ?

 
Nagisa Unada #: Shouldn't it be pZZ1==zigzag(shift)  pZZ2==zigzag(shift) ?

No.

      if((pZZ2=zigzag(shift)) != 0)
// is equivalent to 
      pZZ2=zigzag(shift); if((pZZ2 != 0)
 
Luandre Ezra: on the screenshot shows (part with red dot), it shows that the last zigzag is downtrend where as on the chart is uptrend. Can anyone tell me which part of the code that makes it return false result?

It is only uptrend after the bar zero made its new high. Don't look the the forming bar.