Average drawdown calculation - CBT

Hi all,

This reply New metric CAGR/Ave DD:

shows an output where @portfoliobuilder outputted an average of the five worst drawdowns based on Cesar Alvarez' code. I'm attempting to calculate the median drawdowns (where a dd is the lowest point from each dd's initiation to its end).

Any ideas of how to 1) identify all the drawdowns, and 2) calculate the median of those values?

Sorry for the noob question but have tried various things for hours and I'm stuck...

Thanks in advance!

Cesar's code for average drawdown is available on his website. Since @Tomasz discourages advertising on the forum, I will leave it to you to Google that.

1 Like

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

That's great, thank you so much!!! Instead of the average, how would you calculate the median instead of avg? Could you use the Median() function? But that function takes an array as input (so I guess the drawdowns would need to be saved to a list somehow?) and then the number of values, right?

Add these lines at the end of my upper code:

sp_dd = SparseCompress(bi < 5, dd_sorted);
med = LastValue(Percentile(sp_dd, 5, 50));
printf( "Median of worst 5 drawdown periods: %1.2f%%\n", med );
// bo.AddCustomMetric("Median of 5 worst DD periods", med);

Note: it calculates for period 5. If you want to calculate for other period then replace all fives in the entire code by period and define period = 5; at the top of entire code.

3 Likes

You are a legend!!! Thank you so much - exactly what I had hope to do!

1 Like

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