Help with code: Partial Close orders

 

Hello coders, I'm trying to implement a partialclose function to my trade manager, but I don't actually know how to make it work as expected.

With this code I get several partial closes at specified ClosePrice, what I need is to partial close at distance from the OpenPrice for first step (this does) and the following steps from the last close.

            double ClosePrice=NormalizeDouble(LastClosePrice+(OrderTakeProfit()-LastClosePrice)*(PartialClose/100),MarketInfo(OrderSymbol(),MODE_DIGITS));
            if(OrderType()==OP_BUY&&PartialClose>0&&MarketInfo(OrderSymbol(),MODE_ASK)>=ClosePrice){
               if(!OrderClose(OrderTicket(),NormalizeDouble(OrderLots()*(PartialClose/100),2),ClosePrice,0,clrLawnGreen)){
                  Print(OrderSymbol()," PC OrderClose error ",GetLastError());
                  return;
                  }
               }
            if(OrderType()==OP_SELL&&PartialClose>0&&MarketInfo(OrderSymbol(),MODE_BID)<=ClosePrice){
               if(!OrderClose(OrderTicket(),NormalizeDouble(OrderLots()*(PartialClose/100),2),ClosePrice,0,clrLawnGreen)){
                  Print(OrderSymbol()," PC OrderClose error ",GetLastError());
                  return;
                  }
               }

If someone can help with this, thank you in advance.

 
There must be an update of LastClosePrice If the partial close is succesful.
Also i assume you are handling single orders ,the ticket changes when you partially close an order.
 
Lorentzos Roussos:
There must be an update of LastClosePrice If the partial close is succesful.
Also i assume you are handling single orders ,the ticket changes when you partially close an order.

All open orders are managed from 1 chart. I tried this but unuseful:

double LastClosePrice(){
   double LastClose;
   if(OrderSelect(0,SELECT_BY_POS,MODE_HISTORY)==true){
      if(OrderSymbol()==Symbol()){
         LastClose=OrderClosePrice();
         if(OrderClosePrice()==0){
            LastClose=OrderOpenPrice();
            }
         }
      }
   return(LastClose);
   }
 
  1. NormalizeDouble(OrderLots()*(PartialClose/100),2)
    This amount must be between minLot and OrderLots -minlot.

  2.    if(OrderSelect(0,SELECT_BY_POS,MODE_HISTORY)==true){
    
    If the order is in history, it's closed (or deleted.)