Code to close all buy/sell by group

 

Hello,

I cannot find best way to close all buy/sell trade by group (string comment or magic).

I use this code to open trades...someone could suggest the solution please?

Many thanks!


//+==================================================================================================+
//| FILTER BUY
//+==================================================================================================+

   
   
     if   (CheckForBuy())
     
        {
   
         if(PositionSelect(_Symbol))
           {
            switch((int)PositionGetInteger(POSITION_TYPE))
              {
               case POSITION_TYPE_BUY:
                 if(PositionGetDouble(POSITION_PROFIT)<=0)
                    return;
                 break;
               case POSITION_TYPE_SELL:
                  Trade.PositionClose(_Symbol);
                  break;
              }
           }
         if(!Trade.Buy(Lot,_Symbol,ask,0,0,"Group_1"))
            Print("Error of open BUY ORDER Group_1"+Trade.ResultComment());
            
        }
     
//+==================================================================================================+
//| FILTER SELL
//+==================================================================================================+     
    
    if   (CheckForSell())//(CheckForSell())
   
     {
      if(PositionSelect(_Symbol))
        {
         switch((int)PositionGetInteger(POSITION_TYPE))
           {
            case POSITION_TYPE_SELL:
              if(PositionGetDouble(POSITION_PROFIT)<=0)
                 return;
              break;
            case POSITION_TYPE_BUY:
               Trade.PositionClose(_Symbol);
              break;
           }
        }
      if(!Trade.Sell(Lot,_Symbol,bid,0,0,"Group_1"))
         Print("Error of open SELL ORDER Group_1 "+Trade.ResultComment());
     }
   return;
   
//+==================================================================================================+
//| CLOSE ALL BUY/SELL GROUP 1
//+==================================================================================================+
   
   if (closebuy_1)
      {
         // CODE TO CLOSE ALL BUY GROUP 1      
      }
   
   
   if (closesell_1)
      {
         // CODE TO CLOSE ALL SELL GROUP 1     
      }
      
  
  } // END
Reason: