Here is last version with adding ExRem (comment or uncomment it), output calculated min and max bar delay of array, and text output for bars since last sell when there's new buy as well as MA and LinearReg plot for signal check.
/////////////////////////////////////////////////////////////////////////////////////
/// @link http://forum.amibroker.com/t/buy-not-earlier-then-n-bars-after-sell/5558/7
/// Code to set re-entry delay for Buys occurring n-bars AFTER Sell, vrs. 1.2
/// by fxshrat@gmail.com, April 2018
/////////////////////////////////////////////////////////////////////////////////////
SetBarsRequired( -2 );// set number of bars required (-2 -> sbrall)
SetPositionSize( 100, spsShares );
reentrydelay = 10;// min. Buy re-entry delay of n-bars since sell signal
tradedelay = 0;// trade entry delay
// insert your buy, sell signals here
per = 35;
BuySignal = Cross( C, MA( C, per ) );//C > MA( C, per );//
SellSignal = LinRegSlope( MA(Close, 18), 2 ) < 0;// Cross( MA( C, per ), C );// MA( C, per ) > C;//
Plot( MA( C, per ), "MA"+per, colorRed );
Plot( LinRegSlope( MA(Close, 18), 2 ) < 0, "LinRegSlope < 0", colorGold, styleOwnScale );
/////////////////////////////////////////////////////////////////////////////////////
// (un-)comment to remove/keep excessive signals
BuySignal = ExRem( BuySignal, SellSignal );
SellSignal = ExRem( SellSignal, BuySignal );
Buy = Sell = 0;
Short = Cover = 0;
if( tradedelay > 0 ) BuyPrice = SellPrice = O;
else BuyPrice = SellPrice = C;
maxbars = 0;
minbars = 1e9;
BuySig = SellSig = 0;
buyentryprice = sellexitbar = 0;
tradedelay = Min(tradedelay, Barcount-1);
for( i = tradedelay; i < BarCount; i++ ) {
b = i - tradedelay;
BuySignal2 = BuySignal[ b ] && i >= sellexitbar + reentrydelay;
if( BuySignal2 && buyentryprice == 0 ) {
Buy[ i ] = 1;
buyentryprice = BuyPrice[ i ];
BuySig[ b ] = BuySignal2;
minbars[ i ] = maxbars[ i ] = (i - sellexitbar);
sellexitbar = 0;
}
if( SellSignal[ b ] && buyentryprice > 0 ) {
Sell[ i ] = 1;
SellSig[ b ] = SellSignal[ b ];
sellexitbar = i;
buyentryprice = 0;
}
}
GraphXSpace = 8;
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 ) );
_N( Title += StrFormat( EncodeColor(colorRed) + "\nCalculated Lowest re-entry delay: %g bars", SelectedValue(Lowest(minbars)) ));
_N( Title += StrFormat( EncodeColor(colorGreen) + "\nCalculated Highest re-entry delay: %g bars", SelectedValue(Highest(maxbars)) ));
Plot( C, "Close", ParamColor( "Color Price", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle(), 0, 1, 0, 0);
PlotShapes( BuySig*shapeUpArrow, colorGreen, 0, L, -15 );
PlotShapes( Buy*shapeHollowSmallCircle, colorLightYellow, 0, BuyPrice, 0 );
PlotShapes( SellSig*shapeDownArrow, colorRed, 0, H, -15 );
PlotShapes( Sell*shapeHollowSmallCircle, colorLightYellow, 0, SellPrice, 0 );
bi = Barindex();
fvb = FirstVisiblevalue( bi );
lvb = LastVisiblevalue( bi );
fnt = "Arial";
fntsize = 8;
txtdist = 30;
PlotTextSetFont( "", fnt, fntsize, lvb, 0, -1 );
for ( i = fvb; i <= lvb; i++ ) {
if ( Buy[i] )
PlotText( "Buy\nBars since Sell: " + minbars[i], i, L[ i ], colorGreen, colorDefault, -txtdist );
if ( Sell[i] )
PlotText( "Sell\n", i, H[ i ], colorRed, colorDefault, txtdist-fntsize );
}