Help me with the code

 

Hello I have expert advisor to the following modification: The expert works martingale type, give orders, 0.10, 0.20, 0.30,0.40, when you have many open orders, sometimes you can not close them all, because the price has changed. I need to change the advisor in this function whenever reaches the profit is to close all orders even if the price change, someone can help me. Send the code as it is, can I change what I intend to run. Sorry about my English.

 
//+------------------------------------------------------------------+
//| Partial order close.                                             |
//+------------------------------------------------------------------+
#define INF 0x6FFFFFFF // Not quite infinite, Jul 2029, or 1,879,048,191
bool    CloseOrder(int ticket=EMPTY, double size=INF){  // INF == entire.
    if      (ticket == EMPTY)   ticket = OrderTicket();
    else if (!OrderSelect(ticket, SELECT_BY_TICKET)){
        Alert("OrderSelect(",ticket,",ticket) failed: ", GetLastError());
                                                                return(false); }
    double  minLot      = MarketInfo(chart.symbol, MODE_MINLOT),
            lotStep     = MarketInfo(chart.symbol, MODE_LOTSTEP),
            sizeCurr    = OrderLots(),
            sizeClose   = MathRound(size/lotStep)*lotStep,
            sizeRem     = sizeCurr - sizeClose;

    if (sizeClose < minLot)                                     return(false);
    if (sizeRem   < minLot){    sizeClose = sizeCurr;   // Close all
        color   op.color    = IfI(Color.Buy, Color.Sell);   }
    else        op.color    = Aqua;

    if (OrderClose( ticket, sizeClose, OrderClosePrice(),
                    Slippage.Pips*pips2points, op.color ))      return(true);
    Alert("OrderClose(ticket=", ticket, ", ...) [1] failed: ", GetLastError());
                                                                return(false);
}
void CloseAllOrders(int op = -1){
    for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber() == Magic.Number                  // my magic number
    &&  OrderSymbol()      == chart.symbol                  // and my pair,
    &&  (op < 0 || op == OrderType())                       // and wanted type.
    ){  // Don't combine with &&'s Compiler bug.
        CloseOrder();
    }
}
 
WHRoeder:


Thank you