Download MetaTrader 5
To add comments, please log in or register
Do you have Linux? Find out how to install MetaTrader on it!
Tempestshade David
69
Tempestshade David 2014.04.06 20:43 

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);
                        }
                   }
          }
      }
   }
Keith Watford
Moderator
7906
Keith Watford 2014.04.06 21:13  
double distance = Bid - OrderOpenPrice();    //How will this work out for Sells?
               if (distance >= TrailingStopTrigger * Point)
               {
               }
Tempestshade David
69
Tempestshade David 2014.04.06 21:16  

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);
                        }
         }
      }
   }
Keith Watford
Moderator
7906
Keith Watford 2014.04.06 22:07  

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 ) )
Alain Verleyen
Moderator
28556
Alain Verleyen 2014.04.06 22:37  
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)
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.04.07 00:09  
if (TrailingStopEnabled && OrdersTotal() > 0)
Use it if you don't need explicit bool declaration.
/
To add comments, please log in or register