Adding Percent of Portfolio on Entry as a Trade Metric

I’d like to add Percent of Portfolio on Entry as a Trade Metric.

For example, if I buy IBM and it’s 20% of my portfolio on entry – that would be added as a Trade Metric.

The code I’ve created is like that at “AddCustomMetric” below but I’d replace “1.0” with “Total_Equity_on_Entry_Date”

What I don’t know how to do is access Equity on Entry Date.

Any tips are appreciated.

SetCustomBacktestProc("");
if( Status("action") == actionPortfolio ) {
	bo = GetBacktesterObject(); 
	bo.PreProcess(); 

	for (trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos())  

           { trade.AddCustomMetric("Entry%", 100*trade.GetEntryValue/1.0); }

	for (trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade())

           { trade.AddCustomMetric("Entry%", 100*trade.GetEntryValue/1.0); }

	bo.PostProcess(); 
}
FastMA = MA( C,  5 );
SlowMA = MA( C, 20 );
Buy  = Cross( FastMA, SlowMA );
Sell = Cross( SlowMA, FastMA );

Either bo.Equity or bo.EquityArray should get you what you need.

Matt

2 Likes
SetCustomBacktestProc("");
if( Status("action") == actionPortfolio ) {
	bo = GetBacktesterObject(); 
	bo.PreProcess(); 

	for (trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos())  

           {
            
 trade.AddCustomMetric("Entry%", 100*trade.GetEntryValue/1.0); }

	for (trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade())

           { trade.AddCustomMetric("Entry%", 100*trade.GetEntryValue/1.0); }

	bo.PostProcess(); 
}
FastMA = MA( C,  5 );
SlowMA = MA( C, 20 );
Buy  = Cross( FastMA, SlowMA );
Sell = Cross( SlowMA, FastMA );

It should be noted that the CBT presented here (twice now) will not actually enter any trades. There are lots of examples of working CBTs available, but here’s one that directly addresses custom metrics: https://www.amibroker.com/guide/a_custommetrics.html.

1 Like

By accident I clicked twice in space bar and the post was saved and I didn’t edit it in time, so please disregard the previous formula, which is the same as the original post.

As @mradtke suggested, you can try with bo.equityarray.
You can read more from this post in KB : http://www.amibroker.com/kb/2014/11/20/how-to-show-indicator-values-in-backtest-trade-list/

The correct code:

SetCustomBacktestProc( "" );

if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();
    // run default backtest procedure without generating the trade list
    bo.Backtest( True );

    // iterate through closed trades
    for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
    {
        Eq = Lookup( bo.EquityArray, trade.EntryDateTime );
        trade.AddCustomMetric("Entry", 100*trade.GetEntryValue/eq); ;
    }

    // iterate through open positions
    for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
    {
        Eq = Lookup( bo.EquityArray, trade.EntryDateTime );
        trade.AddCustomMetric("Entry", 100*trade.GetEntryValue/eq); ;
    }

    // generate trade list
    bo.ListTrades( );
}

// your trading system here
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );

4 Likes

Thank you, @pmxgs ! That look like “it”.

Correct, @mradtke, my code above will not do much – I was presenting a snippet to draw focus to the trade.AddMetric section – sorry for the confusion – full version of my (simple) code at top is below.

SetCustomBacktestProc("");
if( Status("action") == actionPortfolio ) {
	bo = GetBacktesterObject(); 
	bo.PreProcess(); 
	for( bar = 0; bar < BarCount; bar++ ) {
	    myEqt[bar] = bo.Equity; 
		bo.ProcessTradeSignals( bar ); 
	} 
	for (trade = bo.GetFirstOpenPos();	trade; trade = bo.GetNextOpenPos())
		{ trade.AddCustomMetric("Entry%", trade.GetEntryValue/1.0); }
	for (trade = bo.GetFirstTrade(); 	trade; trade = bo.GetNextTrade())
		{ trade.AddCustomMetric("Entry%", trade.GetEntryValue/1.0); }
	bo.PostProcess(); 
}
FastMA = MA( C,  5 );
SlowMA = MA( C, 20 );
Buy  = Cross( FastMA, SlowMA );
Sell = Cross( SlowMA, FastMA );
2 Likes