Delay reentry after stop until after standard exit would have triggered

I am trying to disallow reentry into a position after it has been stopped out until some other condition is met. In the code example below, the buy signal is a 3-bar low. The exit signal is a close above the 3ma. If the exit signal does not arrive in 3 days, then the timestop forces a sell. When the timestop kicks in, I do not want to consider new buys until the security has closed above the 3ma (triggered my “standard” exit signal).

Effectively, I want to have a reentry delay where a condition must be met, rather than the standard “wait x bars”.

My attempts below can be seen where I tried to use IIF logic in one case, and a Flip in the other case. Both attempts failed to accomplish my objective. Thanks for any suggestions.



Buy = Sell = Short = Cover = reset = 0;
SetTradeDelays( 0, 0, 0, 0 );
SetBacktestMode(backtestRegular);


// Fixed $ size.
SetPositionSize(10000, spsValue);

///
Xdays = 3;
XdayLowClose = Close == LLV(Close,Xdays);
daystohold = 3;


BuySignal = XdaylowClose ;
Buy = BuySignal AND reset == 1;
BuyPrice = Close;



Sell = Close > MA(Close,3);
ApplyStop( stopTypeNBar, stopModeBars, daystohold, 1);

//reset = IIf(Sell,1,Ref(reset,-1));
reset = Flip(buy, Sell);


//Explore
Filter = 1;
AddColumn(Close,"Close");
AddColumn(XdayLowClose,"XdayLowClose");
AddColumn(Buysignal,"BuySignal");
AddColumn(Buy,"Buy");
AddColumn(Sell,"Sell");
AddColumn(reset,"Reset");



Couldn't you just use:

Buy = ExRem(Buy,Sell);

To eliminate extra Buy signals until after the standard Sell has occurred?

2 Likes

Yup. I knew I was missing something easy. Thanks Matt.

@Qedges,

You are using non-backtester environment Exploration with backtester functions (SetPositionsSize, SetTradeDelays, ...) and you are using Applystop (see comments section there too).

So you should rather use Equity() function. (And Exrem in BacktestRegular (of actual backtest) is not required at all as it removes excessive signals already.)

As aside reset within your Buy assignment makes no sense as it will be zero always and as such will never trigger Buy.

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

SetTradeDelays( 0, 0, 0, 0 );// backtester function
SetBacktestMode(backtestRegular);// backtester function

// Fixed $ size.
SetPositionSize(10000, spsValue);// backtester function

///
Xdays = 3;
XdayLowClose = Close == LLV(Close,Xdays);
daystohold = 3;

BuySignal = XdaylowClose;

Buy = BuySignal;

BuyPrice = Close;
SellPrice = Close;

Sell = Close > MA(Close,3);

ApplyStop( stopTypeNBar, stopModeBars, daystohold, 1);

//reset = IIf(Sell,1,Ref(reset,-1));
//reset = Flip(buy, Sell);

// use in Explore only 
if ( Status( "action" ) == actionExplore ) {

	// single security backtester 
	// (includes removing excessive signals, evaluating stops, applying Set* functions)
	eq = Equity( 1, -1 );
	
	//Explore
	Filter = 1;//Buy or Sell;//

	AddColumn(Close,"Close");
	AddColumn(XdayLowClose,"XdayLowClose");
	AddColumn(Buysignal,"BuySignal");
	AddColumn(IIf(Buy, 1, Null),"Buy");
	AddColumn(IIf(Sell == 1, 1, Null),"Regular Sell exit");
	AddColumn(IIf(Sell == 5, 1, Null),"n-bar stop exit");
	//AddColumn(reset,"Reset");
}
2 Likes