Wait n bars after stop

Maybe there is a lot of examples but i could not find..i m newbie
and my aim is wait 12 bars after long and short signal ...like picture
i try to explain on code area
Buy to Short signal


BuyPrice = O;
ShortPrice = C;

MA1 = MA(C,5);
MA2 = MA(C,25);

LongSig = Cross(MA1,MA2)
ShortSig= Cross(MA2,MA1);



iif(Buy AND LongStop); // ...Stop and wait 12 bars
iif(Short AND ShortStop);....// Stop and wait 12 bars
//after 12 bars continue with active signal (Buy or short)

Hi @saruhan, and Welcome to the Forum.

Please search for "Verified" or "Verified Badge" and follow the steps to get Verified. Only Verified users are allowed to post questions.

As a Newbie, please also search and read about "How to use this site" and "How to ask a good Question".

You have done a good job posting your code, so let us know when you are verified, and then one of the many very helpful forum members is likely to give you some pointers.

1 Like

Looks like your long and short stop variables should be using the CROSS function and then you can pass that result to the BarsSince() function.

1 Like


Glad to see you have Verified.

You might be able to use the n-bar Stop in the Apply Stop https://www.amibroker.com/guide/afl/applystop.html.
You might be able to use BarsSince http://www.amibroker.com/guide/afl/barssince.html

1 Like

The simplest way to do what you want is:

MA1 = MA(C,5);
MA2 = MA(C,25);
BuySetup = Cross( MA1, MA2 );
Buy = BarsSince( BuySetup ) >= 12 AND RSI(7) > 70;

I leave coding for short side to your own excercise.


Thanks for the reply Tomasz

i wanted long and short trade in this code
after every exit signal wait 25 bars
if buy or short signal still same after 25 bars ,enter again exuticon

i did this code...i hope it is correct..(?!) and apologies for this simple and probably redundant question.amibrokeerxx

function PercentR( periods )
 return -100 * ( HHV( H, periods ) - C )/( HHV( H, periods ) - LLV( L, periods ) ); 

WR=PercentR( Param("Periods", 14, 2, 100 ));
MA1 = MA(C,5);
MA2 = MA(C,25);
LongSig = MA1>Ref(MA2,-1);

Buy_savedbar = Valuewhen( LongSig, C, 1 );
Short_savedbar = Valuewhen( ShortSig, C, 1);
Cover= Buy_savedbar AND  WR>=0.00  ; 
Sell= Short_savedbar AND  WR<=-100;
Buy = BarsSince( Cover) >= 12 AND MA1>Ref(MA2,-1);
Short = BarsSince( Sell) >= 12 AND MA2>Ref(MA1,-1);

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