Create a time series representing the drawdown from high-water mark given a series of prices
def calculate_drawdown(prices):
"""Create a time series representing the
drawdown from high-water mark given a
series of prices"""
high_water_mark = prices.iloc[0]
drawdown_amount = 0.
drawdown_list = [0]
for price in prices.iloc[1:]:
if price > high_water_mark:
drawdown_amount = 0.
high_water_mark = price
else:
drawdown_amount = price / high_water_mark - 1
drawdown_list.append(drawdown_amount)
return pd.Series(data=drawdown_list, index=prices.index)