Applystop works fine, was: does not exactly do what it supposed to do

I wrote the following code and tried to backtest the strategy. However, as I'm checking the result, Applystop does not exactly what it is supposed to do. All of the positions closed as "long trail" exited at bar #2, while I expected that if RSI(2) can not cross 50, then stop must be triggered after 5 bars. The setting is also reflected my requirement. Can kindly someone help me to understand why?

 
//PosQty = Optimize( "PosQty", 1, 1, 3, 1 ); 
 
PosQty=5;  
 
SetOption ( "maxopenpositions", PosQty ); 
SetPositionSize( 100 / PosQty, spsPercentOfEquity ); 
 
SellPrice = close; 
 
PositionScore = ROC(Close,100); 
  
BuySignal = Close > EMA(close,100) AND close <= LLV(low, 10);  
 
// buy on the next bar 
Buy = Ref( BuySignal, -1); 
BuyLimitPrice = Ref( low, -1) * 0.97;  //// buy 3% under low of the signal bar 
 
// now we check if limit was hit 
Buy = Buy AND L < BuyLimitPrice; 
 
// if Open price is below the limit, then we use Open for entry 
BuyPrice = Min( Open, BuyLimitPrice ); 
 
//sell conditions 
 
Sell = RSI(2) > 50; 
ApplyStop( stopTypeNBar, stopModeBars, 5 ); // exit after 5 days no matter what happens. 

example
nbar

You shouldn’t use such Title! A more appropriate and careful Title would be “ApplyStop query”.

You have not shown all your 'Stops Settings'. Is there other stop settings active?

This code snippet will clear all current stop settings.

// Disable all UI stops
for (i = 0; i <= 3; i++) ApplyStop(i, 0, 0, 0);

See (use 'Detailed Log')

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.