Backtesting, Exit method

Hi!

I have a strategy where I would like to exit the first day close is higher than my entry price.
If close have´nt been higher than my entry price after five days, I would like to exit anyway.

Is this possible to do in Amibroker, and if so, how?

Appreciate all help,

Leif Axelsson
Sweden

Why are you asking whether it is possible?
Everything is possible.

Below is sample code for snake oil service sellers.

SetPositionSize( 100, spsShares );

Buy = Sell = 0;
Short = Cover = 0;

// buy signal
BuySignal = Cross( C, MA( C, 20 ) );

buyentryprice = buyentrybar = 0; 
for( i = 0; i < BarCount; i++ ) {
	
	if( BuySignal[i] && buyentryprice == 0 ) {
		Buy[ i ] = 1;
		buyentryprice = BuyPrice[ i ];
		buyentrybar = i;
	} 
	
	// exit if close is higher than buyprice or if bar is 5th one after Buy entry
	SellSignal = Close[ i ] > buyentryprice || i == buyentrybar + 5;
	
	if( SellSignal && buyentryprice > 0 ) {
		Sell[ i ] = 1;    
		buyentryprice = buyentrybar = 0;
	}	 
} 
7 Likes

Hi!

Many thanks!

/Leif