Function to close orders :

Code:
//----------------------------------------------------- close

void close(int type){
   bool res=false;
   for(int j=0;j<OrdersTotal();j++){
      if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES)==true){ 
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic){
            if(OrderType()==OP_BUY && OrderType()==type){
               RefreshRates();
               res=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage*point/Point,Orange);
            }    
            if(OrderType()==OP_SELL && OrderType()==type){
               RefreshRates();
               res=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage*point/Point,Orange);
            }
         }
      }
   }
}