Trailing stop won't work

 

Hi friends, i made this EA that launches two diferent orders, so the thing is once the first target is hit, it is supposed to move the second order to a break even point, i used this code but it will only move the stoploss sometimes, most times it wont do anything wich is weird, it is like sometimes it doesn't select the order it should wich i don't get...


{


    for (int i = 0; i < OrdersTotal(); i++) {
        if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
            continue;
        }
        
        if (OrderSymbol() != Symbol()) {
            continue;
        }
        
        if (OrderType() == OP_BUY) {
            if (Bid - OrderOpenPrice() > trailingStop * Point && OrderStopLoss() < Bid - trailingStop * Point) {
                if (!OrderModify(OrderTicket(), OrderOpenPrice(),  OrderOpenPrice()+10 * Point, OrderTakeProfit(), 0, Green)) {
                    Print("OrderModify error ",GetLastError());
                }
                return(0);
            }
        } else if (OrderType() == OP_SELL) {
            if (OrderOpenPrice() - Ask > trailingStop * Point && OrderStopLoss() > Ask + trailingStop * Point) {
                if (!OrderModify(OrderTicket(), OrderOpenPrice(),OrderOpenPrice()-10* Point, OrderTakeProfit(), 0, Green)) {
                    Print("OrderModify error ",GetLastError());
                }
                return(0);
            }
        }
    
}}
 

Don't double post! You already had another thread open.

          General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)