Close SELL order after opening a BUY order

 

I want to close SELL order after a BUY order is opened.

I wrote this code but I have errors 138 and 4108, how can I fix it?


ps: the orders are generated by SELL LIMIT and BUY LIMIT functions


   if (OrderSelect(ticketBuy,SELECT_BY_TICKET)){   
   
      if  ( OrderType() == OP_BUY ){   
      
         if (OrderSelect(ticketSell,SELECT_BY_TICKET)){
         
            if  ( OrderType() == OP_SELL ){
            
               Print( "Ticket SELL da cancellare è il #: " , ticketSell);
          
               if (!OrderClose(ticketSell,OrderLots(),Ask,0,clrCoral)) {  // Chiusura ordine SELL
                
               stampa("Errore in OrderClose dell'ordine SELL ", GetLastError());
                  
                  }        
                                       
            } 
                           
         }
                   
      } 
 
   }
Files:
 

For a start you should use

  if  ( OrderType() == OP_SELL && OrderCloseTime()==0){


to prevent trying to close an order that is already closed

 if (!OrderClose(ticketSell,OrderLots(),OrderClosePrice(),0,clrCoral)) {  // Chiusura ordine SELL


Use OrderClosePrice() instead of Ask as it will be current from the OrderSelect()


but I have errors 138 and 4108
When asking for help please look up and post what the error return codes mean. That way everybody who reads your post does not have to look them up.
Reason: