I don't understand why the -1 in for loop OrdersTotal() - 1

 

The book says: The example below closes all sell orders currently opened by an expert advisor:

I think I understand most of this code, but the:

OrdersTotal() - 1

I don't get. Why - 1?

Thanks.

for(int order = 0; order <= OrdersTotal() - 1; order++)
{
  bool selected = OrderSelect(order, SELECT_BY_POS);
  
  if(OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber && selected == true)
  {
      // Close order
      bool closed = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clrRed);
      if(closed == true) order--;
  } 
   
}
 
afiverestatcastle:

The book says: The example below closes all sell orders currently opened by an expert advisor:

I think I understand most of this code, but the:

I don't get. Why - 1?

Thanks.

Because OrderSelect starts selecting from address 0 to Max number of position - 1

Let say you have 3 open positions

First address will be 0

second address will be 1

third address will be 2( 2  But not 3. How to get the 2, 3-1=2)

As It started counting from 0 NOT from 1 that is reason why It ends at max-1

 

I see;

OrdersTotal()

will equal 3, but we need it to equal 2 because the array will count: 0,1,2.

Thanks.

 
Exactly
 
  1. for(int order = 0; order <= OrdersTotal() - 1; order++)
    is exactly the same as
    for(int order = 0; order <  OrdersTotal()    ; order++)

  2. Why did you post your MT4 question in the Root / instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  3. bool selected = OrderSelect(order, SELECT_BY_POS);
    
    if(closed == true) order--;
    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:
    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum
      For In First Out (FIFO rules-US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order, close it, and on a successful operation, reprocess all remaining positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16

    2. and check OrderSelect in case earlier 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
    3. 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 (be direction independent and use) OrderClosePrice().

 

Why did you post your MT4 question in the Root / instead of the MQL4 section, (bottom of the Root page?)
          General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon.


Will do next time.

Thanks.

Reason: