Curious trailing stop problem

 

I have a strange problem with my trailing stop code, which somehow set's the stoploss higher than the actual price, which then leads to very small profits. I only use long positions for now.

I have a picture which describes it best:


at #45 the order is opened at 1.5622 with stoploss 1.5422 and t/p of 1.5702.

Already at #46 the order gets modified to 1.5622 with stoploss 1.5622 (!!) and t/p untouched! Finally, at #53 the order gets S/L'ed (huh?) with a minimal profit of 9,

because the S/L value somehow got minimally higher than the actual price.


I use the following code which I took from another EA, however I checked the code and can't see a mistake.


void TrailingPositions(int TrailingStop, int TrailingStep) {

  for (int i=0; i<OrdersTotal(); i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
        if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC ) {
          if (OrderType()==OP_BUY) {
            if ((Bid-OrderOpenPrice())>TrailingStop*Point && OrderStopLoss()<Bid-(TrailingStop)*Point) { 
             
   
                  ModifyStopLoss(Bid-TrailingStop*Point, clModifyBuy);
             
            }
          } else if (OrderType()==OP_SELL) {
            if (OrderOpenPrice()-Ask>TrailingStop*Point) {
            //  if (OrderStopLoss()>Ask+(TrailingStop+TrailingStep-1)*Point || OrderStopLoss()==0) {
               if (OrderStopLoss()>Ask+(TrailingStop)*Point || OrderStopLoss()==0) {
                  //RefreshRates();
                  ModifyStopLoss(Ask+TrailingStop*Point, clModifySell);
              }
            }
          }
       }
    }
  }
}

void ModifyStopLoss(double ldStop, color clModify) {

   bool   bres;
   bres=OrderModify(OrderTicket(), OrderOpenPrice(), ldStop, OrderTakeProfit(), 0, clModify);
   if (!bres) Print("Error closing order : ",ErrorDescription(GetLastError()));
}

Any help appreciated! :)
 
oracle1:

I have a strange problem with my trailing stop code, which somehow set's the stoploss higher than the actual price, which then leads to very small profits. I only use long positions for now.

[...]

I use the following code which I took from another EA, however I checked the code and can't see a mistake.

I'm not sure what you think the problem is. The repeated 1.5622 in the "Price" column isn't the current (bid) price. It's the entry price for the position.


It's perfectly normal for a trailing stop to go above the price at which the position was opened - that's usually the point of a trailing stop - and there is nothing in the code you're using to prevent that happening. From context, the bid price in this example looks to be rising to 1.5631+TrailingStop, moving the stop to 1.5631, and then retraces down to take out the trailing stop.

Reason: