Custom Backtest metric - new highs

I'm trying to create a custom backtest metric to count the total number of days not setting new equity highs during the entire backtest. What is wrong with my code below?

if( Status("action") == actionPortfolio ) 
    bo = GetBacktesterObject(); 
    st = bo.GetPerformanceStats(0);
    eq = bo.EquityArray();
    DaysSinceLastHigh = HighestBars(eq);
    UnProfitableDays = Cum(DaysSinceLastHigh);

    MyCustomMetric =  UnProfitableDays;
    bo.AddCustomMetric( "MyCustomMetric", MyCustomMetric ); 

You are summing twice. HighestBars is already a sum of bars that passed since last high. If you want to count days that are NOT making new highs. You should be using

NotNewHigh = HighestBars(eq) > 0;
UnProfitableDays = Cum(NotNewHigh );
1 Like

Thank you Tomaz, I have corrected the code as you suggested. But the main problem is that the code is not outputting any results. See the screenshot below where the custom metric has no value:


You are passing array, while per-backtest metric (a single number) should be scalar.

So your code is incorrect and should be changed:

// if "myCustomMetric" is an array, you have to use LastValue for per-backtest metric
bo.AddCustomMetric( "MyCustomMetric", LastValue( MyCustomMetric ) );

Thank you. That worked and I learned something new :slightly_smiling_face:

1 Like

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