How can we combine text and numbers in the metrics of the report?

Hello. I would love to change the output metric "Winning Rate" from 85.71 to p = 85.71 %., because it makes my further processing more efficient.

WinningRate

Here is the current code:

/* First we need to enable custom backtest procedure and
** tell AmiBroker to use current formula
*/

SetCustomBacktestProc("");

/* Now custom-backtest procedure follows */

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

    bo.Backtest(); // run default backtest procedure

    st = bo.GetPerformanceStats(0); // get stats for all trades

    // Expectancy calculation (the easy way)
    // %Win * AvgProfit - %Los * AvgLos
    // note that because AvgLos is already negative
    // in AmiBroker so we are adding values instead of subtracting them
    // we could also use simpler formula NetProfit/NumberOfTrades
    // but for the purpose of illustration we are using more complex one :-)
    WinningRate=st.GetValue("WinnersPercent"); 
	
    // Here we add custom metric to backtest report
    bo.AddCustomMetric( "Winning Rate", WinningRate, Null, Null, DecPlaces=2);
} 

How can we add the p = and % to the output?

Hi @EveryDayBetter
As mentioned in the doc, AddCustomMetric second argument is a Variant, so you can provide a string. To format a string, it's possible to use StrFormat. For instance:

bo.AddCustomMetric( "Winning Rate", StrFormat("p = %.02f %%", WinningRate));

Note there are 2 AddCustomMetric functions, one for trades and another for portfolio.
Regards.

5 Likes

Thank you very much. I wish you a great weekend and great trades for all of your trading sessions. This helped a lot.

1 Like

I have tried the CODE in the post, but it can't display the Explore results as above. Can post the AFL. Thanks

Can anyone help?, kind @Beppe. Can you help me. Thanks

@beauty, the above screenshot results from an "Optimize" execution. So include in your formula some variables to be "optimized".
Then, modify the AddCustomMetric line of the original post as suggested by @alligator and it should work.

3 Likes

Thank you very much kind Mr Beppe, I will give it a try. Gbu

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.