Stop Conditions not met - stopout below the lowest low of 5 bars before entry bar

I’m playing around with a system idea and am trying to use a fairly tight stop so that my stop is a close below the low of the lowest 5 bars preceeding the entry bar…

Buy = Whatever Buy conditions;

// Stopout below the lowest low of 5 bars before entry bar
Condition = Buy == true;
BarsAgo = barssince(Condition) + 5;
Stopout = LLV(Low, BarsAgo);

StopCondition = Close < Stopout;

Sell = Cross(Stopout, Close) OR StopCondition; // neither of these seem to work in my code

if(Status("Action") == actionExplore)
{
Filter = 1;
AddColumn( Condition, "Condition Value = Buy", 1.2, colorDefault, IIf(Condition, colorGreen, colordefault )) ;
AddColumn( BarsAgo, "Bars Ago Value" ) ;
AddColumn( Ref(BuyPrice, 1), "Buy Price" );
AddColumn( Stopout, "Stopout" ) ;
AddColumn( Cross(Stopout, Close), "Stopout True/False" ) ;
AddColumn( StopCondition, "StopCondition True/False" );

Neither of the stop conditions seem to work in my code even though an exploration shows that it is picking up the correct Stopout value. However, the Sell condition is never being met.

Any ideas?

Thanks,

Dave

For proper stops you have to use ApplyStop or have to create looping code.

https://www.amibroker.com/kb/index.php?s=Applystop
https://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/

For example... If in your case of your code you have a Buy condition such as this one.

Buy = C > MA(C, 10);

Then you will get state signal with several Buy being true one after another. But as stop starting point you want to fix the initial Buy only. Codes such as yours won't work properly.


As aside in addition your would have to change to

Stopout = Ref(LLV(Low, BarsAgo),-1);

since LLV includes "current" bar. So e.g Close can never be lower than Low if "current" bar's one is included.

2 Likes

@fxshrat thank you very much, it didn't look obvious to me at 3 in the morning!! :slight_smile: very helpful. The use of Ref shifting one bar ago seemed to work and I'm also looking at applystop which I haven't used in awhile. Marcel brought this to my attention as well... It's these nuances that seem to catch me and cause hours of head scratching... time that I'll never get back but hopefully will ingrain the memory not to make the same mistakes.