Python 计算最大回撤, 使用pandas库中的.cummax()函数计算
最大回撤: ( 累计最大回报-当前回报 )最大值为最大回撤的位置
MDD = max(data['Ret'].cumsum().cummax()- data['Ret'].cumsum())
如果按周期计算(月最大回撤):
生成时间序列和随机回报:
import pandas as pd
import numpy as np
data = pd.DataFrame()
data.index = pd.date_range(start = '20100101',end = '20191231',freq = 'B',closed = 'right')
a = np.random.standard_normal(len(data))
b = a/(a.max()-a.min())*0.02
data['Ret'] = b
打标签:
freq = 'M'
data = pd.DataFrame(data['Ret'])
data_s = pd.DataFrame()
data_s['group'] = data.resample('{}'.format(freq)).sum()
data_s['group'] = data_s.index
data = data.join(data_s['group'], how='outer')
data['group'] = data['