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...
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.
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.
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?
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.