How to equalize iterations for skipped Sunday bars?

 

Hi guys,

Basically the indicator gathers data over the last x bars (given as length).

If the indicator is used on D1 it is supposed to ignore Sunday bars, hence the if-clause.

I don't want to just skip the Sunday with continue but also still look back exactly x bars. So I add to length with length++.

Butt the length++ - line leads to weird results and finally after half way through to an array out of range Error.

   for(int i=limit; i>=0; i--)
   {   
      //Look back x bars (=length)
      for(int y=0; y<length; y++)
      {
         //If using D1 ignore Sunday bar
         if((Period()==PERIOD_D1) && TimeDayOfWeek((Time[i+y]))==0)
         {
            length++;
            continue;
         }
                  
         //Some calcualtions on Bar i+y
         //...
         //...

      }
      Buffer_0[i] = result;
   }
 

If length is a global, static or extern variable, you will be increasing it every time a Sunday is found. If so, you will need to use an intermediate variable.

   for(int i=limit; i>=0; i--)
     {
      int temporary_length=length;
      //Look back x bars (=length)
      for(int y=0; y<temporary_length; y++)
        {
         //If using D1 ignore Sunday bar
         if((Period()==PERIOD_D1) && TimeDayOfWeek((Time[i+y]))==0)
           {
            temporary_length++;
            continue;
           }

         //Some calcualtions on Bar i+y
         //...
         //...

        }
      Buffer_0[i]=result;
     }

 should help you to solve your problem

 

GumRai:

should help you to solve your problem

Yes, indeed. Thank you!
 
Alexnice:
Yes, indeed. Thank you!

That's good.

This type of problem often comes up with externs.

In my opinion it is good to get into the habit of using input instead of extern. input is a constant and if you try to change its value in the code, the compiler will tell you. 

Reason: