对一个基金A,有每日收益序列RET_A,用python计算其收益回撤比。
思考: 收益回撤比 = 年化收益率 / 最大回撤
计算年化收益率
import numpy as np
import pandas as pd
import random
# 设置一个随机种子
random.seed(10)
import matplotlib.pylab as plt
RET_A = [random.uniform(-0.1,0.1) for _ in range(100)] # 随便生成的收益率数据
# 首先计算年化收益率
N = len(RET_A)
date_line = range(N)
#计算组合净值曲线
capital_line = np.cumprod(1 + np.array(RET_A)).tolist()
#根据 A0 * (1+ annual_rtn)^(N/250) = AN
annual_rtn = pow(capital_line[-1] / capital_line[0], 250/N ) -1
计算最大回撤
def max_drawdown(date_line , capital_line):
df = pd.DataFrame({'date': date_line , 'capital': capital_line})
#计算到时刻x 之前 的最大组合净值
df['max_up_to_now'] = list(map(lambda x : max(df.ix[0:x ,'capital']) , range(len(capital_line))))
df['drawdown'] = df['capital'] / df['max_up_to_now']
max_dd = max(abs(df['drawdown']))
return max_dd
max_dd = max_drawdown(date_line , capital_line)
result = annual_rtn / max_dd