Loops and Closing or Deleting Orders - page 3

To add comments, please log in or register
ffoorr
1340
ffoorr  

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;   
Simon Gniadkowski
17696
Simon Gniadkowski  
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.
mrwayne
252
mrwayne  
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 ...... )

Syed Naufal Gaddafi
1923
Syed Naufal Gaddafi  
Instead of asking, why not just try that? It would be your experience.
mrwayne
252
mrwayne  
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 ?
Syed Naufal Gaddafi
1923
Syed Naufal Gaddafi  
Try both and see if you can spot the differences.
Keith Watford
Moderator
17041
Keith Watford  
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 --)
James Hodges
6395
James Hodges  

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...

William Roeder
18943
William Roeder  
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.
Gordon Gekko
624
Gordon Gekko  

Hi experts, what is your opinion about this solution?

while(OrdersTotal()>0)
  {
   if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
     {
      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;
         case OP_BUYSTOP:
         case OP_SELLSTOP:
         case OP_BUYLIMIT:
         case OP_SELLLIMIT: if(!OrderDelete(OrderTicket())) Print("OrderDelete error ",GetLastError());
        }
     }
   else Print("OrderSelect error ",GetLastError());
  }
To add comments, please log in or register