CloseAll function

 
Guys good evening I made this function that should close me all open orders
void CloseAll( int Magic)
{
   for( int i = OrdersTotal(); i >= 0; i-- )
   {
      if( OrderSelect( i, SELECT_BY_POS ) && OrderMagicNumber() == Magic )
      {
         for( int k = 0; k < 10; k++ )
         {
            RefreshRates();
            if( !OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 20, clrRed ))
            {
               Print("Error close order sell!, error#", GetLastError());
            }
            else 
            {
            
               Print("Chiuso tutto" , GetLastError());
            }
         }
      }
   }
}

But it always gives me the error #4108 that "it doesn't know the tiket id" and since I tried to do it in various ways and the error is always the same I resort to your help, where am I wrong?



 
texoro:
Guys good evening I made this function that should close me all open orders

But it always gives me the error #4108 that "it doesn't know the tiket id" and since I tried to do it in various ways and the error is always the same I resort to your help, where am I wrong?



then try this


//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void CloseAll(int magicNumber)
  {
   for(int i=OrdersTotal()-1 ; i>=0 ; i--)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
         if(OrderMagicNumber()==magicNumber)
            switch(OrderType())
              {
               case OP_BUY:
                  trade=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),30,clrBlue);
                  break;

               case OP_SELL:
                  trade=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),30,clrRed);

              }
  }
//+------------------------------------------------------------------+
 
void CloseAll( int Magic)
{
   for( int i = OrdersTotal(); i >= 0; i-- )
   {
      if( OrderSelect( i, SELECT_BY_POS ) && OrderMagicNumber() == Magic )
      {
         for( int k = 0; k < 10; k++ )
         {
            RefreshRates();
            if( !OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 20, clrRed ))
            {
               Print("Error close order sell!, error#", GetLastError());
            }
            else 
            {
            
               Print("Chiuso tutto" , GetLastError());
            }
         }
      }
   }
}

Either remove the highlighted part (you are trying to close the same order over and over).

Or

void CloseAll( int Magic)
{
   for( int i = OrdersTotal(); i >= 0; i-- )
   {
      if( OrderSelect( i, SELECT_BY_POS ) && OrderMagicNumber() == Magic )
      {
         for( int k = 0; k < 10; k++ )
         {
            RefreshRates();
            if( !OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 20, clrRed ))
            {
               Print("Error close order sell!, error#", GetLastError());
            }
            else 
            {
               break;
            }
         }
      }
   }

so that it will exit the loop when an order is closed successfully.

 

I forgot to add.

for( int i = OrdersTotal(); i >= 0; i-- )

should be

for( int i = OrdersTotal()-1; i >= 0; i-- )
 
Comments that do not relate to this topic, have been moved to "Off Topic Posts".
Reason: