Partial close help

 
Hello, I've written this partial close function but it's not working as it's supposed to. I need your help in correcting the bug so that it can work in my code and help in closing orders partially after certain % of pips has been achieved.
void ClosePartial()
  {
        if(OrdersTotal())
          {
              if(!OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
              Print("Unable to select order");
              double op = OrderOpenPrice();
              int type = OrderType();
              double lots = OrderLots();
              int ticket = OrderTicket();
              double profit = OrderProfit();
              if(lots == Lots)
                {
                     if(type == OP_BUY && Bid - op > (PC_points * Point))
                     {
                         Comment("qerty");
                         if(!OrderClose(ticket,lots /2 ,Bid,3,Green))
                         Print("Failure to close order");
                     }
                   else if(type == OP_SELL && op - Ask > (PC_points * Point))
                     {
                        if(!OrderClose(ticket,lots / 2,Ask,3,Green))
                        Print("Failure to close order");
                     }  
                }
          } 
       
  }
  
 
if(!OrderClose(ticket,lots /2 ,Bid,3,Green))

You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot.

You also must check if you have already done it, to avoid repeated closing. Alternatives:

  • Move SL to Break Even+1 before the partial close. That way, you know that you already did it.
  • Set a flag in persistent storage (files, global variables w/flush)
  • Open two orders initially, and close one (manually or by TP.)
 
William Roeder #:

You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot.

You also must check if you have already done it, to avoid repeated closing. Alternatives:

  • Move SL to Break Even+1 before the partial close. That way, you know that you already did it.
  • Set a flag in persistent storage (files, global variables w/flush)
  • Open two orders initially, and close one (manually or by TP.)

Thank you for your help

 
Ernest Akoyi:
Hello, I've written this partial close function but it's not working as it's supposed to. I need your help in correcting the bug so that it can work in my code and help in closing orders partially after certain % of pips has been achieved.


For partial close purposes, there is already an OrderCloseBy function that you can use.

 
Nino Guevara Ruwano #:


For partial close purposes, there is already an OrderCloseBy function that you can use.

Thank you  I already got the solution