Long/short equity

There's a way to call short or long equity() during backtest ?
I would like to check short exposure bar by bar.

You can extract short/long of portfolio or single sec. equity via CBT

equity_name = "~SystemEquity";
SetCustomBacktestProc( "" );
if ( Status( "actionex" ) == actionPortfolio ) {
	bo = GetBacktesterObject();
	bo.preProcess();// initialize backtester

	/// @link https://forum.amibroker.com/t/long-short-equity/13297/2
	short_eq = long_eq = Null;
	for ( i = 0; i < BarCount; i++ ) {
		bo.ProcessTradeSignals( i );

		for ( op = bo.GetFirstOpenPos(); op; op = bo.GetNextOpenPos() ) {
			if( op.IsLong ) 
				long_eq[ i ] = bo.Equity;
			if( ! op.IsLong ) 
				short_eq[ i ] = bo.Equity;				
		}    
	}

	bo.postProcess();// finalize backtester        

	flag = atcFlagDeleteValues | atcFlagEnableInPortfolio;
	AddToComposite( bo.EquityArray, equity_name, "C", flag );
	AddToComposite( long_eq, equity_name, "H", flag );
	AddToComposite( short_eq, equity_name, "L", flag );
}

SetForeign(equity_name);
_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 ) );
Plot( IIf( H > 0, H, Null), "Long Equity", colorGreen, styleStaircase );
Plot( IIf( L > 0, L, Null), "Short Equity", colorRed, styleStaircase );
Plot( C, "Equity", colorDefault, styleStaircase );
RestorePriceArrays();

// dummy system
m = MA( Close, 20 );
Buy = Cross( Close, m ); 
Sell = Cross( m, Close ); 
Short = Sell;
Cover = Buy;

1

5 Likes