Possible to have a trailing stop loss on losing trades?

 

Hi I have a trailing stop on my winning trades that works very well.  Is it possible to have a similar trailing stop on losing trades rather than a hard stop loss.  Can the stop loss be adjusted/ decreased as the price moves in my favor?  Here is the code for my trailing stop loss on winning trades:

//+------------------------------------------------------------------+
//| TrailingStop                                                     |
//+------------------------------------------------------------------+
void Trailing_Stop(){
   double Trail=Trailing*Pip,TSTP=TS_SL*Pip;
   for(int i=OrdersTotal()-1; i>=0; i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderSymbol()==Symbol()){               
            if(OrderType()==OP_BUY){
               if(Bid-OrderOpenPrice()>TSTP){
                  if(OrderStopLoss()<Bid-Trail){
                     if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Trail,OrderTakeProfit(),0,Orange))
                     RefreshRates();
                    }
                  }                
                }
            if(OrderType()==OP_SELL){
               if(OrderOpenPrice()-Ask>TSTP){
                  if(OrderStopLoss()>Ask+Trail){
                     if(OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Trail,OrderTakeProfit(),0,Orange))
                      RefreshRates();
                     }
                   }
                 }  
               }
             }
           }
         }

And the code that I have tried on losing trades, but it doesn't always work.  Only most of the time:

//+------------------------------------------------------------------+
//| TrailingStoploss                                                 |
//+------------------------------------------------------------------+
void Trailing_Stoploss(){
   double TSTP=StoplossStart*Pip,Trail=StoplossStop*Pip;
   for(int i=OrdersTotal()-1; i>=0; i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderSymbol()==Symbol()){               
            if(OrderType()==OP_BUY){
               if(OrderStopLoss()<Ask-TSTP-Trail){
                     if(OrderModify(OrderTicket(),OrderOpenPrice(),Ask-TSTP-Trail,OrderTakeProfit(),0,Orange))
                     RefreshRates();
                    }
                  }
            if(OrderType()==OP_SELL){
                  if(OrderStopLoss()>Bid+TSTP+Trail || OrderStopLoss()==0){
                     if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid+TSTP+Trail,OrderTakeProfit(),0,Orange))
                      RefreshRates();
                     }
                   }                
                 }
               }
             }  
           }
 
trader3000:I have a trailing stop on my winning trades that works very well.  Is it possible to have a similar trailing stop on losing trades rather than a hard stop loss
Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.
  1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
  3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
              Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19
  4. You must normalize lots properly and check against min and max.
  5. You must also check FreeMargin to avoid stop out

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 
Thank you for your quick reply, but you know the difference between a Take profit and a Trailing stop.  With a Take profit your gains are limited ...... with a Trailing stop they are not.  Is there a similar but opposite code for a stop loss.  With a hard stop loss your loss is set at maximum.  Can this be trailed so that the loss decrease as the price moves in your favor?  As per my question and code above.  The SL is is set at say -50 pips and then starts trailing.  The SL is set and the risk is not infinite
Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
 
The problem is that with a trailing stop on winning trades, the "if (Bid-OrderOpenPrice()>TSTP){" statement remains true most of the time so that the rest of the code can be executed.  With a trailing stoploss on losing trades, however, it only acts as a trigger, but usually does not remain true so the rest of the code cannot be executed.  Is there a way to set a trigger after this?   
 
trader3000:  With a hard stop loss your loss is set at maximum.  Can this be trailed so that the loss decrease as the price moves in your favor?  As per my question and code above.  The SL is is set at say -50 pips and then starts trailing.  The SL is set and the risk is not infinite

What you just said is the SL moves in the direction of the trade, lessening risk and finally locking in profits. Fine. If it makes no difference if the trade is loosing or winning, just remove any test for break even.

trader3000:Is it possible to have a similar trailing stop on losing trades rather than a hard stop loss.  

If there is no hard stop loss, then you are moving the SL away from price and you have infinite risk. It is possible but you don't want that.

Make up your mind what your question is; don't change it mid-post.

 
William Roeder:

If there is no hard stop loss, then you are moving the SL away from price and you have infinite risk. It is possible but you don't want that.

Thank you for your reply.  I actually mean the opposite.  In other words, I want the SL to move towards my OrderOpenPrice and thereby minimizing the loss.  So I will try and explain like this:

I have a trailingstop on my winning trades.  The SL is activated/ triggered after the price moves X pips (say 50 pips) in my favor and trails behind by say 10 pips locking in a minimum profit of 40 pips.  This part I have figured out and it's easy.  My question is, is it possible to have a similar trailingstop on my losing trades?  In other words, the Stoploss is activated/triggered after the price moves X pips (say 50 pips) against me and trails 10 pips behind locking in  a maximum loss of 60 pips.  But if the price turns and start moving in my favor to keep adjusting this SL (trailing it) so as to minimize the loss.  For example once the SL is triggered at 50 pips minus the 10 pip trail, but the price improves by 10 pips, the SL is then adjusted to a max loss of only 50 pips and so on.  This is as per the code I posted in post #1

 
trader3000: I actually mean the opposite.  In other words, I want the SL to move towards my OrderOpenPrice …

Answered in #4. What part of "just remove any test for break even." was unclear? Here is your originally posted code minus the BE test:

            if(OrderType()==OP_BUY){
//No BE        if(Bid-OrderOpenPrice()>TSTP){
                  if(OrderStopLoss()<Bid-Trail){
                     if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Trail,OrderTakeProfit(),0,Orange))
 

Thank you once again, but with this the price has no room to breath,  The SL is way to tight.  I would like it to first move 50 pips before the SL sequence of events are triggered,  So buy order moves against me.  When Bid is 50 pips away from OrderOpenPrice it triggers and sets a buystop like this:

//Glogal variables
extern int StoplossStart=50;
extern string Label_Trailingstop="Pips trailing behind price once activated";
extern int TrailingStop=2;

void
double TSTP=StoplossStart*Pip;
if(Bid-OrderOpenPrice()>TSTP){  //This allows the price to breathe and moves away by 50 pips before SL is locked in at 2 pips
   if(OrderStopLoss()<Bid-Trail){

If I remove this as suggested the SL starts trailing immediately.  Is there no way it can first breathe exactly as per my TrailingStop code at the top?  Thanks

 

You asked if it is possible. I've answered that 3+ times.

You just said you want it to "first move 50 pips" and move to BE. Then you don't want to trail a loosing trade.

Make up your mind what you want. Until you can express it in concrete terms it can't be coded.

 

Okay thanks, I will try and explain it line by line:

I place a Buy order, but the price turns against me.  When the price is 50 pips against me, I want the EA to set a Stoploss that is a further 10 pips away so there is room to breathe.  In other words:

OpenOrderPrice of buy order =x  Stoploss is triggered (OrderModify) at x -50 and set at x-50-10:

extern int Trailingstart=50;
extern int Trailing=10;
//---------------------
double TSTP=Trailingstart*Pip,Trail=Trailing*Pip;
for(int i=OrdersTotal()-1; i>=0; i--){
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
      if(OrderSymbol()==Symbol()){               
         if(OrderType()==OP_BUY){
            if(OrderOpenPrice()-Bid>TSTP){ //Stoploss is triggered when price reach -50 pips from OrderOpenPrice.
               if(OrderStopLoss()<Bid-Trail){ //Stoploss is set at Bid -10 pips
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Trail,OrderTakeProfit(),0,Orange))
The hope is now that during the breathing space of 10 pips that the price will turn in my favor and the stoploss will be adjusted upwards with every tick.  In other words if Bid price goes up, so does the trailing stoploss until, hopefully, it might break even or better yet, show a profit.  My biggest problem is that if(OrderOpenPrice()-Bid>TSTP){ cannot remain true if the price starts to go up again.  This is merely suppose to act as a trigger.  How could I do this?




 
trader3000: , I want the EA to set a Stoploss that is a further 10 pips away so there is room to breathe.

Now you've changed back to no hard stop loss (#0, #2 #9) Infinite risk.

Normal SL (#2, #3, #5, #7)

Make up your mind

You can't live on the East coast and the West coast at the same time. Your title is "is it possible," NO!
Reason: