Combination of 2 stops: 1. Safety stop loss 2. Trailing (profit) stop

Hi all,
I would like to backtest if it makes sense to add to a normal stop-loss another trailing-stop (trailing 5% below the close). However, I want the 2nd stop to be activated ONLY if a certain condition is met (eg, if Close today is 20% higher vs the close of 10 bars ago activate the 5% trailing-stop, if the condition is not met do nothing).

I do have the code for the first stop loss as well as its plotting (see below) but I am just not getting it right for the 2nd conditional stop. Its conditions would be:

  • Activate only if closing price today is 10% above the close 10 bars ago
  • If activated start trailing 5% below the close
  • If above condition is not met do not do anything

The idea is to test if the system can benefit better from rapid / sudden price rises.

1000 thanks for your help and feedback,



InTrade = Flip(Buy,Sell);
Initialstopline = IIf(InTrade,valuewhen(Ref(Buy,-1),O-5*Ref(ATR(13),-1),1),Null);

PlotOHLC(open,high,low,close,Name(),colorBlueGrey,style=styleCandle,Null,null,XShift =0,ZOrder=0,width=1);
Plot(Initialstopline,"Initial stop line",colorRed,styleDots,0,0,0,0,1);
amount = IIf(C>1.1*Ref(C,-10), 5, 99);
ApplyStop(stopTypeTrailing, stopModePercent, amount, ExitAtStop, Volatile=True);

But AFAIK, trailing stop mode of ApplyStop uses High price.
So what you could try is this one

amount = IIf(C>1.1*Ref(C,-10), 5, 99);
H = High = Close;
ApplyStop(stopTypeTrailing, stopModePercent, amount, ExitAtStop, Volatile=True);
RestorePriceArrays();//restore original High array

Or alternatively create loop code.


Thanks fxshrat. What do you mean with create loop code?

@nicolauserdmann I believe he is referring to the type of stop code in this Official Knowledge Base article. You would need to make a few modifications to match your specific needs.

If you search this forum you will find several other examples of using loops for trailing stop codes.