void TralingStopLoss(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) continue; if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype) { price += OrderOpenPrice() * OrderLots(); order_lots += OrderLots(); } } } avgprice = NormalizeDouble(price/ order_lots + TrailingStop*Point, Digits); if(!avgprice ) return; 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(OrderType()==OP_BUY) { if(NormalizeDouble(Bid-OrderOpenPrice(),Digits)>NormalizeDouble(TrailingStop*Point,Digits)) { if(NormalizeDouble(OrderStopLoss(),Digits)NormalizeDouble(TrailingStop*Point,Digits)) { if(NormalizeDouble(OrderStopLoss(),Digits)>NormalizeDouble(Ask+(TrailingStop+TrailingStep-1)*Point,Digits)) if(OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Ask+TrailingStop*Point,Digits), OrderTakeProfit(), 0, Yellow)) Print("OrderModify Error ", GetLastError()); else Print("OrderModify Ok "); } } } } } RefreshRates(); }