Average drawdown calculation - CBT

That code by Alvarez does not calculate the average drawdown.
Based on description and looking at that code it is supposed to list the 5 worst drawdowns.

But AFAICS, it does not work properly because it does not do that (listing the 5 worst ones).

See below picture if outputting some array's drawdown with that code on chart the highest drawdown is around 69.2%. But the code lists 44.0% as worst one in interpretation window.

So I have no idea what is the real purpose of that code.

30

So here I've made code myself

/// @link https://forum.amibroker.com/t/average-drawdown-calculation-cbt/27568/3
eq = C;//bo.EquityArray();
dd = -100 * ( eq / Highest( eq ) - 1 );

bi = BarIndex();
is_last_bar = bi == LastValue( bi );

is_zero = dd == 0;
hh_perc = HighestSince(is_zero, dd);
hh_perc = IIf(is_zero OR is_last_bar, Ref( hh_perc, -1 ), 0);
dd_sorted = Reverse(Sort( hh_perc ));

//Plot( dd, "Drawdown", colorDefault );
//Plot( hh_perc, "hh Drawdown", colorred );
//Plot( dd_sorted, "hh Drawdown", colorOrange );

printf( "Worst 5 drawdown periods:\n" );
for ( i = 0, cs_dd = 0; i < 5; i++ ) {
    printf( "DD%g: %1.2f%%\n", i+1, dd_sorted[i] );
    //bo.AddCustomMetric("DD"+(i+1), dd_sorted[i]);
    cs_dd += dd_sorted[i];
}
printf( "\nAvg. of worst 5 drawdown periods: %1.2f%%\n", cs_dd / i );
// bo.AddCustomMetric("Avg. of 5 DD", cs_dd / i);

And here is output (69% is listed as worst one now):
30

4 Likes