Close all orders for Symbol

 

Sorry for my English

I was using this function to close all opened orders for one symbol  if profit > xxx but the problem it close some of orders then the profit be <xxx and it stop closing other orders

//---------------------------------
 bool CloseDeleteAll()
{
    int total  = OrdersTotal();
      for (int cnt = total-1 ; cnt >=0 ; cnt--)
      {
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
       
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) 
         {
         if(OrderSymbol()==Symbol()){
            switch(OrderType())
            {
               case OP_BUY       :
               {
                  if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),0,Violet))
                     return(false);
               }break;                  
               case OP_SELL      :
               {
                  if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),0,Violet))
                     return(false);
               }break;
            }             
         
            if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP || OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)
               if(!OrderDelete(OrderTicket()))
               { 
                  Print("Error deleting " + OrderType() + " order : ",GetLastError());
                  return (false);
            }
             }  
          }
      }
      return (true);
}

and i found other function it close all orders and it close all open orders for the symbol but the problem its depending on while OrdersTotal >0  the problem is when there is other orders for other symbles the EA for this symble stop till OrdersTotal =0 , so how can i fix this function to depend on all orders for symbol not for OrdersTotal

//+------------------------------------------------------------------------+
void CloseAll()
{
   int i;
   bool result = false;

   while(OrdersTotal()>0)
   {
      // Close open positions first to lock in profit/loss
      for(i=OrdersTotal()-1;i>=0;i--)
      {
         if(OrderSelect(i, SELECT_BY_POS)==false) continue;

         result = false;
         if ( OrderType() == OP_BUY)  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 15, Blue );
         if ( OrderType() == OP_SELL) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 15, Blue );
      }
      for(i=OrdersTotal()-1;i>=0;i--)
      {
         if(OrderSelect(i, SELECT_BY_POS)==false) continue;

         result = false;
         if ( OrderType()== OP_BUYSTOP)  result = OrderDelete( OrderTicket() );
         if ( OrderType()== OP_SELLSTOP)  result = OrderDelete( OrderTicket() );
         if ( OrderType()== OP_BUYLIMIT)  result = OrderDelete( OrderTicket() );
         if ( OrderType()== OP_SELLLIMIT)  result = OrderDelete( OrderTicket() );
      }
      Sleep(1000);
   }
}

 
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
       
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) 

No need to repeat it.

                  if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),0,Violet))
                     return(false);

Why do you return if a single OrderClose() fails?

 
Keith Watford:

No need to repeat it.

Why do you return if a single OrderClose() fails?

do you mean if i delete return(false) it will close all symbol opening orders ?
 
Radonee:
do you mean if i delete return(false) it will close all symbol opening orders ?

No, I mean that if an OrderClose fails, then none of the remaining can be closed.

Instead of returning, you should be printing to the log the reason why it failed so that you can make corrections to your code. You do if deleting an order fails.

 
Keith Watford:

No, I mean that if an OrderClose fails, then none of the remaining can be closed.

Instead of returning, you should be printing to the log the reason why it failed so that you can make corrections to your code. You do if deleting an order fails.

becouse my English i didn't explain it good, i will try explain with example:

if i have 4 orders for EURUSD and i have 3 Orders USDJPY , i close all symbol orders if symbol orders profit >=100 and when the EURUSD orders profit is >=100 the EA will start close all EURUSD orders .

function 1 :the problem in function 1 is when the EA close 1 or 2 orders the eurusd orders be <100 then the EA stop closing other 2 EURUSD orders becouse the profit <100 .

function 2 : the EA already close the 4 opining EURUSD opening orders but the problem its close inside while(OrdersTotal>0) so the EA still inside while and stop continue on symbol EURUSD till USDJPY orders also is close to becose OrdersTotal=0 and after close USDJPY orders and OrdersTotal = 0 then it back to work normal.

so i want to edit function 2 to close all 4 EURUSD orders and EA go out while(OrdersTotal>0) and back to work again on symbol EURUSD.

sorry agian for my English

 

Check for the condition once and then close all orders.

If you check for the condition again, of course it will drop below the desired value.

You can just turn it into a function and call it:

CloseAllSymbol(_Symbol);
//+------------------------------------------------------------------+
//---> Close All for one symbol
//+------------------------------------------------------------------+
void CloseAllSymbol(string symbol)
  {
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS)==false) continue;
        {
         if(OrderSymbol()==symbol)
           {
            if(OrderType()==OP_BUY)
              {
               bool result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),15,Blue);
                 {
                  if(!result)
                    {
                     Print("OrderCloseError! Ticket: "+IntegerToString(OrderTicket()));
                    }
                 }
              }
            if(OrderType()==OP_SELL)
              {
               bool result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),15,Blue);
                 {
                  if(!result)
                    {
                     Print("OrderCloseError! Ticket: "+IntegerToString(OrderTicket()));
                    }
                 }
              }
            if(OrderType()==OP_BUYSTOP)
              {
               bool result=OrderDelete(OrderTicket());
                 {
                  if(!result)
                    {
                     Print("OrderCloseError! Ticket: "+IntegerToString(OrderTicket()));
                    }
                 }
              }
            if(OrderType()==OP_SELLSTOP)
              {
               bool result=OrderDelete(OrderTicket());
                 {
                  if(!result)
                    {
                     Print("OrderCloseError! Ticket: "+IntegerToString(OrderTicket()));
                    }
                 }
              }
            if(OrderType()==OP_BUYLIMIT)
              {
               bool result=OrderDelete(OrderTicket());
                 {
                  if(!result)
                    {
                     Print("OrderCloseError! Ticket: "+IntegerToString(OrderTicket()));
                    }
                 }
              }
            if(OrderType()==OP_SELLLIMIT)
              {
               bool result=OrderDelete(OrderTicket());
                 {
                  if(!result)
                    {
                     Print("OrderCloseError! Ticket: "+IntegerToString(OrderTicket()));
                    }
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
 
Marco vd Heijden:

Check for the condition once and then close all orders.

If you check for the condition again, of course it will drop below the desired value.

You can just turn it into a function and call it:

do i have to write  a function  check for close like this
void check_close_symbol(string symbol)
{
   if(profit(symbol)>100)CloseAllSymbol(symbol);
}

and call it in OnTick  ?

 
Radonee:

Sorry for my English

I was using this function to close all opened orders for one symbol  if profit > xxx but the problem it close some of orders then the profit be <xxx and it stop closing other orders

and i found other function it close all orders and it close all open orders for the symbol but the problem its depending on while OrdersTotal >0  the problem is when there is other orders for other symbles the EA for this symble stop till OrdersTotal =0 , so how can i fix this function to depend on all orders for symbol not for OrdersTotal

double Profit=300;
int start()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    if(OrderSymbol()==Symbol() && OrderProfit()>=Profit)
    {
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
   }
    
  }
  
  return(0);
}
 
Mehmet Bastem:

You need to check that OrderSelect has selected the order. The code can also be further simplified... 


bool close_all()
{
   bool result = true;
   for(int i=OrdersTotal()-1; i>=0; i--){
      if(OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == _Symbol){
         if(OrderType() < 2){
            if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 10)){
               result = false;
            }
         }
         else if(!OrderDelete(OrderTicket())){
            result = false;
         }
      }
   }
   return result;
}
 
This will not fix the problem , the problem is if i have many orders for example 20 orders and the condition is if the profit of all orders >=100  close and delete all , when the EA start close orders the profit be < 100 then the EA stop closing the orders, i want its still in colse and delete all function till close all 20 orders and after close all orders get out the function.
 
Radonee:
This will not fix the problem , the problem is if i have many orders for example 20 orders and the condition is if the profit of all orders >=100  close and delete all , when the EA start close orders the profit be < 100 then the EA stop closing the orders, i want its still in colse and delete all function till close all 20 orders and after close all orders get out the function.

That is precisely what the code above your post does.