Calculate metrics during backtest

Hi All,

At first, English is not my 1st language, but I will try best to express my problem as clear as possible.
I would like to know if I can get a certain metric during the backtest? Say for example, I run the backtest from 2007 to 2012, and I would like to know the K-ratio during the at the end of 2010. (2007 to 2010).

My idea is below:

   for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) 
		if ( condition to determine the trade time is > 2010)
		halfwayKRatio = st.getvalue( "KRatio" );	
		bo.AddCustomMetric( "halfwayKRatio", halfwayKRatio ); 

However, the value returned is the KRatio for all trades, not the trade from 2007 to 2010.

From this link:

It says:
"Stats object
Stats object provides the access to built-in backtester statistics and metrics. Metrics are usually calculated once backtest is completed but it is also possible to calculate metrics during backtest. To calculate current metrics and get the access to them simply call GetPerformanceStats Method of Backtester object. Please note that if you calculate statistics in the middle of the backtest they will include only closed trades."

But I don't find any example to calculate the metrics during backtest. Do I miss anything? anyone here can help?

Many Thanks!

I have never used the Stats object except after the entire backtest has been run. Based on the documentation that you found, it appears that it would be possible to use the Stats object in the middle of the backtest as well. To do that, you would need a mid-level or low-level CBT. Also, you wouldn't loop through the trades with bo.GetFirstTrade()... bo.GetNextTrade() as you have shown, but rather would generate the Stats object from inside the bar-by-bar loop that is the core of every mid and low level CBT.

Thanks Mradtke. I am still trying low level basktest. I think I need to calculate the K ratio by myself instead of calling the build in metric.

I added this code to my low-level CBT:

		if ( bar == floor(BarCount/2))

			st = bo.GetPerformanceStats( 0 );
			halfwayKRatio = st.getvalue( "KRatio" );	
			bo.AddCustomMetric( "halfwayKRatio", halfwayKRatio ); 

The variable bar is my loop counter for the main bar-by-bar loop in the CBT. When I look at the backtest report, I see a different value for halfwayKRatio than for the K-Ratio generated at the end of the test. Since I don't use K-Ratio, I didn't try to verify whether the new value is actually accurate, but I have no reason to believe that it is not.

Thanks Mradtke.

I tried both K ratio and Net Profit by your method. I think it works. Net Profit is more easily to verity.
I got one more question: the built-in K ratio is shown in 4 decimal place(i.e 0.0082) while the one we add by AddCustomMetric is only 2 decimal place, I tried to follow the intruction to indicate the decimal place by "bo.AddCustomMetric( "halfwayKRatio", halfwayKRatio, DecPlaces = 4 );" but doesn't work. I have already changed the Miscellaneous setting in Preferences.

Thanks a lot!!

AmiBroker doesn't allow you to name your function arguments like Visual Basic and some other languages. Instead, the values you pass are assigned based on their position, and the optional Decimal Places argument must be the 5th value passed. Try one of these methods instead:

bo.AddCustomMetric( "halfwayKRatio", halfwayKRatio, 0, 0, 4 );
bo.AddCustomMetric( "halfwayKRatio", NumToStr(halfwayKRatio, 1.4, False) );

Thanks! It works. Thanks again.

Hi, I just think of another related question - your way can detect the "halfwayKRatio" by counting when the bar equal to half of BarCount. Do you think it is possible to detect the K-ratio of the second half of the BarCount loop in the same CBT? It seems to me that, by using this method, I can check the Metric in the middle of the CBT, from the first bar(beginning of the CBT) to any given bar, but can I also modify the code to calculate the metric by any given starting point/bar to any given ending point/bar?

I have tried to start the loop by somewhere else instead of bar[0], then call the getvalue again, but it reports the same value as if i started the looping at bar[0].


The CBT must loop through all bars from 0 to BarCount-1. The only way to do what you're suggesting is to actually calculate the metric yourself instead of retrieving it from the Stats object.

1 Like

Is there a way to combine the retrieval of the K-ratio metric, as shown here, with PositionScore to create a rotational strategy based on K-ratio?

@brooksrimes, please don't ask the same question multiple times. This is at least the third thread where you've posted the same basic question.

Your question as stated doesn't really make sense, or perhaps I'm just not understanding. How does the K-Ratio of the entire portfolio help you evaluate which symbols are the most desirable for the next rotation period? You need to give some thought to what you're really trying to achieve and then express it clearly.

See: How to ask a good question