Disable Custom Backtester When Running an Optimization

Hello,
is there a way to disable custom backtester when running an optimization?

Either by a conditional statement that would not include CBT code from other AFL file, or a parameter that would allow to skip CBT runs in any Optimization context.

I tried

SetCustomBacktestProc( "" );
if (Status("action") == actionPortfolio)
{
    bo = GetBacktesterObject();
    if (Status("actionex") != actionExOptimizePortfolio)
    {
        // Code here
    }
}

Which I just discovered in this thread Why i get error "COM/Object handle is null"? a solution from Steve, but it seems the CBT still runs: when I run an individual optimization it says it can't because of the CBT.

No that is wrong.

You can disable via SetCustomBacktestProc 2nd argument...

E.g.

if ( Status( "actionex" ) == actionExOptimizeBacktest ) 
	enable = False;
else 
	enable = True;	
	
SetCustomBacktestProc("", enable ); 

So e.g. some full example....

SetOption("GenerateReport", 1);// enable report creation

fa = Optimize( "MACD Fast", 5, 5, 12, 1 );
sa = Optimize( "MACD Slow", 34, 26, 34, 1 );
sig = 5;// Optimize( "Signal average", 5, 5, 9, 1 );

SetPositionSize( 1, spsShares );

tradedelay = 0; // entry/exit bar delay  
SetTradeDelays( tradedelay, tradedelay, tradedelay, tradedelay );
 
if ( tradedelay > 0 )
    array  = Open;
else
    array  = Close;

Buy = Cross( MACD( fa, sa ) , Signal( fa, sa, sig ) );
Sell = Cross( Signal( fa, sa, sig ), MACD( fa, sa ) ) ;
Short = Cover = 0;    

BuyPrice = SellPrice = array; 
ShortPrice = CoverPrice = array;


if ( Status( "actionex" ) == actionExOptimizeBacktest ) 
	enable = False;
else 
	enable = True;	

// AB code snippet	
SetCustomBacktestProc("", enable ); 
if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();
    bo.Backtest(); // run default backtest procedure

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

    some_metric = 1; // your calculation here
    bo.AddCustomMetric( "some_metric", some_metric);
}
2 Likes

Thank you for your answer. It correctly disable the CBT from running when optimizing.

But when I "individual optimize" it seems to run the CBT code and says CBT is not supported.
It's because I think the individual optimize is not recognized by "actionExOptimizeBacktest " as hardcoding "false" instead of "enable" correctly disable it and allows "individual optimize".

I don't know if this makes any difference but I use:

bo.PreProcess(); 

in my CBT code.

CBT simply is NOT supported in any individual optimization.

1 Like

Edit:

CBT simply is NOT supported in any individual optimization.

Yes, thanks for reminding that. This is why I wanted to disable CBT also when running "individual optimize".

I think:

if ( Status( "actionex" ) == actionBacktest  ) 
	enable = True;
else 
	enable = False;	

SetCustomBacktestProc("", enable ); 

could do the trick, as it disables CBT even in individual optimize context also, which was the goal.

I mark this as solution for future readers, but your post definitely put me on the right track.

@Brakkar,

the code you posted will disable CBT in individual backtest.
"actionex" in individual backtest returns value 20.

So more complete

// enable CBT in portfolio and individual backtest only
actionIndividualBT = 20;// individual backtest
action_ex = Status("actionex");
if (action_ex == actionBacktest OR action_ex == actionIndividualBT)
   enable = True;
else
   enable = False;

// AB code snippet	
SetCustomBacktestProc("", enable); 
if ( Status( "action" ) == actionPortfolio ) {    
    _TRACE("Inside CBT");
    
    bo = GetBacktesterObject();
    bo.Backtest(); // run default backtest procedure

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

    some_metric = 1; // your calculation here
    bo.AddCustomMetric( "some_metric", some_metric);
}

Also it would work like this since "actionex" returns 21 in individual optimization

// enable CBT in portfolio and individual backtest only
actionIndividualOpt = 21;//  individual optimization
action_ex = Status("actionex");
if (action_ex != actionExOptimizeSetup AND action_ex != actionExOptimizeBacktest AND action_ex != actionIndividualOpt)
   enable = True;
else
   enable = False;

// AB code snippet	
SetCustomBacktestProc("", enable); 
if ( Status( "action" ) == actionPortfolio ) {    
    _TRACE("Inside CBT");
    
    bo = GetBacktesterObject();
    bo.Backtest(); // run default backtest procedure

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

    some_metric = 1; // your calculation here
    bo.AddCustomMetric( "some_metric", some_metric);
}
2 Likes

Thanks for the comprehensive solution.

if (status("actionex") != 21)
   enable = True;
else
   enable = False;
SetCustomBacktestProc("", enable); 

I am trying to get custom metrics to run when I press regular optimize and when I press individual optimize this would turn off CBT, it seems to work but then the optimization goes on forever.  Am I missing something, should this work?