for(int pos = orders_total-1; pos >=0; pos--) VS for(int pos = 0; pos < orders_total; pos++)

 

Hi all

Been searching around the forum looking for references in regard to when to use "for - count down" vs "for - count up" statements.

I use the following code to check open orders in my bot

   int orders_total = OrdersTotal();
   
   for(int pos = orders_total-1; pos>=0; pos--)
     {
      if(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic_No || OrderComment()!= CurrentBoxPattern) continue; //not my order

         TradePattern=false;
         
         int ticket = OrderTicket();
         int type   = OrderType();
         double price = OrderOpenPrice();
         double stop  = OrderStopLoss();
         double profit= OrderTakeProfit();
       
         //--- insert function
        }
      }

 

 but because of my very limited skills are unsure of the pros and cons of using counting down vs counting up inside a "for" statement. And for the life of me can't find the threads that I've seen before addressing this.

 Can someone please expand on this or if they have them, provide the links to relevant threads.

 Many thanks

Bob 

 

Definitely count down when closing orders, deleting objects etc.

Generally, I will always count down unless there is a particular reason to count up.

 

It depends on what you are doing.

As GumRai said, with closing orders etc you should always count down. There is a very good post here explaining why.

If you are working with timeseries (i.e. the bigger the number the older the bar) you may also want to count down to avoid calculations being based on bars that hadn't happened yet. It also makes it easier to avoid array out of range errors IMHO.

 

Count down when deleting. Removing an indexed member reindexes other members, but removing the highest index is safe. Alternatively you may loop removing index 0 until there is a member.

Reason: