Re-entry delay in very simple formula

hi,

I have simple formula, not portfolio, only one symbol, here it is:

Buy =
(
C > TEMA ( C , 12 )
AND C > TEMA ( C , 26 )
)
;

Sell =
(
C < TEMA ( C , 12 )  
AND C < TEMA ( C , 26 ) 
)
;
Short = 0;
Cover = 0;

ticket is weekly (1 bar = 1 week)

How can I make a code, when I have a 2 weeks (2 bars) delay from last SELL signal to new BUY signal?

I dont want to buy, when the last bar was "SELL". I want to have at least 2 bars delay between SELL and new BUY signal.

Thanks and hope i get this right :slight_smile:

You can add this line at the end of your code:

Buy = Buy AND BarsSince( Sell ) > 2;
2 Likes

Can i use:

Buy = Buy AND BarsSince( Sell ) >= 2;

Does it mean, the trade will make "BUY" when there is 2 bars delay or more?

Yes, sure. It can be >=

Just one more thing, regarding this bar delay.

why function:

SetOption ("HoldMinBars", 6);

gives different results than:

Sell = Sell AND BarsSince (Buy) >= 6

Should be the same, both say that i have to wait minimum 6 bars after BUY to trigger SELL signal...

Why am i giving the different results in function backtesting?

of course I have:

Short = 0;
Cover = 0;

Refers to initial Buy.

So this

SetOption("HoldMinBars", 6);

m = MA( Close, 20); 
Buy = Close > m; 

Sell = 1;
Short = Cover = 0;

Would be like this

m = MA( Close, 20); 
Buy = Close > m; 

Sell = 0;
bars = 6;
ApplyStop( stopTypeNBar, stopModeBars, bars, True );

Short = Cover = 0;

Also HoldMinbars disables exit.

AFL Function Reference - SETOPTION

  • HoldMinBars - Number - if set to value > 0 - it disables exit during user-specified number of bars even if signals/stops are generated during that period

May refer to repeated Buy (state signals).

The following code does mimic HoldMinBars.
(NOTE: It is not required! Use SetOption("HoldMinBars", bars). Just for demonstration! )

procedure HoldMinBars(BuySig, SellSig, nbars) {
	global Buy, Sell;
	// !!! NOT REQUIRED !!!
	// !!! JUST TO SHOW HOW TO MIMIC SetOption("HoldMinBars", bars) !!!	
	// https://forum.amibroker.com/t/re-entry-delay-in-very-simple-formula/29823/8
	for( i = 0, Buy = 0, Sell = 0, entrybar = 0; i < BarCount; i++ ) {			
		if( BuySig[ i ] AND entrybar == 0 ) {
			Buy[ i ] = 1;
			entrybar = i;
		} 
		SellSignal = SellSig[ i ] AND i >= entrybar + nbars;	
		if( SellSignal AND entrybar > 0 ) {
			Sell[ i ] = 1;    
			entrybar = 0;
		}	 
	} 
}

m = MA( Close, 20 ); 
Buy = Close > m; 
Sell = m > Close; 

HoldMinBars(Buy, Sell, 6);

Short = Cover = 0;

Then compare with

SetOption("HoldMinBars", 6);

m = MA( Close, 20 ); 
Buy = Close > m; 
Sell = m > Close; 

Short = Cover = 0;

BT results should be the same ones.

1 Like

ok, but I want to have 2 bars delay between INITIAL SELL, not every SELL signal.

How to do that?

so, im looking for some kind of reversed "holdminbars".

Hope i explain this right :slight_smile:

thanks

here is an idea (that I did not test). Maybe you need to change your sell code so that barssine works. The cross function could work:

buy = C > max(TEMA ( C , 12 ),  TEMA ( C , 26 )) and
BarsSince(cross( min(TEMA ( C , 12 )  , TEMA ( C , 26 )),c)) >2

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