S/R Script Returning different result than expected

 

I've been trying to write a part of a Support and Resistance script, where a Horizontal Line is drawn where price has previously stalled, meaning that we've had either many closes at that price, or highs, or lows, or bars that have ranged across those levels.

 Then the script merges levels that are less than x amount of pips away from each other, then it removes the "0"s from the array, and draws the objects.

Not all the levels that I can personally see on the charts are actually being drawn. What do you think is wrong in the code?

 

   double PreviousCloses[10000] = {};
   double PreviousHighs[10000] = {};
   double PreviousLows[10000] = {};
   double Levels[10000] = {};
   double LevelsCleaned[10000]={};
   
   ObjectsDeleteAll(NULL,0,OBJ_HLINE);
   
   for ( int a = 0; a <= TestPeriod - 1 ; a++)
   {
   PreviousCloses[a] = Close[a];
   PreviousHighs[a] = High[a];
   PreviousLows[a] = Low[a];
   }
   
   int CurrentCloseOccurences[10000] = {};
   int CurrentCloseTimes = 0;
   
   for ( int a = 0; a <= ArraySize(PreviousCloses) - 1 ; a++)
   { 
     
      for ( int b = 0; b <= ArraySize(PreviousCloses) - 1 ; b++)
      {  
         if(PreviousCloses[b] == PreviousCloses[a] || PreviousHighs[b] == PreviousCloses[a]
            || PreviousLows[b] == PreviousCloses[a] || ( PreviousHighs[b]>PreviousCloses[a] 
            && PreviousLows[b]<PreviousCloses[a] ) )
              CurrentCloseTimes++;

      }
     CurrentCloseOccurences[a] = CurrentCloseTimes;
     
     if( CurrentCloseOccurences[a] >= MinimumOccurences)
     Levels[a]=PreviousCloses[a];
   }
      
      
  int j = 0;
  for(int i = 1; i<=ArraySize(Levels)-1; i++)
  {
   if(Levels[i]==Levels[j])
   {
   Levels[j]=0;
   }
   
   j++; 
   
  }
  j=0;
  
  ArraySort(Levels,WHOLE_ARRAY,0,MODE_ASCEND);
  for(int i = 1; i<=ArraySize(Levels)-1; i++)
  {
   if((Levels[i]-Levels[j])<MinimumSRInterval*10*Point()&& Levels[i]!=0)
   {
   Levels[i] = Levels[i] - (Levels[i] - Levels[j])/2;
   Levels[j]=0;
   } 
   j++;
  }

 ArrayCopy(LevelsCleaned,Levels,0,0,WHOLE_ARRAY);
 ArraySort(LevelsCleaned,WHOLE_ARRAY,0,MODE_DESCEND);
 for(int a = ArraySize(LevelsCleaned)-1; a >= 0; a--)
 {
   if(LevelsCleaned[a] == 0)
   ArrayResize(LevelsCleaned,(ArraySize(LevelsCleaned)-1));
 }
 for(int b = 0; b<ArraySize(LevelsCleaned); b++)
 {
    if(LevelsCleaned[b]!=0)
   ObjectCreate(IntegerToString(b),OBJ_HLINE,0,0,LevelsCleaned[b]);
 }
 

I can't comment on your code, but I just finished a similar script that does something similar. 

 


Reason: