股票收益评价指标的几个计算(用于回测)

最大回测率计算

import numpy as np
import matplotlib.pyplot as plt
def MaxDrawdown(return_list):
    '''最大回撤率'''
    return_list = np.array(return_list)
    i = np.argmax((np.maximum.accumulate(return_list) - return_list) / np.maximum.accumulate(return_list)) 
    # 结束位置
    if i == 0:
        return 0
    j = np.argmax(return_list[:i])  # 开始位置
    return (return_list[j] - return_list[i]) / (return_list[j])
return_list=[12,12,21,15,27,16,21,22,25,20,16,17]
print(MaxDrawdown(return_list))

计算贝塔系数

def beta(gcode,start_date,end_date,zcode='399300.SZ'):
    df1 = pro.daily(ts_code=gcode, start_date=start_date, end_date=end_date).sort_values(by='trade_date')
    #提取深市指数
    df2 = pro.index_daily(ts_code=zcode, start_date=start_date, end_date=end_date).sort_values(by='trade_date')
    #计算贝塔系数
    s1 = df1['pct_chg']
    s2 = df2['pct_chg']
    s = (np.cov(s1, s2))[0][1]/np.var(s2)
    return s 

##计算夏普比率

def shape(gcode,start_date,end_date):
    df1 = pro.daily(ts_code=gcode, start_date=start_date, end_date=end_date).sort_values(by='trade_date')
    x = df1['pct_chg'] - 0.04/252
    s = (x.mean() * math.sqrt(252))/x.std()#粒度是以天算的,要乘以252天
    return s

你可能感兴趣的:(股票)