Skip to content

Instantly share code, notes, and snippets.

@julius-datajunkie
Created December 2, 2013 18:39
Show Gist options
  • Save julius-datajunkie/7754646 to your computer and use it in GitHub Desktop.
Save julius-datajunkie/7754646 to your computer and use it in GitHub Desktop.
Drawdown Calculation
import numpy as np
import matplotlib.pyplot as plt
def drawdown2(series):
MDD = 0
peak = -99999
length = len(series)
DD = np.zeros(length)
for i in range(length):
if (series[i] > peak): # peak will be the maximum value seen so far (0 to i)
peak = series[i]
DD[i] = 100.0 * (peak - series[i]) / peak
if (DD[i] > MDD): # Same idea as peak variable, MDD keeps track of the maximum drawdown so far.
MDD = DD[i]
return DD
series = np.random.rand(5,1)
maxdd = 0
drawdown = list()
length = len(series)
for i in range(length):
cur = series[i]
dd = cur - series[0]
for j in range(1,i):
if (cur - series[j] < dd):
dd = 100 * (cur - series[j])/series[j]
drawdown.append(dd)
maxdd = min(drawdown)
plt.plot(series)
print("The Maxdrawdown is %s \n Drawdowns are %s " % (maxdd, drawdown))
print(drawdown2(series))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment