NBarStop, but only if not enough profit

I have a long only trend following strategy which already has various stops coded for (a stock EMA, a market filter, a Trailing Stop and MaxLoss Stop).

Sometimes I have a position that moves into some degree of profit and then just drifts along for weeks or months without hitting any stops. I would like to add an NBarStop, but only if profit is below a certain amount. For example, if the position has only moved 12% above the Buy Price at 100 days I want to have the NBarStop close the position. However, if at 100 days the stock is more than 12% above the Buy Price, then continue to hold the position open with the NBarStop ignored from that point on. (A dynamic NBarStop would be nice, but that's probably a bridge too far at this point).

The following code stops everything out at the nominated day, regardless of profitability. I've searched the forum and AB KB, but can't find anything that specifically answers my need.

Any ideas?

thanks,
Chris.

MinProfitAmount =  1.12; //Optimize("MPA", 1.05, 1.01, 1.25, 0.01);
NumberBars = 100; //Optimize ("nBars", 15, 50, 250, 5); //215
IIf (( Close < (BuyPrice + (BuyPrice*MinProfitAmount ))), ApplyStop(stopTypeNBar, 1, NumberBars, 0, False, 1, 0. -1), Null);

Your use of IIf and ApplyStop is incorrect.

IIF function is returning array. It is not a procedure.
So it should be written like this with assignment to variable:
x = IIf(is_true, true_case, false_case);

On the other hand ApplyStop is returning nothing. So it doesn't make sense to put into IIf function.


When using ApplyStop you can do something along these lines:

Sell = 0;
SellPrice = Close;

amount = 12;
valid_from = 100;
valid_to = 100;
exit_at_stop = 0;
// exit if price has gained minimum amount at set bar after entry
// -> so any price hitting profit level at set bar
ApplyStop(stopTypeProfit, stopModePercent, amount, exit_at_stop, False, 0, valid_from, valid_to );

But since you have additional requirement -> exit at any price smaller than 12% level plus...

I think you would have to write custom stop via loop for that case (so without ApplyStop completely).

So you would have to do something like this

/// @link https://forum.amibroker.com/t/nbarstop-but-only-if-not-enough-profit/27049/3
/// @link http://www.amibroker.com/guide/h_backtest.html
SetPositionSize( 100, spsShares );

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

nbars = 100;

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

SellPrice = Close;

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 smaller than buyprice*1.12 and if bar is nth one after Buy entry
	SellSignal = Close[ i ] <= buyentryprice*1.12 AND i == buyentrybar + nbars;	
	if( SellSignal && 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", colorDefault, styleBar);

PlotShapes( Buy*shapeUpArrow, colorGreen, 0, L, -15 );
PlotShapes( Sell*shapeDownArrow, colorRed, 0, H, -15 );
1 Like

For what it's worth, consider using a 'Stale Stop' (based off indicator/s) instead of a NBarStop, testing with and preferably without added conditions such as your 12% rule.

2 Likes

Thank you gentlemen. These both look like really good solutions worth exploring - I shall do so and report back.

1 Like

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