How to write Portfolio Wise Target

Dear Frnds,

I have a question, Individual Level I can write the Targets and Stoploss.

Just want to check, If I am running my strategy on 50 Stocks Portfolio, on the 50 stocks I would like to place the Target or Stoploss.

So, is this possible, if possible, can someone can help me out by giving some sample code for the Portfolio Level Targets.

BRgds,
Rama Reddy

Everything is possible. And everything is already covered in Knowledge Base. I am yet to see a single question that is not covered in existing documentation. Of course not always verbatim copy-paste, but close enough to just take and modify, like this: http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/

As shown in the KB article you can use bo.Equity that holds current portfolio-level equity or bo.EquityArray that holds up-to-current-bar equity array to do whatever you wish to do. You can for example check if bo.Equity dropped below highest value ever encountered by given percent to implement your portfolio-level stop.

The example code below is just taken from the KB article pointed above and just few lines are added and few removed. Really. Take it and compare with KB article. It does not involve rocket science.

/* YOUR SYSTEM MUST GO HERE 
** Not for copy-paste artists
** You need to UNDERSTAND the code before copy-pasting
** and add the system 
** as the formula is only for CBT part
*/

if( Status("action") == actionPortfolio )
{
  bo = GetBacktesterObject();
 
  bo.PreProcess(); // Initialize backtester
 
  HighestEquity = 0; 
 
  for(bar=0; bar < BarCount; bar++)
  {
   bo.ProcessTradeSignals( bar );
  
   CurEquity = bo.Equity;
   
   if( CurEquity > HighestEquity )
   {
       HighestEquity = CurEquity;
   }
   
   if( CurEquity < 0.8 * HighestEquity )
   {
	// portfolio level equity dropped 20% below highest
	// DO WHAT EVER YOU WISH
	
	// for example exit all open positions
	for( openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos() )
	{
		bo.ExitTrade( bar, pos.Symbol, pos.GetPrice( bar, "C" ) );
	}
   }
  
  }
  bo.PostProcess(); // Finalize backtester
}
2 Likes

Thanks for the Updates…Let me try out and let you know if I have any doubts.

BRgds,
Rama Reddy

I found some errors when I've tried to test it

I had to added

SetCustomBacktestProc( "" );

And pos. is not declared, I tried with

bo.ExitTrade( bar, openpos.Symbol, openpos.GetPrice( bar, "C" ) );

It seems good priori but I have not been able to avoid that problem

Once is below that percentage it is very unlikely that it will go back, right? every trade you open closes it immediately.

How can we fix the concept of close trades but having the system recoverable later?

Thank you in advance!