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

Back to topics list To post a new topic, please log in or register
forexbob1970 2016.10.13 03:58

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(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic_No || OrderComment()!= CurrentBoxPattern) continue; //not my order

         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



GumRai 2016.10.13 04:24 #

Definitely count down when closing orders, deleting objects etc.

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

honest_knave 2016.10.13 09:37 #

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.

Ovo 2016.10.13 09:43 #

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.

Back to topics list  

To add comments, please log in or register