How to Generate an nBarStop Sell/Cover Signal

I am using an nBarStop in my formula, but want to generate Sell/Cover signals as well.

The problem is that when an nBarStop occurs, no Sell/Cover signal is generated.

So, I tried "coding" this myself got the following...

// Generate Sell/Cover signal for nBarStop
nBars = 2;
for (i = 0; i < BarCount - 1; i++ ) {
	if ( Buy[ i ] == 1 ) {
		Sell[ i + nBars ] = 1;
	if ( Short[ i ] == 1 ) {
		Cover[ i + nBars ] = 1;

Even though it "kind-of" works, I know it's not correct.

Any help or suggestions would be greatly appreciated.

Your code snippet is insufficient to address the problem you are describing.
Post the nBarStop code here.

On top of that, the construct itself is incorrect because when I is equal to BarCount -1, adding Sell[ i + nBars ] is an out-of-bounds access.
Plus you are implementing a look-ahead approach whereas I believe you should be using a "look-behind" to see if two bars have passed and no exit signal has occurred, then set it.

1 Like


first, the simplest way is using ApplyStop function with stopTypeNBar.
You would just need one single ApplyStop line for both Buy and Short.

Sell = Cover = 0;
ApplyStop( stopTypeNBar, stopModeBars, nbars = 2, 1 );

Secondly if you still need loop then I have posted a few examples on how to do something nbars after entry.


and a few other codes counting bars since entry via loop.

It all exists in forum already.


Thanks @fxshrat - this is very useful!

So far, I've tried your code above...I.e.,

Sell = Cover = 0;
ApplyStop( stopTypeNBar, stopModeBars, nbars = 2, 1 );

and it works for the most part, but when I do a Backtest, I see that that some signals are generated past 2 bars...For example...nBarStop

Why does this happen?


As been mentioned several times in the forum... readers here can not walk on water so readers here can not see your screen and code from miles distance as well.

Anyway this complete rule set example works just fine

SetOption( "ReverseSignalForcesExit", False );
SetPositionSize( 1, spsShares );
SetTradeDelays( 1, 1, 1, 1 );

BuyPrice = ShortPrice = Open;
SellPrice = CoverPrice = Close;

m = MA( Close, 20 ); 
Buy = Cross( Close, m ); 
Short = Cross( m, Close );

Sell = Cover = 0;

ApplyStop( stopTypeNBar, stopModeBars, nbars = 2, priority = 1 );

Note: #Bars column includes entry bar.


Thanks fxshrat for your help - nBarStop%20fxshrat I used the code above and I am still getting a similar result...Most are fine, but a few are greater than 3 bars...see below...

Larger numbers are shown in #Bars column to symbols which have been closed but have had data holes in between entry and exit. Also larger values are shown to those symbols that are still open and have not up to date data (last date of array smaller than set end date of analysis range) and had last entry signal at less bars before nbar stop could trigger (-> last barindex of array minus barindex of entry being smaller than nbars stop).

And that is why it is counting because not being able to exit (earlier).

Anyway the code posted works as expected.

1 Like

Nice. This makes sense now. Thanks.