How to cancel trades in the CBT

Is there a way to delete trades in the CBT that are over a cutoff % profit?

I have CBT code below to find the mid point in profitability of all of the trades a backtest makes. So for example if the max % gain a trade made was 50% and the min gain was -50%, the mid point cut off for profitability would be 0%.

I am stuck on how to complete this coding. Help appreciated :slight_smile:

SetCustomBacktestProc("");

if (Status("action") == actionPortfolio) 
{
    bo = GetBacktesterObject();	//  Get backtester object
    bo.PreProcess();	//  Do pre-processing (always required)
    
    // First, find the highest and lowest profit made by any trade
    maxval = -9999
    minval = 9999
    
    for( openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos() )
     {
        maxval = max(maxval, openpos.GetPercentProfit());
        minval = min(minval, openpos.GetPercentProfit());
     }
     
    for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
     {
        maxval = max(maxval, trade.GetPercentProfit());
        minval = min(minval, trade.GetPercentProfit());
     }     
    
    // 50% value
    cutoff = (maxval+minval)/2;
    
    // --------> THIS IS WHERE I WANT TO CANCEL TRADES IF THEIR PERCENTPROFIT IS > cutoff
    
}

You can only cancel SIGNALS BEFORE the trades get opened.
You cannot perform backtest and suddenly remove trades that you don't like (post factum after you know their profit) because it is looking into the future and as such is against the idea of backtest that is to provide realistic test results.

If you want statistics to be calculated only on those "better trades", just do that (i.e. calculate statistics).

    for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
     {
       if( trade.GetPercentProfit() > cutoff ) CalculateYourStats( trade );
        
     }     

Hi Tomasz, thank you for your reply.

My thinking for this was to remove high performing outliers that may be biasing the system's true performance, and you are right - it is only the statistics that need to be calculated on these lower performance filtered trades.

I assume from your code that in the function CalculateYourStats I could just add up all of the trade profits to find what this filtered total return would be.

But how do I see metrics such as MDD or K-Ratio on these filtered trades?

Some statistics (like MDD or K-Ratio) can't be reliably calculated if you remove some of the trades because, depending on position sizing scheme you have chosen, there is serial dependency between trades. So for example if trade A happens before trade B and the size of trade B depends on equity, then removing trade A changes equity and trade B would have different size in the absence of A.

Other than the serial dependency, nothing stops you from calculating any statistic of your own invention and adding them as custom metrics. This is really not that hard.

Alternatively you can just run second backtest with those trades disabled.

You can adopt the code from KB that reads the list of signals from file

but instead of trading on them, you would block them

Buy = ...your original buy ... and NOT OnTheExceptionList();

Thank you for that, very useful :slight_smile: