-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcalculateMaxDD.py
29 lines (25 loc) · 1.06 KB
/
calculateMaxDD.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 21 17:08:58 2018
@author: Ernest
"""
import numpy as np
def calculateMaxDD(cumret):
# =============================================================================
# calculation of maximum drawdown and maximum drawdown duration based on
# cumulative COMPOUNDED returns. cumret must be a compounded cumulative return.
# i is the index of the day with maxDD.
# =============================================================================
highwatermark=np.zeros(cumret.shape)
drawdown=np.zeros(cumret.shape)
drawdownduration=np.zeros(cumret.shape)
for t in np.arange(1, cumret.shape[0]):
highwatermark[t]=np.maximum(highwatermark[t-1], cumret[t])
drawdown[t]=(1+cumret[t])/(1+highwatermark[t])-1
if drawdown[t]==0:
drawdownduration[t]=0
else:
drawdownduration[t]=drawdownduration[t-1]+1
maxDD, i=np.min(drawdown), np.argmin(drawdown) # drawdown < 0 always
maxDDD=np.max(drawdownduration)
return maxDD, maxDDD, i