Prevent reentry when signal's price is below previous stop loss level

Does anyone know where I should look for in the documentation regarding the issue? I am thinking whether I could use methods other than a loop to save the previous stop loss level in a variable. Thanks.

@jackyjacky135 the ApplyStop() function has a specific parameter:

ReEntryDelay - how many bars to wait till entering the same stock is allowed.

Did you try to use it?

Hi beppe, thanks for the advice but I am looking for how to invalidate a signal when the signal is generated below the last SL level instead of using number of days to prohibit a potential trade.

You have to create loop (without ApplyStop) since Buy is dependent on previous Sell (level).

Below code will always enter above previous exit level (of Stop or of regular Sell).
If you only want to enter above prev. stop level otherwise entering as usual then uncomment line 25.

procedure BuyAbovePrevExitLevel(SellSignal,percent) {
	/// code source:
	/// @link https://tinyurl.com/7jd6je8w
	/// by fxshrat@gmail.com
	global Buy, Sell, BuyPrice, SellPrice;
	local i, stp_array, stp_level, prev_sl_level, pcnt_level;
	stp_level = prev_sl_level = 0; 
	stp_array = Null;
	pcnt_level = 1 - percent/100;
	for ( i = 0; i < BarCount; i++ ) {			
		if ( stp_level > 0 ) {	
			// STOP LOSS EXIT
			if ( L[ i ] < stp_level ) {
				Sell[ i ] = 99;   
				SellPrice[ i ] = Min(O[ i ], stp_level); 
				prev_sl_level = stp_level;
				stp_level = 0;
			} 	
			// REGULAR Sell	EXIT
			if ( SellSignal[ i ] ) {
				Sell[ i ] = 1;  
				prev_sl_level = SellPrice[i]; 
				// if you do not always want Buy above 
				// prev. REGULAR Sellprice then set to 0
				//prev_sl_level = 0; 
				stp_level = 0;
			} 
		}  else Sell[ i ] = 0;
		//
		is_aboveSL = BuyPrice[ i ] > prev_sl_level;
		if ( Buy[ i ] && stp_level == 0 && is_aboveSL ) {			
			stp_level = BuyPrice[ i ] * pcnt_level;
			prev_sl_level = 0;
		} else Buy[ i ] = 0;	
		//
		if ( stp_level > 0 )
			stp_array[ i ] = stp_level;			
	}
	return stp_array;
}

Buy = Cross( MACD(), Signal() );
Sell = Cross(C, MA(C,200));
BuyPrice = Close;
SellPrice = Close;
Short = Cover = 0;	

percent = 3;// precent stop level
stp_arr = BuyAbovePrevExitLevel(Sell,percent);

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", 
						O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
Plot( stp_arr, "stp_arr", colorRed );
color = IIf(Sell==1, colorOrange,colorRed);
y = IIf( Buy, BuyPrice, SellPrice );
PlotShapes( Buy * shapeSmallCircle, colorGreen, 0, y, 0);
PlotShapes( (Sell>0) * shapeSmallCircle, color, 0, y, 0);

Also read about QuickAFL here before commenting in "Signals appear and disappear..."

5 Likes

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