Portfolio Reentry Delay

Hello All

How can we do a portfolio reentry delay of 1 bar after the last trade?
This is to use with intra-day trading.

@Nick, I believe @fxshrat already answered this question the last time you posted it: Adding a pause between trades

The answer is still the same: you will need to create a CBT and add the delay logic there. If you've given that a try and your CBT is not working as expected, please post it and let us know what's going wrong.

Thanks mradtke, I know that you're trying to help.
Made this thread because I failed to explain the portfolio requisite in the last one.

I'm asking for help because I'm not a coder and CBT is for advanced users.
An experienced member here could fix it in minutes what would take me days/weeks.
Or maybe this has already been discussed in old forums and a link is enough.

Here's the good fxrat CBT reentry delay code for individual symbols with my settings:

SetOption( "ExtraColumnsLocation", 1 );
MaxPos = 1;//Optimize ("MaxPos",3,1,5,1);
SetOption( "MaxOpenPositions", MaxPos );
SetOption( "Initialequity", 10000 );
SetPositionSize( 100/MaxPos, spsPercentOfEquity );
//SetPositionSize( 100, spsShares );

SetTradeDelays( 0, 0, 0, 0 );



/////////////////////////////////////////////////////////////////////////////////////
/// @link http://forum.amibroker.com/t/buy-not-earlier-then-n-bars-after-sell/5558/7
/// Code to set SINGLE SECURITY (per symbol) re-entry delay for Buys occurring n-bars AFTER Sell, vrs. 1.3
/// by fxshrat@gmail.com, April 2018
/////////////////////////////////////////////////////////////////////////////////////
reentrydelay = 20;// min. Buy re-entry delay of n-bars since sell signal
tradedelay = 0;// trade entry delay

SetBarsRequired( -2 );// set number of bars required (-2 -> sbrall)

/////////////////////////////////////////////////////////////////////////////////////
// insert your buy, sell signals here
per = 35;
BuySignal = Cross( C, MA( C, per ) );//C > MA( C, per );// 
SellSignal = Cross( MA( C, per ), C );// LinRegSlope( MA(Close, 18), 2 ) < 0;//  MA( C, per ) > C;// 

Plot( MA( C, per ), "MA"+per, colorRed );
//Plot( LinRegSlope( MA(Close, 18), 2 ) < 0, "LinRegSlope < 0", colorGold, styleOwnScale );

/////////////////////////////////////////////////////////////////////////////////////


/* High level custom-backtest procedure just for additional Column output*/ 
SetCustomBacktestProc(""); 
if( Status("action") == actionPortfolio ) 
{ 
    bo = GetBacktesterObject(); 
    bo.Backtest(1); // run default backtest procedure    

    // iterate through closed trades first 
    for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) 
    { 
        staticlbars = StaticVarGet( "llbars"+trade.symbol );
		statichbars = StaticVarGet( "hhbars"+trade.symbol );
        trade.AddCustomMetric("lowestbars", Lookup( staticlbars, trade.ExitDateTime ), 0 );
        trade.AddCustomMetric("highestbars", Lookup( statichbars, trade.ExitDateTime ), 0 );  
    }
    
    for( trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos() ) 
    { 
        staticlbars = StaticVarGet( "llbars"+trade.symbol );
		statichbars = StaticVarGet( "hhbars"+trade.symbol );
        trade.AddCustomMetric("lowestbars", Lookup( staticlbars, trade.ExitDateTime ), 0 );
        trade.AddCustomMetric("highestbars", Lookup( statichbars, trade.ExitDateTime ), 0 );   
    }  

    bo.ListTrades(); 
}

// (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;	
	}		
} 

hbars = Highest(maxbars);
lbars = Lowest(minbars);
StaticVarSet( "hhbars" + Name(), hbars);
StaticVarSet( "llbars" + Name(), lbars);

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 );
}

If you're not a coder, then you are probably wise to stay away from the CBT. However, having written hundreds of strategies that utilize the CBT myself, I would be surprised if anyone could "fix it in minutes". Perhaps someone with the appropriate skills and plenty of time on their hands will write the code for you. Otherwise, you may need to resort to paying a consultant if this feature is important to you.

2 Likes