Problem with trailing stop loss

 

Hi,

code below works but only for buy, for sell not activated. What is problem?


void AdjustTrail(double WhenToTrail, double TrailAmount) {
int ticket = 0;
  // Loop through orders.
   

  // Buy Order
  for (int b=OrdersTotal()-1; b>=0; b--) {
    if (OrderSelect(b, SELECT_BY_POS, MODE_TRADES)) {  
      if (OrderMagicNumber() == MagicNumber) { // This EA ownes it
        // Check the Symbol !!!!   
        if (OrderSymbol() == Symbol() && OrderType() == OP_BUY) {
          //                          Convert pips to money.
          if (Bid-OrderOpenPrice()  > WhenToTrail*_Point) {
            if (OrderStopLoss() < Bid-TrailAmount*_Point || 
            OrderStopLoss() == 0) {  // Check whether the trail
                                                           // has already been moved,
              ticket = OrderModify(
                OrderTicket(),                             // ticket
                OrderOpenPrice(),                          // price
                
                
                Bid-(TrailAmount*_Point),                    // stop loss
                OrderTakeProfit(),                         // take profit
                0,                                         // expiration
                CLR_NONE                                   // color
              );
                              if (ticket > 0){
                
              CreateTrailingStopStatusInfoComment("Activated!");
                          }
            } 
          }
        }
      }
    }
  }
  

  // Sell trade
  for (int s=OrdersTotal()-1; s>=0; s--) {
    if (OrderSelect(s, SELECT_BY_POS, MODE_TRADES)) {  
      if (OrderMagicNumber() == MagicNumber) {
        if (OrderSymbol() == Symbol() && OrderType() == OP_SELL) {
          if (OrderOpenPrice()-Ask  > WhenToTrail*_Point) { // How far it has fallen
            if (OrderStopLoss() > Ask+TrailAmount*_Point || 
                OrderStopLoss() == 0 ) { // If stop loss is not 
                                         // moved yet, move it, else ignore
                                         // Now, if somebody puts StopLoss 0
                                         // (not using stop loss)
                                         // sell side never trails,
                                         // stop loss never kicks in. 
                                         // (Buy side is ok)
                                         // Put that condition here. 
              ticket = OrderModify(
                OrderTicket(),
                OrderOpenPrice(),

                
                Ask+(TrailAmount*_Point),
                OrderTakeProfit(),
                0, CLR_NONE
                
              );
                              if (ticket > 0){
                
              CreateTrailingStopStatusInfoComment("Activated!");
                          }
            } 
          }
        }
      }
    }
  }
}
 
chris.dotan:

Hi,

code below works but only for buy, for sell not activated. What is problem?


The best place to start would be to print any possible errors. The OrderModify() function is a bool (true/false) so always print an error notification function should a bool function return the opposite of what you want it to return. 

I know it's not a definitive solution but its the best place to start in investigating it.  

if (!OrderModify(...)){

        PrintFormat("Your position failed to modify. (Error %d", GetLastError());

} else {

        CreateTrailingStopStatusInfoComment("Activated!");

}
 
              ticket = OrderModify(

OrderModify does not return a ticket number.

Reason: