Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 133

 
Hi All. Can you tell me how to change this code for(int i=OrdersTotal()-1;i>=0;i--){
At the moment it is closing the most recently opened ones. I want it to close the first ones first!
 
Rustam Bikbulatov:
Hi All. Can you tell me how to change this code for(int i=OrdersTotal()-1;i>=0;i--){
At the moment it is closing the most recently opened ones. I want it to close the first ones first!
for(int i=0; i<OrdersTotal(); i++)
 
Rustam Bikbulatov:
Hi All. Please tell me how to change this code for(int i=OrdersTotal()-1;i>=0;i--){
At the moment it is closing the most recently opened ones. You want it to close the first ones first!
for( int i = 0; i < OrdersTotal(); i++ )
   {
    if( /*Функция закрытия. Если вернула истину*/ )
       i--;
   }

Try it this way. Instead of comments, you should use the function to close the order. I have not checked its functionality. And here, I think that we will have to call the function that calculates the amount of orders each time, since the number of orders will decrease.

You may also try such a variant:

while( OrdersTotal() > 0 )
   /*Функция закрытия ордера*/
 
Vitaly Muzichenko:
for(int i=0; i<OrdersTotal(); i++)
it doesn't close properly)
 
Rustam Bikbulatov:
it closes in an awkward way)
The correct way is to remember which orders need to be closed, sort the list as you need and close in that order

not correct, but for now (up to some build) it works: on successful closing of i--



 
Rustam Bikbulatov:
Hi All. Can you tell me how to change this code for(int i=OrdersTotal()-1;i>=0;i--){
At the moment it is closing the most recently opened ones. I want it to close the first ones first!

//+------------------------------------------------------------------+
//|           Закрыть все позиции начиная с первых                   |
//+------------------------------------------------------------------+

double CloseOrders()

{
  for(int i=0; i<OrdersTotal(); )
  {
    if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
    break;
    
    if (OrderMagicNumber() != mn)
    break;
    
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage, CloseColor );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage, CloseColor );
                          break;
    }
    
    if(result == false)
    {
      Print("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      i++;
    }  
  }
return (True);
}
// End
 
Rustam Bikbulatov:
It closes in an awkward way)
The option I have suggested is generally incongruous for closure.
If you are talking about closing from old to new, then you are closing the grid. Having some experience with grids, all I can say for sure is that you should not choose by the age of the position, but by its lot, and you should close from a larger lot to a smaller one. If the grid is single lot, then there is no difference in the order of closing. Use ready-made functions to close, and do not invent something that has already been invented)
 
Thanks so much guys! I've already sorted it out!!!
 
Vitaly Muzichenko:
The option I have suggested is generally incongruous for closure.
If you are talking about closing from old to new, then you are closing the grid. Having some experience with grids, all I can say for sure is that you should not choose by the age of the position, but by its lot, and you should close from a larger lot to a smaller one. If the grid is single lot, then there is no difference in the order of closing. Use ready-made functions for closing, and do not invent something that has already been invented)
That's right. You're right!
 
Gentlemen Developers! Good day to all. I am interested in the question of creating a template for an Expert Advisor (script) when creating it. Can this be edited somewhere and how is it done?
Reason: