Trailing Stop

 

Hi guys then I'm trying to do the function of this This is the code But I did not understand why I do not change the order however in the function "TranlingStop " enters it (in fact I see the comment "tS") but it is not however does not perform the "orderModify" and I do not understand why someone can help me

\void TrailingStop(double e_trailingstop,double e_trailingstep){
        Comment("ts");

      for (int i= OrdersTotal()-1; i >=0; i --){
         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))continue;
            if(OrderSymbol()==Symbol()&& OrderMagicNumber()== nMagic)
            {
              Print("SELECT ORDER ERROR # ", GetLastError());
               return;
            }
            if (OrderType() == OP_BUY)
               if ((Bid - OrderOpenPrice()) > (e_trailingstop * Point))
                  if (OrderStopLoss() < Bid - (e_trailingstop + e_trailingstep - 1) * Point) {
                     if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask - e_trailingstop * Point, OrderTakeProfit(), 0, clrGreen))
                     return;
                  }
            if (OrderType()==OP_SELL)
               if ((OrderOpenPrice() - Ask) > (e_trailingstop * Point))
                  if (OrderStopLoss() > Ask + (e_trailingstop + e_trailingstep - 1) * Point || OrderStopLoss()==0) {
                     if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask + e_trailingstop * Point, OrderTakeProfit(), 0, clrGreen))
                     return;
                  }
       }
}
 

I will gift you this code

try it

void TrailStop()
{
   if(UseTStop)
   {
      RefreshRates();
      
      int    Res     = 0;
      double SLPrice = 0;
      double NewSL   = 0;
      double Range   = 0;
      int    digit   = 0;
   
      for(int cnt=0; cnt<OrdersTotal(); cnt++)
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
           {
            while(IsTradeContextBusy()) Sleep(100);
            if(OrderType()==OP_BUY && 
               OrderMagicNumber()==MagicNumber)
              {
               Range = MarketInfo(OrderSymbol(),MODE_BID) - OrderOpenPrice();
               NewSL = MarketInfo(OrderSymbol(),MODE_BID) - TrailingStop*MyPoint;
               NewSL = NormalizeDouble(NewSL,Digits);
   
               if(Range>=TrailingStart*MyPoint)
                  if(NewSL-OrderStopLoss()>=TrailingStep*MyPoint)
                    {
                     Res=OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),0,clrNONE);
                     
                     //---
                     if(Res)
                     Print("Order Stop Loss Has Been Modified Successfully.");
                     else
                     Print("Error With Trailing Stop: ", ErrorDescription(GetLastError()));
                    }
              }
   
            if(OrderType()==OP_SELL && 
               OrderMagicNumber()==MagicNumber)
              {
               Range = OrderOpenPrice() - MarketInfo(OrderSymbol(),MODE_ASK);
               NewSL = MarketInfo(OrderSymbol(),MODE_ASK) + TrailingStop*MyPoint;
               NewSL = NormalizeDouble(NewSL,Digits );
   
               if(Range>=TrailingStart*MyPoint)
                  if(OrderStopLoss()-NewSL>=TrailingStep*MyPoint || OrderStopLoss()==0)
                    {
                     Res=OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),0,clrNONE);
                     
                     //---
                     if(Res)
                     Print("Order Stop Loss Has Been Modified Successfully.");
                     else
                     Print("Error With Trailing Stop: ", ErrorDescription(GetLastError()));
                    }
              }
           }
   }
}
 
texoro: ) but it is not however does not perform and I do not understand why someone can help me
      for (int i= OrdersTotal()-1; i >=0; i --){
         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))continue;
  1. If you had used the debugger or printed out your variables, you would know why.
  2. If you selected an order you continue.
 
void TrailingStop(double e_trailingstop,double e_trailingstep)
{
        Comment("ts");

      for (int i=OrdersTotal()-1; i>=0; i--)
      {
         if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))continue;
         if(OrderSymbol()!=Symbol())continue;
         if(OrderMagicNumber()!= nMagic)continue;

            if (OrderType() == OP_BUY)
               if ((Bid - OrderOpenPrice()) > (e_trailingstop * Point))
                  if (OrderStopLoss() < Bid - (e_trailingstop + e_trailingstep - 1) * Point) {
                     if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask - e_trailingstop * Point, OrderTakeProfit(), 0, clrGreen))
                        continue;
                  }
            if (OrderType()==OP_SELL)
               if ((OrderOpenPrice() - Ask) > (e_trailingstop * Point))
                  if (OrderStopLoss() > Ask + (e_trailingstop + e_trailingstep - 1) * Point || OrderStopLoss()==0) {
                     if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask + e_trailingstop * Point, OrderTakeProfit(), 0, clrGreen))
                        continue;
                  }
       }
}
 
Konstantin Nikitin:

When using Bid/Ask in loop sending orders (OrderSend, OrderModify, OrderClose, etc...), you NEED to use RefreshRates().

There is a typo on the statement for a BUY :

if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask Bid- e_trailingstop * Point, OrderTakeProfit(), 0, clrGreen))
Reason: