Download MetaTrader 5

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

To add comments, please log in or register
Robert Browne
Robert Browne 2016.10.13 01: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


Keith Watford
Keith Watford 2016.10.13 02: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 07: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.

Ex Ovo Omnia
Ex Ovo Omnia 2016.10.13 07: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.

To add comments, please log in or register