Buy&Hold CAR column in Backtest window

Hi, how do you code a custom metrics for Buy&Hold CAR in Custom Backtest AFL ? B&H is in the report but I'd like to have it as a Backtest column. Thanks !

Guillaume

/// B&H CAR as column
/// https://forum.amibroker.com/t/buy-hold-car-column-in-backtest-window/8024/3
bi		= BarIndex();
fbr		= Status( "firstbarinrange" );
lbr		= Status( "lastbarinrange" );
fbrbi	= LastValue( ValueWhen( fbr, bi ) );
lbrbi	= LastValue( ValueWhen( lbr, bi ) );
dd1900	= DaysSince1900();
days	= dd1900[ lbrbi ] - dd1900[ fbrbi ];
CAR		= 100 * ( ( C / C[ fbrbi ] ) ^ ( 365 / days ) - 1 );
StaticVarSet( "CBT_CAR" + Name(), CAR );

// Custom Metrics Backtest
SetCustomBacktestProc( "" ); // activate custom backtester
if ( Status( "action" ) == actionPortfolio ) {
    bo = GetBacktesterObject(); 
    bo.Backtest( 1 );
    
    // Iterate through closed trades
    for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) {         
        car = StaticVarGet( "CBT_CAR" + trade.symbol ); 
        trade.AddCustomMetric( "CAR", StrFormat( "%1.2f%%", Lookup( car, trade.ExitDateTime )) );  
    }
    
    // Iterate through open pos.    
    for ( trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos() ) {         
        car = StaticVarGet( "CBT_CAR" + trade.symbol ); 
        trade.AddCustomMetric( "CAR", StrFormat( "%1.2f%%", Lookup( car, trade.ExitDateTime )) );  
    }

    bo.ListTrades(); 
}

Edit: if applying multi-symbol portfolio backtest then you should rather use Foreign Close price

e.g.

 frg_close = Foreign( "SPY", "C" );

instead of just C in CAR line.

1 Like

Hi fxshrat, thanks for helping ! I've tried your code and I get the CAR column in the trade list but it outputs 100000000000.00% so I guess there's a problem :wink: Also I would like this column only in the summary, I guess using bo.AddCustomMetric but with what parameters ?

Am I sitting in front of your monitor? How would I know what you have applied??
In my test there is no Null value (resulting in -10000000000% output).

2018-09-15_234842

If whole column is showing "-100.....00%" then the reason is that you have used Foreign function instead of simple Close array (as I have done in upper code example) and in addition you have inserted a symbol name to that function's first argument that does not exist in your DB (or that symbol has no data)!

If I do that (using non existing symbol in foreign function) then I get Null column (resulting in -10000000000%) too.

29

But I did not use Foreign in upper code.
You did but you did not tell that you have applied changed code but instead you let people guessing.
Thank you very much!

Check foreign symbol's DB existence or its data!

You did not say that. You just said "as a Backtest column".
Consider manual. It is explained there already how to add custom stat to report.

1 Like

I have not replaced C with anything, I just copy/pasted your code. But if I replace C / C[ fbrbi ] with a number (say 2 for example) then I get a number in the CAR column so C is in fact the problem, why it stays empty I don't know... I test on 1 stock only.

EDIT: I know the reason now... uncheck PAD & ALIGN in backtest settings.
Then I can reproduce (if it is checked)..


(This below text is previous response which might be obsolete now...)

I can not reproduce it (except for if using foreign() with non existing symbol)!

What stock? What data vendor? EOD? What first and last day imported? Which Analysis range? Which analysis settings?

So please upload symbol used... go to data base's subfolder having same first letter as your symbol name. Zip that symbol's data file and upload. Also upload APX file.

It's that simple.

No one knows what you have applied.

In addition you may try this CAR alternative

arr		= C;//Foreign( "SPY", "C" );//
dd1900	= DaysSince1900();
fbr		= Status( "firstbarinrange" );
lbr		= Status( "lastbarinrange" );
fbrdd	= LastValue( ValueWhen( fbr, dd1900 ) );
lbrdd	= LastValue( ValueWhen( lbr, dd1900 ) );
days	= lbrdd-fbrdd;
fb_arr	= LastValue(ValueWhen(fbr, arr));
CAR		= 100 * ( ( arr / fb_arr ) ^ ( 365 / days ) - 1 );
1 Like