Looping difference confusion

 
  1. Since a long time i am using (1a)
  2. for(int i=OrdersTotal()-1; i >= 0; --i) // Suggested by most of the experts
    for looping through orders, but whenever i come to forum for some guidance i often find that people are using (1b)
    for(int i=1; i<=OrdersTotal(); i++) // from the documentation

    whats the difference?

  3. if i use
    for(int i=OrdersTotal(); i > 0; --i)
    in my code, what exactly it means.
Expecting a reply especially from @William Roeder
     
    I'm assuming from your previous posting history, that this is about MQL4 and not MQL5, so I am moving it to the correct section: MQL4 e MetaTrader 4
     

    The best method is the first one (1a). It indexes correctly and allows for deleting/closing orders within the loop without having to reset the loop.

    for(int i=OrdersTotal()-1; i >= 0; --i) // Suggested by most of the experts

    Method (1b) is acceptable only if no orders are closed or deleted during the loop, so it is not ideal nor recommended.

    Method (2) is completely wrong because the indexing is offset by one which is incorrect.

     

    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in an index loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions (from zero).
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    and check OrderSelect in case other positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

     
    Fernando Carreiro #:
    I'm assuming from your previous posting history, that this is about MQL4 and not MQL5, so I am moving it to the correct section: MQL4 e MetaTrader 4
    Thanks. I will try to post in relevant section in future
     
    Fernando Carreiro #:

    The best method is the first one (1a). It indexes correctly and allows for deleting/closing orders within the loop without having to reset the loop.

    Method (1b) is acceptable only if no orders are closed or deleted during the loop, so it is not ideal nor recommended.

    Method (2) is completely wrong because the indexing is offset by one which is incorrect.

    So
    for(int i=OrdersTotal()-1; i >= 0; --i)
    is not actually used for excluding pending orders. Right?
     
    Qæs #: So is not actually used for excluding pending orders. Right?
    In MT4/MQL4, "orders" refers to both pending and market orders. They are all in the same pool. They are not segregated.
     
    William Roeder #:

    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in an index loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions (from zero).
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    and check OrderSelect in case other positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

    Thanks for reply.

    Right now i am working on to get properties or first order, last order, second last order and last order of one symbol (EURUSD), one chart, one EA (My EA), one magic number (777), either buy or sell order but not a pending order.

    My EA does not have code for opening, deleting pending orders

    it has code for opening, modifying, closing market orders

    using count down i can get the properties of 1st and 2nd and last order from the trading pool.

    how i will be able to choose 2nd last order using count down? Any advice please?

     
    Fernando Carreiro #:
    In MT4/MQL4, "orders" refers to both pending and market orders. They are all in the same pool. They are not segregated.
    int count(int type = -1)
      {
       int n = 0;
       for(int i=OrdersTotal()-1; i >= 0; --i)
          if(OrderSelect(i,0)
             && OrderSymbol() == Symbol()
             && OrderMagicNumber() == magicID
             && (type == -1 || OrderType() == type)
             && OrderType() < 2                  // i added this to exclude pending orders.
            )
             ++n;
       return n;
      }
    void OnTick()
      {
       if(count()==0){...}
      }
    is it right or is there any better way of getting total orders count excluding pending orders?
    Reason: