How to make a condition close position.

Hi there, I am a newbie and try to make a code but i got stuck so please help. My idea is that I will close my current position with my condition as closebuy / closeshort. But to prevent the price suddenly pull back I want to add some conditions such as the very first 2 bar form buy or short only give exit signal if price goes lower / higher than lowest / highest within 10 bars.

I tried to make the code as below but did not work (closebuy and closeshort actually worked) :

Sell= IIf(BarsSince(Buy)>2, Closebuy, C < LLV (L,10)) ;
Cover= IIf(BarsSince(Short)>2,closeshort, C> HHV (H,10) ;

Sorry for my bad english, hope you understand :smiley:

Which AmiBroker version do you have installed?

I am using Amibroker 6.0xx

First your code does NOT "add a condition". It replaces the condition with something else.
First step is to understand your own code, see:

Also if you have repeated Buy signals it won't work because BarsSince would got reset each time Buy signal occurs. So you either would need to eliminate duplicates or you should be using ApplyStop for stops.
It is suggested to use recent version 6.20 or higher that has "ValidFrom" , "ValidTo" parameters in ApplyStop function that allows stops to be activated after certain number of bars since entry.

Can you explain what you want to do, because this would never seem to evaluate to true.

After a buy, I want to set 2 exit condition that within next 2 bar if price closes below the lowest of 10 recent bars and from the third bar will be another condition. Can you suggest any method or what should I read to deal with it.

The best answer is already there in Post #4, nothing to add.

Just to help with understanding how to use AFL, when you want to check if the current bar Close is less than the previous 10 recent bars, you need to write it like this.

condition1 = C < Ref( LLV( L, 10), -1);

read here