My Trailing Stop Won`t Work

 

Hello everyone,

Have been trying to develop a trailing stop and it does not seem to want to work with SELLS. It adds a stop loss, but does not trail it afterwards.

Appreciate the help.

  if (TrailingStopEnabled = True && OrdersTotal() > 0)
   {  
      double pipvalue;
      for (int i=1; i<=OrdersTotal(); i++)
      {
         if(OrderSelect(i-1, SELECT_BY_POS)==true)
         {
            if(OrderType()==OP_BUY) pipvalue=(OrderClosePrice()-OrderOpenPrice())/Point;
            if(OrderType()==OP_SELL) pipvalue=(OrderOpenPrice()-OrderClosePrice())/Point;
            
            double distance = Bid - OrderOpenPrice();
               if (distance >= TrailingStopTrigger * Point)
                  {
                     double newStopLossPrice;
                     if(OrderType() == OP_BUY) newStopLossPrice = Bid - TrailingStopPips * Point;
                     if(OrderType()==OP_SELL) newStopLossPrice = Ask + TrailingStopPips * Point;
                     if(OrderStopLoss() == NULL || newStopLossPrice > OrderStopLoss() && OrderType() == OP_BUY)
                        {
                        OrderModify(OrderTicket(), OrderOpenPrice(), newStopLossPrice, NULL, 0, Red);
                        }
                     if (OrderStopLoss() == NULL || newStopLossPrice < OrderStopLoss() && OrderType() == OP_SELL)
                        {
                        OrderModify(OrderTicket(), OrderOpenPrice(), newStopLossPrice, NULL, 0, Red);
                        }
                   }
          }
      }
   }
 
double distance = Bid - OrderOpenPrice();    //How will this work out for Sells?
               if (distance >= TrailingStopTrigger * Point)
               {
               }
 

Hello,

I changed it to the below and it seems to work. Can someone please verify it to make sure it makes sense?

  if (TrailingStopEnabled = True && OrdersTotal() > 0)
      {  
      double pipvalue;
      for (int i=1; i<=OrdersTotal(); i++)
      {
         if(OrderSelect(i-1, SELECT_BY_POS)==true)
         {
                     double newStopLossPrice;
                     if(OrderType() == OP_BUY) newStopLossPrice = Bid - TrailingStopPips * Point;
                     if(OrderType()==OP_SELL) newStopLossPrice = Ask + TrailingStopPips * Point;
                     if(OrderStopLoss() == NULL || newStopLossPrice > OrderStopLoss() && OrderType() == OP_BUY)
                        {
                        OrderModify(OrderTicket(), OrderOpenPrice(), newStopLossPrice, NULL, 0, Red);
                        }
                     if (OrderStopLoss() == NULL || newStopLossPrice < OrderStopLoss() && OrderType() == OP_SELL)
                        {
                        OrderModify(OrderTicket(), OrderOpenPrice(), newStopLossPrice, NULL, 0, Red);
                        }
         }
      }
   }
 

I am not aware that NULL can be used to replace stoploss or Takeprofit, maybe it can. But 1 wouldn't use it.

Make sure that you are totally clear when mixing || and && in boolean tests

if(OrderStopLoss() == NULL || newStopLossPrice > OrderStopLoss() && OrderType() == OP_BUY)

//Do you mean
if( ( OrderStopLoss() == NULL || newStopLossPrice > OrderStopLoss() ) && OrderType() == OP_BUY)

//Or
if(OrderStopLoss() == NULL || ( newStopLossPrice > OrderStopLoss() && OrderType() == OP_BUY ) )
 
Tempestshade:

Hello,

I changed it to the below and it seems to work. Can someone please verify it to make sure it makes sense?

Also don't use this :

  if (TrailingStopEnabled = True && OrdersTotal() > 0)

but this :

  if (TrailingStopEnabled && OrdersTotal() > 0)

or eventually this :

  if (TrailingStopEnabled == True && OrdersTotal() > 0)
 
if (TrailingStopEnabled && OrdersTotal() > 0)
Use it if you don't need explicit bool declaration.
Reason: