Download MetaTrader 5
To add comments, please log in or register
Do you have Mac OS? Find out how to install MetaTrader on it!
mark101
82
mark101 2015.12.08 22:42 

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;
   }
Keith Watford
Moderator
7899
Keith Watford 2015.12.08 23:42  

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

mark101
82
mark101 2015.12.08 23:47  

GumRai:

should help you to solve your problem

Yes, indeed. Thank you!
Keith Watford
Moderator
7899
Keith Watford 2015.12.09 00:00  
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. 

/
To add comments, please log in or register