Is it possible to get result from backtest and optimization?

Hi,
I'm wondering it is possible to get result (e.g. profit factor, sharp ratio, CAR/MDD, avg. profit/loss) from the results of backtesting and optimization? I need those values for further processing (to produce charts on PDF) in my plugin. Many thanks for any clues.

Read this document

https://www.amibroker.com/guide/a_custombacktest.html

stats = bo.GetPerformanceStats( 0 );

/// @link https://www.amibroker.com/guide/a_custombacktest.html
/// @link http://www.amibroker.com/kb/2015/01/06/how-to-display-interest-gains-in-the-backtest-report/
SetCustomBacktestProc( "" );  
if ( Status( "action" ) == actionPortfolio ) {
    bo = GetBacktesterObject();
    bo.Backtest(); // run default backtest procedure

    stats = bo.GetPerformanceStats( 0 );
    ProfitFactor= stats.GetValue( "ProfitFactor" );
    SharpeRatio= stats.GetValue( "SharpeRatio" );

    StaticVarSet( "CBT_ProfitFactor", ProfitFactor); 
    // etc.
}

// call elsewhere
ProfFactor = StaticVarGet( "CBT_ProfitFactor" );
//etc.

Or

/// @link https://www.amibroker.com/guide/a_custombacktest.html
/// @link http://www.amibroker.com/kb/2015/01/06/how-to-display-interest-gains-in-the-backtest-report/
/// @link https://forum.amibroker.com/t/is-it-possible-to-get-result-from-backtest-and-optimization/12570/2
SetCustomBacktestProc( "" );  
if ( Status( "action" ) == actionPortfolio ) {
    bo = GetBacktesterObject();
    bo.Backtest(); // run default backtest procedure

    stats = bo.GetPerformanceStats( 0 );
    ProfitFactor= stats.GetValue( "ProfitFactor" );
    SharpeRatio= stats.GetValue( "SharpeRatio" );

    metrics_num = 2;
    mat = Matrix( metrics_num, 1 );
    mat[0][0] = ProfitFactor;
    mat[1][0] = SharpeRatio;
    // etc.

    StaticVarSet( "CBT_StatsMat", mat);
}
// call elsewhere

mat = StaticVarGet( "CBT_StatsMat" );

If ( typeof( mat ) == "matrix" ) {
    ProfitFactor = mat[0][0];
    SharpeRatio = mat[1][0];
}
5 Likes

@fxshrat Many thanks for your reply, you save my day!:slightly_smiling_face: