Help with for loop.

 
void LoadSnR()
{

// function searches the charts for support adn resitance and stores the level in global varibale SnR[] and the amount of times touched in
// SnRCount. Global variable BarsPassed counts how many bars have passed since the last check for SnR levels and the function sets as many bars 
// back as BarsOassed defines. BarsPassed is initialised with -5
int i,j;
  double CurrentHigh, CurrentLow;
  int Place;
  
 int  Handle;
  
//----
   
   
   // Get the range of the current screen
   
   CurrentHigh = WindowPriceMax(0);
   CurrentLow = WindowPriceMin(0);
   
  
  // Sets the tolerance according to Timeframe
 
  switch (Period())
  {
   case 1: Tolerance =5;
   case 5: Tolerance =8; 
   case 15: Tolerance = 13;
   case 30: Tolerance =21;
   case 60: Tolerance = 34;
   case 240: Tolerance  =55;
   case 1440: Tolerance = 89;
   case 10080: Tolerance = 144;
  }   
  Tolerance = Tolerance *Point;
  if (BarsPassed == -5) // if this is the first check
   {
   BarsPassed =Bars; 
   // Begin search between range
   
   }
   
   for (i =BarsPassed ; i>=1; i--)
   
      {
      
      
            // This Part Finds Resistance        
            
          if (High[i] > High [i+1] && High[i] >High[i-1])
             {// if a high has been found
               
                    // Compare to previous prices
                   
                   
                   
                   
                   if (ArraySize(SnR) > 0)
                   {
                     Place = ArrayBsearch(SnR,High[i]); // uf the array has items in it mark the closest level to the found High
                   }
                   
                   else // if arraysize is 0 then open 1 cell
                      {
                      ArrayResize(SnR,ArraySize(SnR) + 1);
                      ArrayResize(SnRCount,ArraySize(SnRCount) +1);
                      SnR [ ArraySize(SnR) -1 ] = 0;
                      SnRCount [ArraySize(SnRCount) -1] = 0;
                      }
                      
               if (Place !=666666) // irelevant. 
               {
                   if  (  SnR [Place]>= High[i] - Tolerance && SnR[Place] <= High[i] + Tolerance ) // Maybe a problem
                      {
                        SnR[Place] =(SnRCount[Place]*SnR[Place] + High[i]) / (SnRCount[Place] +1);
                        SnR[Place] = NormalizeDouble(SnR[Place],Digits);
                        SnRCount [Place]++; // if the levels matched count that level
                      }
                      
                   
                   else 
                      { // if they did not then make a new space
                      ArrayResize(SnR,ArraySize(SnR) + 1);
                      ArrayResize(SnRCount,ArraySize(SnRCount) +1);
                      SnR [ ArraySize(SnR) -1 ] = High[i];
                      SnRCount [ArraySize(SnRCount) -1] = 1;
                      }
                      
                      
                  }    
            }
            
      // This Part Finds Support      
         if (Low[i] < Low [i+1] && Low[i] >Low[i-1])
                  {// if a high has been found
               
                    // Compare to previous prices
                   
                   
                   
                   
                   if (ArraySize(SnR) > 0)
                   {
                     Place = ArrayBsearch(SnR,Low[i]);
                   }
                   
                   else // if arraysize is 0 then open 1 cell
                      {
                      ArrayResize(SnR,ArraySize(SnR) + 1);
                      ArrayResize(SnRCount,ArraySize(SnRCount) +1);
                      SnR [ ArraySize(SnR) -1 ] = 0;
                      SnRCount [ArraySize(SnRCount) -1] = 0;
                      }
                      
             if (Place !=666666)            
               {
                   if  (  SnR [Place]>= Low[i] - Tolerance && SnR[Place] <= Low[i] + Tolerance  ) // Maybe a problem
                      {
                        SnR[Place] =(SnRCount[Place]*SnR[Place] + Low[i]) / (SnRCount[Place] +1);
                        SnR[Place] = NormalizeDouble(SnR[Place],Digits);
                        SnRCount [Place]++; // if the levels matched count that level
                      }
                      
                   
                   else 
                      { // if they did not then make a new space
                      ArrayResize(SnR,ArraySize(SnR) + 1);
                      ArrayResize(SnRCount,ArraySize(SnRCount) +1);
                      SnR [ ArraySize(SnR) -1 ] = Low[i];
                      SnRCount [ArraySize(SnRCount) -1] = 1;
                      
                      }
                      
               }       
                      
                  }    
   
   }
   
   

} 
 

Hello all.

Sorry That this is split but i was getting an error that text is too long.

The above function is part of an EA that trades S/R levels. I am having problems witht he first for loop which is cycling between 200- 210 istead of between BarsPassed and 1. If anyone can help me find the reason i would be most thankfull

Thanks.
Tal

 
talolard:

Hello all.

Sorry That this is split but i was getting an error that text is too long.

The above function is part of an EA that trades S/R levels. I am having problems witht he first for loop which is cycling between 200- 210 istead of between BarsPassed and 1. If anyone can help me find the reason i would be most thankfull

Thanks.
Tal

From the first look I see this:

for (i =BarsPassed ; i>=1; i--)

On the first loop BarsPassed=Bars and later you are trying to compare High[i] to High[i+1] which is the same as High[Bars+1]. This may cause some problems as there is no such value.

So it's better using BarsPassed=Bars-1;

 
robofx.org:

From the first look I see this:

On the first loop BarsPassed=Bars and later you are trying to compare High[i] to High[i+1] which is the same as High[Bars+1]. This may cause some problems as there is no such value.

So it's better using BarsPassed=Bars-1;

Good point. Thanks for that.

Still doesnt solve the wierd behaviour of the loop though.

Thanks

Tal

 
talolard:

Good point. Thanks for that.

Still doesnt solve the wierd behaviour of the loop though.

Thanks

Tal

Another thing is that you have to put break after every case. Not sure if it has anything to do with the for loop but without breaks this will be always the case -> Tolerance = 144;