Untitled
unknown
plain_text
a year ago
1.0 kB
9
Indexable
#%% Drawdown functions
def findDrawdowns(dfReturn, treshold):
df = pd.DataFrame()
df['Return'] = dfReturn
df['Value'] = np.cumprod(1 + df['Return'])
df['Drawdown'] = df['Value'] / df['Value'].expanding().max() - 1
dfDrawdown = pd.DataFrame(columns=['Start', 'Bottom', 'End', 'MaxDwon', 'TimeToBottom', 'TimeToRecovery'])
idx = 1
start, bottom, maxDown, toBottom, monthCount = None, None, 0, 0, 0
for t, down in zip(df.index, df['Drawdown']):
if down == 0:
if maxDown < treshold:
dfDrawdown.loc[idx] = [start, bottom, t, maxDown, toBottom, monthCount + 1 - toBottom]
idx += 1
start = t
bottom, maxDown, toBottom, monthCount = None, 0, 0, 0
else: # in drawdown
monthCount += 1
if down < min(treshold, maxDown):
bottom = t
maxDown = down
toBottom = monthCount
return dfDrawdownEditor is loading...
Leave a Comment