I am very new to Python, Pandas and to portfolio statistics in general. I have a large portfolio with different assets and over 4000 rows of return numbers. I am trying to calculate the start, end and duration of top N max drawdowns for each asset (column). I am using Pandas library and I am applying the nsmallest() on my drawdown list to find out the top n smallest values which are max drawdowns, I then iterate through the columns and add max drawdown values for each asset to the ndrawdowns list. However, I struggle to find the start of those drawdowns and duration. This is what I have so far:
def max_drawdown(self, ddcount):
returns = self.df
maxd = returns.cummax()
dd = returns/maxd - 1.0
def finddrawdowns(n, count=ddcount):
dd1 = dd.nsmallest(count, dd.columns[n])
d = dd1[dd1.columns[n]]
return d
ndrawdowns = []
for a in range(0, length):
values = finddrawdowns(a)
ndrawdowns.append(values)
return ndrawdowns
Any recommendations would be welcome! Thank you!