Need some help to understand and debug an issue with an indicator's historical points

 

Hi all,

I recently encountered an issue related to indicator's historical point, which I would like to have some opinion/idea on what actually happened.


Long story short, this indicator is designed to get low & high, using fractals and zigzag. The values of the indicator are stored in an array, called Final[]. While most of the time, the indicator does give correct values, however, for some reason, one of the historical point was wrong as shown in the pic.

      //--- Lower fractal found
      if (FractalsDown[i] != 0 && FractalsUp[i] == 0)   
      {  
         if (PrevFratal == OP_BUY)                       //if previously is a upper fractal
         {  
            if (FractalsDown[i] < Final[Index]) 
            { 
               Final[i] = FractalsDown[i];     
               Index = i;   
            }
	 } 
      }  

From the debugging message, it is clearly shown that the point (memory) was erased from Final[] array, as highlighted in orange. However, the indicator still uses that point value in the array. Could anyone shed some light on this matter?

PS: the indicator was run according to i++ sequence. Would this impact an indicator correctness?



 
         if (PrevFratal == OP_BUY) 
You are using a variable value from the previous iteration, which means, as is, you can't process bar zero (the forming one,) more than once, ever.
  1. You can put it in a buffer so you can get the previous bar value.
  2. Only process bar zero once (or none.)
  3. Save and restore them when processing bar zero.
  4. Use an array (size 2) and use iNS=rates_total-1-iBar, iCur=iNS%2, iPre=!iCur; var[iCur]=function(var[iPre]);. No save/restore required.
 
William Roeder:
You are using a variable value from the previous iteration, which means, as is, you can't process bar zero (the forming one,) more than once, ever.
  1. You can put it in a buffer so you can get the previous bar value.
  2. Only process bar zero once (or none.)
  3. Save and restore them when processing bar zero.
  4. Use an array (size 2) and use iNS=rates_total-1-iBar, iCur=iNS%2, iPre=!iCur; var[iCur]=function(var[iPre]);. No save/restore required.

Hi William, 

Thanks for replying. But I must say, I don't quite understand what you are trying to say, especially on item 4. Could you please elaborate if you don't mind?


Also, I don't quite get it when you said "process bar zero more than once, ever". If i understand your statement correctly, what you are trying to say is that this PrevFratal variable is being carried forward for next "new iteration" when new bar is formed. What I did is I reset the PrevFratal variable, equals to -1, everytime if a new bar is formed.

//+==========================================================       
//+-- Original ZigZag Code from MQL4
//+==========================================================
   int    i,limit,counterZ,whatlookfor=0;
   int    back,pos,lasthighpos=0,lastlowpos=0;
   double extremum;
   double curlow=0.0,curhigh=0.0,lasthigh=0.0,lastlow=0.0;
   
//--- check for history and inputs
   if(rates_total<InpDepth || InpBackstep>=InpDepth)
      return(0);
|
|
** Codes for calculating ZigZag and Fractal **
|
|
//+==========================================================       
//+-- Combine ZigZag & Fractals
//+==========================================================
   int Index = 0, PrevIndex = 0, PrevFratal = -1;

   while (i < limit-1)     
   {
      Final[i] = 0;
      double a = FractalsUp[i];
      double b = FractalsDown[i];
      
      //--- Upper fractal (red) found
      if (a != 0 && b == 0)    
      {
         if (PrevFratal == OP_SELL)                      //if previously is a lower fractal
         {
            if (a > Final[Index]) 
            { 
               Final[i] = a;     
               Index = i;   
            }
         }   
      }
      //--- Lower fractal (blue) found
      if (b != 0 && a == 0)   
      {  
         if (PrevFratal == OP_BUY)                       //if previously is a upper fractal
         {  
            if (b < Final[Index]) 
            { 
               Final[i] = b;    
               Index = i;   
            }
         } 
      }     
     i++; 
   }   
   
   //--- return value of prev_calculated for next call
   return(rates_total);

PS: The full code is a little too long, but basically prior to these code, there is a section for calculating ZigZag and Fractals.

 
Jia Soo:Thanks for replying. But I must say, I don't quite understand what you are trying to say, especially on item 4. Could you please elaborate if you don't mind?

Also, I don't quite get it when you said "process bar zero more than once, ever". If i understand your statement correctly, what you are trying to say is that this PrevFratal variable is being carried forward for next "new iteration" when new bar is formed. What I did is I reset the PrevFratal variable, equals to -1, everytime if a new bar is formed.

   for (i = 0; i < 30; i++)
   {   
  1. You are counting up. PrevFratal doesn't contain anything previous, it contains future values. Count down.
  2. Since you compute 30 bars every tick, my comment is irrevalent.