void ModifyOrders(int otype) { double avgprice = 0, order_lots = 0; price = 0; for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype) { price += OrderOpenPrice() * OrderLots(); order_lots += OrderLots(); } } } avgprice = NormalizeDouble(price / order_lots, Digits); if(!avgprice ) return; if(otype == OP_BUY) if(Bid - OrderOpenPrice() >= SL && SL != OrderStopLoss ()) SL = NormalizeDouble(avgprice + TrailingStop * Point, Digits); if(otype == OP_SELL) if(OrderOpenPrice() - Ask >= SL && SL != OrderStopLoss ()) SL = NormalizeDouble(avgprice - TrailingStop * Point, Digits); for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic) continue; if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype) if(OrderModify(OrderTicket(), OrderOpenPrice(), SL + TrailingStep, 0, 0, Yellow)) Print(" OrderModify Ok!"); else Print(" OrderModify Error!"); } } RefreshRates(); }