# Loops and Closing or Deleting Orders - page 3

1340

What if we do a loop like this one,

```  for(int i=0; i< OrdersTotal(); i++) // For market and pending orders
{
if((OrderSelect(i,SELECT_BY_POS)==true)     //If there is the next one
&& (OrderSymbol()==Symbol()) && OrderMagicNumber() == Magic_A )
{```

This is the loop given in the book mql4 to create array for order.

Will the OrdersTotal() decrease by itself each time an order is closed ?

//***************************************************************************

I just have a look in the book, below is the loop given to close an order, so the anwers is yes : ( https://book.mql4.com/trading/orderclose )

``` for(int i=1; i<=OrdersTotal(); i++)          // Order searching cycle
{
if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
{                                       // Order analysis:
//----------------------------------------------------------------------- 3 --
if (OrderSymbol()!= Symb) continue;   ```
17696

ffoorr:

What if we do a loop like this one,

This is the loop given in the book mql4 to create array for order.

Will the OrdersTotal() decrease by itself each time an order is closed ?

Yes it will . . . but the fact remains nt all the Orders will be closed. Read the first 2 posts in this thread . . . it is explained in detail.
252

```int TotalNumberOfOrders;   //  <-- this variable will hold the number of orders currently in the Trade pool

TotalNumberOfOrders = OrdersTotal();    // <-- we store the number of Orders in the variable

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)  //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO ! ```

Is it okay not to declare TotalNumberOfOrders = OrdersTotal() ;

What I mean was straight away use OrdersTotal()

for ( x = OrdersTotal() - 1 ...... )

1923

252

I'm currently using that but I'm not sure about to output even though it is the same . Since OrdersTotal() is in the for loop, will closing 1 order affect the OrdersTotal again and again ?
1923

Try both and see if you can spot the differences.
Moderator
17041

juniorlcq:
I'm currently using that but I'm not sure about to output even though it is the same . Since OrdersTotal() is in the for loop, will closing 1 order affect the OrdersTotal again and again ?

No, because you are not checking OrdersTotal, you are checking PositionIndex as the condition to continue the loop

`for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)`
6395

Excellent job of explaining this topic! Without out the graphics it's just impossible to explain... Well done.

The only time I have been forced to use the count up method instead of the countdown method was on a broker

that was really xxx about FIFO and would not let me close the newest trade first.... how stupid.

Again I say ... well done...

18943

Jimdandy: The only time I have been forced to use the count up method instead of the countdown method was on a broker that was really anal about FIFO and would not let me close the newest trade first.... how stupid.
In that case, capture all relevant ticket numbers (in an array) and then close. Don't count up AND close.
624

```while(OrdersTotal()>0)
{
{
switch(OrderType())
{
case OP_BUY:  if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),MaxSlippage,CloseColor)) Print("OrderClose error ",GetLastError()); break;
case OP_SELL: if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),MaxSlippage,CloseColor)) Print("OrderClose error ",GetLastError()); break;