Custom Metrics - GetValue()

I was to add a custom metric to my backtest ... I have several that are working OK.

Have problem with GetValue( "# Bars");
I have tried GetValue( "#Bars"); , GetValue( "Avg. Bars Held"); but get error

My code snippet below

`bo = GetBacktesterObject();   						//  Get backtester object
        bo.Backtest();            							//  Run default Backtest (High Level)
        st = bo.GetPerformanceStats( 0 ); 					//  Get stats for all trades
//Define gain per 100 bars
		CAR					= st.GetValue( "CAR" );
		TimeInMkt			= st.GetValue( "#bars" );		// <<<<<<<<<  PROBLEM LINE
		G100bars			= CAR * 100 / TimeInMkt;`

bars is a SYMBOL / INDIVIDUAL TRADE specific stat.

you can't access it from GetPerformanceStats.
GetPerformanceStats addresses all PORTFOLIO based metrics: CAR of the whole system, CARMDD of whole system, average bars of whole system, etc.

I you need it, try mid-level/low level CBT and create a custom metric.

ong BarsInTrade

bars spent in trade (counting starts from 0)

Note however that the value of zero is available only when trade is just opened in "low-level" approach, so normally you would see numbers >= 1 (all other reporting in AB remains as it was, so enter today and exit tommorrow counts as 2-bar trade) <


If you need to know how many bars were in your entire back test (not an individual trade), you can just use the built-in BarCount variable. In Phase 1 (your high level AFL) there are usually more bars than there are periods in your From-To date range because AmiBroker loads extra data so that you can correctly calculate your indicators as of the first in-range bar. However, in the CBT bar 0 corresponds to the first trading bar of your from-to range and bar (BarCount-1) is the final trading bar of that range.


Thanks for the responses ... guess my post was not clear enough ... I was looking for the correct argument for GetValue() to use in High Level CBI backtest, trying to get the value of the Average Bars Held.

On a second look at help files I found the answer

1 Like

glad it work out! Quite nice, isn't it?

You could mark your post as solved for completeness sake.