This forum is in read-only mode now. You may discuss your questions on forums of MQL4.community and MQL5.community

Trailing stops not working with OP_SELLSTOP orders


Back to topics list
avatar
129
Vooch 2005.07.07 08:48 
It's taken me 2 days, but I finally figured out why my trailing stops are not working.

Trailing stops are not working with stop orders.

Trailing stops work with OP_BUY and OP_SELL, but they do NOT work with OP_SELLSTOP (and probably OP_BUYSTOP).



            // Adjust Trailing Stop on SELL order
            if( (TrailingStop>0) ) {
                if ( (OrderType()==1) || (OrderType()==3) || (OrderType()==5) ) { 

/////////////////////
//  THIS IS THE LINE OF CODE THAT DOES NOT WORK:
                    if( OrderOpenPrice()-Ask>TrailingStop*Point ) {
/////////////////////

                        if(OrderStopLoss()>(Ask+(Point*TrailingStop)) || (OrderStopLoss()==0) ) {
                                order_open = OrderOpenPrice();
                                order_stoploss = Ask+(Point*TrailingStop);
                                order_takeprofit = OrderTakeProfit();
                                OrderModify(OrderTicket(),order_open,order_stoploss,order_takeprofit,0,Orange);
                                Print(ScriptName," ADJUSTING TRAILING STOP sOrderModify:#",OrderTicket()," Ask=",Ask," OrderOpenPrice=",OrderOpenPrice()," StopLoss= from ",OrderStopLoss()," to ",Ask+(Point*TrailingStop)," TakeProfit=",OrderTakeProfit());
                        }
                    }
                }
            } 


avatar
129
Vooch 2005.07.07 09:03 
Trying it the alternate way doesn't work either:

            // Adjust Trailing Stop on SELL order
            if( (TrailingStop>0) ) {
                if ( (OrderType()==1) || (OrderType()==3) || (OrderType()==5) ) { 


a=OrderOpenPrice()-Ask;
b=TrailingStop*Point;
                    if( a>b ) {


                        if(OrderStopLoss()>(Ask+(Point*TrailingStop)) || (OrderStopLoss()==0) ) {
                                order_open = OrderOpenPrice();
                                order_stoploss = Ask+(Point*TrailingStop);
                                order_takeprofit = OrderTakeProfit();
                                OrderModify(OrderTicket(),order_open,order_stoploss,order_takeprofit,0,Orange);
                                Print(ScriptName," ADJUSTING TRAILING STOP sOrderModify:#",OrderTicket()," Ask=",Ask," OrderOpenPrice=",OrderOpenPrice()," StopLoss= from ",OrderStopLoss()," to ",Ask+(Point*TrailingStop)," TakeProfit=",OrderTakeProfit());
                        }
                    }
                }
            } 

avatar
129
Vooch 2005.07.07 09:13 
Someone should also check OP_SELLLIMIT and OP_BUYLIMIT while they're at it.

avatar
80
tonyc2a 2005.07.07 09:14 
When a stop order is actually entered, meaning it becomes a live trade instead of just an order waiting to be entered, it changes type to OP_BUY OR OP_SELL respectively.

You can still modify stop and limit orders using OrderModify() as you do with live trades.

Looking at you code it seems that you are missing some extra logic that is required. You may have simply left this out for simplicity. But if not, take a look at the following code. It is a complete trailing stop that works. Just insert it in your code. Of course, you need to declare and intialize the variable 'TrailingStop'

   for (int i = 0; i < OrdersTotal(); i++){
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      //--- LONG TRADES
      if(OrderType() == OP_BUY && OrderSymbol() == Symbol()){
         if (Bid - OrderOpenPrice() >= TrailingStop * Point){
            if (OrderStopLoss() < Bid - TrailingStop * Point || OrderStopLoss() == 0){
               OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * Point, OrderTakeProfit(), Red);
            }
         }
      } 
      //---SHORT TRADES
      if(OrderType() == OP_SELL && OrderSymbol() == Symbol()){
         if(OrderOpenPrice() - Ask >= TrailingStop * Point){
            if((OrderStopLoss() > Ask + TrailingStop * Point) || OrderStopLoss() == 0){
               OrderModify(OrderTicket(), OrderOpenPrice(),Ask + TrailingStop * Point, OrderTakeProfit(), Red);
            }
         }
      }
   }

avatar
129
Vooch 2005.07.07 09:20 
tonyc2a,

I left it out for simplicity. The reason I did that is to focus on what is the problem exactly.

My guess is that Stop Order handling is not tied to OrderOpenPrice() properly, but only the developers would know for certain.

The error for trailing stops not working with OP_SELLSTOP orders comes down to one line of code:

if( OrderOpenPrice()-Ask>TrailingStop*Point ) {

It took me 2 days to figure this out.
avatar
80
tonyc2a 2005.07.07 09:24 
Oh ok, I see what your saying.
avatar
129
Vooch 2005.07.07 19:35 
Slawa or Lenar,

Are you aware of this issue?

Thanks,
Vooch
avatar
4464
Slawa 2005.07.08 15:00 
before tell about open price, trailing and ask please. i suspect that your condition
if( OrderOpenPrice()-Ask>TrailingStop*Point )
is not correct for SELL_STOP
Back to topics list  
Download MetaTrader 5 (450 Kb, web installer) — a new terminal for financial markets