Exit when today close is low than 5% of entry price with in 5 first days

Hi,

I have a strategy where I would like to exit when today close is low than 5% of entry price with in 5 first days. after 5 first days. My code as below does not work. Could you please correct code or suggest me for correct code.

Thank you.
Tatum

My Code

Buy = MACD() > 0;
Sell = MACD() < 0;
 
buyentryprice = 0; 
 
for( i = 0; i < BarCount; i++ ) {
                
                if( Buy[i] && buyentryprice == 0 ) {
                                Buy[ i ] = 1;
                                buyentryprice = BuyPrice[ i ];
                } 
                
                // exit if close is lower than 0.95*buyprice within 5th first days after entry
                if ( i <= 5 )
                {
                Sell[i] = Close[ i ] < 0.95*buyentryprice;
                }
 
                if( Sell[i] && buyentryprice > 0 ) {
                                Sell[ i ] = 1;    
                                buyentryprice = 0;
                }              
}

Read here and adjust Backtesting, Exit method
You already have copied from there so why removing buyentrybar if you need
it?

1 Like

@tatum Firstly your question is incomplete. You seem to want a 5% initial stop loss (within the first 5 days of your entry). Then what?? You wrote,

exit when today close is low than 5% of entry price with in 5 first days. after 5 first days

What does “after 5 first days” in the second sentence mean?

Anyway, why are you are making this complicated and not using ApplyStop? I think your question is answered here on the forum,

And more information you should be familiar with,

https://www.amibroker.com/guide/afl/applystop.html

http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/

If after you have thoroughly looked over that information and you still think you need to do something different, then please come back and elaborate.

Good luck.

1 Like

@portfoliobuilder,

ApplyStop is more suited for “simpler” exits.

You can’t construct this via ApplyStop (-> considering %stop only within first x bars and afterwards doing something else). Why do you think I have posted link to similar code?

And there isn’t anything complicated about that.

The info about “after first 5 days…” got lost in his post. But here is some “Check this one within first five bars and trigger if true and afterwards consider doing something else (if first one didn’t trigger)…”-code.

/// @link http://forum.amibroker.com/t/exit-when-today-close-is-low-than-5-of-entry-price-with-in-5-first-days/2048/4

SetPositionSize( 100, spsShares );

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

// buy signal
BuySignal = MACD() > 0;
SellSignal2 = MACD() < 0;

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 lower than 0.95*buyprice within 5th first bars after entry
	SellSignal1 = Close[ i ] < 0.95 * buyentryprice && i <= buyentrybar + 5;	
	if( SellSignal1 && buyentryprice > 0 ) {
		Sell[ i ] = 1;    
		buyentryprice = buyentrybar = 0;
	}	

	// default sellsignal 5 bars after entry (or construct something else)
	if( SellSignal2[i] && i > buyentrybar + 5 && buyentryprice > 0 ) {
		Sell[ i ] = 1;    
		buyentryprice = buyentrybar = 0;
	}		 
} 


SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );
Plot( C, "Close", ParamColor( "Color Price", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle(), 0, 1, 0, 0);

PlotShapes( Buy*shapeUpArrow, colorGreen, 0, L, -15 );
PlotShapes( Buy*shapeHollowUpArrow, colorWhite, 0, L, -15 );
PlotShapes( Buy*shapeHollowSmallCircle, colorGreen, 0, BuyPrice, 0 );

PlotShapes( Sell*shapeDownArrow, colorRed, 0, H, -15 );
PlotShapes( Sell*shapeHollowDownArrow, colorWhite, 0, H, -15 );
PlotShapes( Sell*shapeHollowSmallCircle, colorRed, 0, SellPrice, 0 );
1 Like

NOT True.

ApplyStop has ValidFrom and ValidTo parameters that allow to define validity period. See manual:
http://www.amibroker.com/f?applystop

// last two arguments are validfrom, validto
ApplyStop( stopTypeLoss, stopModePercent, 5, False, False, 0, 0, 5 ); 
3 Likes

Thanks! Sorry, I missed that “new” functionality.

Just compared the output between loop version and Applystop version and I am getting slightly different exits at few stages. Will figure out why.

1 Like

Tomasz, via Applystop… how do you consider SellSignal2 of my sample code to be applied only after 5 bars but not before and without loop? I should have added that to my statement “(considering %stop only within first x bars and afterwards doing something else only after those x bars)”. Forgot that bold one (as it is to be seen in the code of same post). I guess you would still have to consider looping+ApplyStop.

Nevertheless thanks again for the info about valifrom-validto.

1 Like