I cannot seem to set a Trailing Stop for my Sell Positions

 

So ive been trying to code a trailing stop after some profit for my sell positions, Ive been able to successfully code this for my buy positions, but for some reason the trailing stop for my Sell's also move opposite profit and dont just follow the profit


Here's the code:

for(int s=OrdersTotal()-1; s>=0; s--)
     {
      //--- Select open order by position s relative to the counter
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
         //--- Check if order belongs to the current chart
         if(OrderSymbol()==Symbol())
            //--- If its a sell order
            if(OrderType()==OP_SELL)
              {
               //--- If the stop loss has passed profit
               if(OrderStopLoss()< OrderOpenPrice()-35*_Point)
                 //--- If the current Ask has gone beyond our desired tSL 
                  if(Ask < OrderStopLoss()-35*_Point)
                     OrderModify(
                        OrderTicket(),
                        OrderOpenPrice(),
                        Ask+35*_Point,
                        OrderTakeProfit(),
                        0,
                        clrGray
                     );
              }
     }
 
if(OrderStopLoss()< OrderOpenPrice()-35*_Point)

Why would the SL be below the open price unless you have already moved it?


Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

 
This condition :
Is always false… your initial StopLoss will be higher then OrderOpenPrice… this supposed to be your trigger for trailing … Trigger should involve Ask/Bid … like… if( OrderOpenPrice() - Ask > InpTrailStart) … next of your code 
 if(OrderStopLoss()< OrderOpenPrice()-35*_Point)
 
Daniel Cioca #:
This condition :
Is always false… your initial StopLoss will be higher then OrderOpenPrice… this supposed to be your trigger for trailing … Trigger should involve Ask/Bid … like… if( OrderOpenPrice() - Ask > InpTrailStart) … next of your code 

Okay so I switched it back to your suggestion and now it doesnt execute the SL at all

for(int s=OrdersTotal()-1; s>=0; s--)
     {
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_SELL)
               if(OrderOpenPrice()-Ask > 50*_Point)
                  if(Ask < OrderStopLoss()-35*_Point)
                     OrderModify(
                        OrderTicket(),
                        OrderOpenPrice(),
                        Ask+35*_Point,
                        OrderTakeProfit(),
                        0,
                        clrGray
                     );
              }
 
David Maza #:

Okay so I switched it back to your suggestion and now it doesnt execute the SL at all

for(int s=OrdersTotal()-1; s>=0; s--)
     {
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_SELL)
               if(OrderOpenPrice()-Ask > 500*_Point)
       {
                  bool result =   OrderModify(
                        OrderTicket(),
                        OrderOpenPrice(),
                        Ask+350*_Point,
                        OrderTakeProfit(),
                        0,
                        clrGray
                     ); }
              }

 
David Maza #:

Okay so I switched it back to your suggestion and now it doesnt execute the SL at all

for(int s=OrdersTotal()-1; s>=0; s--)
     {
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_SELL)
               if(OrderOpenPrice()-Ask > 50*_Point)
                  if(Ask < OrderStopLoss()-35*_Point)
                     OrderModify(
                        OrderTicket(),
                        OrderOpenPrice(),
                        Ask+35*_Point,
                        OrderTakeProfit(),
                        0,
                        clrGray
                     );
              }

Your code misses one very important check which may or may not be relevant and that is if the initial order is opened without a stoploss.

if(Ask < OrderStopLoss()-35*_Point || OrderStopLoss()==0)
{
}


Daniel Cioca #:

for(int s=OrdersTotal()-1; s>=0; s--)
     {
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_SELL)
               if(OrderOpenPrice()-Ask > 500*_Point)
       {
                  bool result =   OrderModify(
                        OrderTicket(),
                        OrderOpenPrice(),
                        Ask+350*_Point,
                        OrderTakeProfit(),
                        0,
                        clrGray
                     ); }
              }

Your code is not really a trailing stop as it will only ever close the trade at 150 points profit.

The trade may go 10,000 points in profit but if the Ask rises, The stoploss will rise again.

You should always check the the new SL is better than the existing SL.

 
Keith Watford #:

Your code misses one very important check which may or may not be relevant and that is if the initial order is opened without a stoploss.


No… it will start to trail at 500 point in profit … and it will stay behind  350 points… trail… off course… this is a minim code… many other things should be checked 
 
Keith Watford #:

Your code misses one very important check which may or may not be relevant and that is if the initial order is opened without a stoploss.


Daniel Cioca #:
No… it will start to trail at 500 point in profit … and it will stay behind  350 points… trail… off course… this is a minim code… many other things should be checked 

Why have you quoted and responded to my comment about David Maza's post? It had nothing to do with your code.

 
Keith Watford #:

Why have you quoted and responded to my comment about David Maza's post? It had nothing to do with your code.

Sorry… 😊
 
Keith Watford #:

Your code misses one very important check which may or may not be relevant and that is if the initial order is opened without a stoploss.

if(Ask < OrderStopLoss()-35*_Point || OrderStopLoss()==0)
{
}

Okay so I tried that out and im still getting no trailing stop. Maybe I'm testing wrong? 
Here's how im testing it to see if the sell trailing stop goes through

void OnTick()
  {
   if(OrdersTotal()==0)
     {
      int buyticket  =  OrderSend(Symbol(),OP_SELL,0.10,Bid,3,0,0,NULL,0,0,clrGreen);
     }
   if(OrdersTotal()>0)
     {
      for(int s=OrdersTotal()-1; s>=0; s--)
        {
         //--- Select open order by position b relative to the previous counter
         if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
            //--- Check if order beloings to the current chart
            if(OrderSymbol()==Symbol())
               //--- If its a buy order
               if(OrderType()==OP_SELL)
                 {
                  //--- If the stop loss is above Open
                  if((OrderOpenPrice()-Ask) > 50*_Point || OrderStopLoss()==0)
                     if(Ask < (OrderOpenPrice()-50*_Point))
                        OrderModify(
                           OrderTicket(),
                           OrderOpenPrice(),
                           Ask+35*_Point,
                           OrderTakeProfit(),
                           0,
                           clrGray
                        );
                 }
        }
     }
  }
 
David Maza #:

Okay so I tried that out and im still getting no trailing stop. Maybe I'm testing wrong? 
Here's how im testing it to see if the sell trailing stop goes through

Read my post properly

Reason: