import numpy as np
np.maximum.accumulate(np.array([100, 120, 130, 100, 65, 80, 100, 120, 140, 160]))
array([100, 120, 130, 130, 130, 130, 130, 130, 140, 160])
values = [100, 120, 130, 100, 65, 80, 100, 120, 140, 160]
values = [100, 80, 75, 90,140,180, 220, 160, 190]
values = [500000,750000,400000,600000,350000]
def cummax( nums ):
cum = []
max = 0
for item in nums:
if item > max:
max = item
cum.append( max )
return cum
drawdown = [x - y for x, y in zip(values, cummax(values))]
idx_lower = drawdown.index(min(drawdown))
idx_upper = values.index(max(values[:idx_lower]))
print((values[idx_lower]-values[idx_upper])/values[idx_upper])
-0.5333333333333333
import numpy as np
def mdd(x):
arr = np.array(x)
idx_lower = np.argmin(arr - np.maximum.accumulate(arr))
idx_upper = np.argmax(arr[:idx_lower])
return (arr[idx_lower] - arr[idx_upper]) / arr[idx_upper]
print(mdd(values))
-0.5333333333333333
values = [100, 120, 130, 100, 65, 80, 100, 120, 140, 160]
print(cummax(values))
print(np.maximum.accumulate(np.array(values)))
[100, 120, 130, 130, 130, 130, 130, 130, 140, 160] [100 120 130 130 130 130 130 130 140 160]