Stop n bar exit price

Hi all, I am trying to set the stoptypenbar to be 17 bars from entry and try to exit on the 17th bar's close price. However, base on the following code's generated result, I found the program always exit the position on 17th bars' high price, which I am not sure where did I write wrongly. Could anyone point me to that error? I suspect it is related to the coverprice variable but I am not sure how it might have caused the problem.

//Backtesting Only Code will not plot any signals
_SECTION_BEGIN("Simple RSI Momentum Long Only Strategy");

//Trading Logic
Short = RSI(5) < 40 and Ref(RSI(5),-1) > 40;
SL_above_previous_high = Ref(High,-1) * 1.02;
Cover = H > SL_above_previous_high;
CoverPrice = Max(0, Ref(High,-1)*1.02);

ApplyStop(stopTypeLoss,	stopModePercent, Optimize( "max. loss stop level", 3, 2, 30, 1 ), 1);
ApplyStop( stopTypeNBar, stopModeBars,Optimize( "num of days to stop",17,5,30,1 ),1);

//ApplyStop( stopTypeTrailing, stopModePercent, SL_above_previous_high ,1);

ApplyStop( stopTypeProfit, stopModePercent, Optimize("profit stop",20,1,30,1),1);


SetStopPrecedence( stopTypeNBar, stopTypeTrailing, stopTypeLoss, stopTypeProfit );

//Position Size
//PositionSize = MarginDeposit = 1;



Plot( Close,"Price",colorBlack,styleBar);

Plot(SL_above_previous_high , "trailing stop line", colorRed );

Please use code tags if inserting code. It is mandatory requirement.

Take a look at your CoverPrice. There you have previous high defined.

So change to

CoverPrice = IIf(Cover==1, Max(O, Ref(High,-1)*1.02), C);

Thanks for reminding, will do next time.

For the code part, as Max(O, Ref(High,-1)*1.02) ,for me, means taking the maximum value of either 0 or (yesterday high * 1.02), but not the maximum value of either 0 or today's high... I am not sure why it took today's high price as exit...

Your codes work like charm.

Your interpretation of Max(O, Ref(High,-1)*1.02) is correct. So if yesterday's High * 1.02 is greater than today's high, and AmiBroker automatically limits you to the day's actual trading range, then today's exit price will be today's high.

You had zero but not Open in your first post:

I corrected it to use Open in 2nd post.

But yes, as said by default prices are bound to current bar's H-L range.
Your n-bar Applystop does not have a check for previous High being within that H-L range as in your default Cover rule. That's why current bar's high was used as Coverprice for n-bar stop.
(As aside, bar range limit can be disabled via SetOption field PriceBoundChecking and value False. But just being a side note.)

Precisely. @jackyjacky135 - that is why CODE TAGS are so important.
With code tags the mistake is obvious:

CoverPrice = Max(0, Ref(High,-1)*1.02); // wrong


CoverPrice = Max(O, Ref(High,-1)*1.02); // letter O not 0
1 Like

Thanks mradtke for clarifying. I noticed I made a mistake of setting "O" as 0 and that's should be the problem to my case.

Thank you fxshrat I understood where I went wrong. Can I also assume that, let say if I have applied all four stop loss mechanisms (hard stop loss, trailing stop loss, n bar stop loss and profit taking), they will all override any setting in my analysis settings?

You got noticed.

Mentioned in manual already:

AFL Function Reference - APPLYSTOP

FUNCTION controls built-in stops from the formula level (allows optimization of stops)

And if you want to make sure that all inbuilt stops are disabled and just single or multiple line AFL stops being used:

1 Like

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