Download MetaTrader 5
To add comments, please log in or register
Are you used to trading from your smartphone? Mobile version of MetaTrader 5 will suit you best!
Robert Browne
88
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(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 

Keith Watford
Moderator
7979
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
969
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.

Ovo Cz
2951
Ovo Cz 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