概述
我们在创建策略的过程中, 需要通过不同的评估指标来验证策略的有效性. 这些指标可以帮助我们调整策略, 得到更好的结果.
常见的策略评估指标有:
- 回测收益率
- 回测年化收益率
- 最大回撤
- 夏普比率
- Alpha, Beta
获取取票数据
- 以伊利股份和贵州茅台为例
- 分析区间 2019-01-01 到 2021-01-01
注: 贵州茅台兑牛奶, 那叫一个爽.
代码:
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
plt.style.use("fivethirtyeight")
# 定义获取数据时间段
start_date = "2019-01-01" # 开始日期
end_date = "2021-01-01" # 结束日期
# 获取洋河股份数据
data = get_price(["600887.XSHG", "600519.XSHG"], start_date=start_date, end_date=end_date, fields="close")
data = pd.DataFrame(data, index=data.index)
# 调试输出
print(data.head())
# 绘制走势图
data.plot(figsize=(16, 10), subplots=True)
输出结果:
600887.XSHG 600519.XSHG
date
2019-01-02 21.3696 583.4249
2019-01-03 21.1985 574.6781
2019-01-04 21.4076 586.3665
2019-01-07 21.6453 589.7659
2019-01-08 21.4837 589.0840
回测收益率
代码:
# 计算每天的累计收益
data_new = data / data.iloc[0]
print(data_new.head())
# 画图
data_new.plot(figsize=(16, 10))
# 区间累计收益率
total_return = data_new.iloc[-1] - 1 # 用最后一个值减去1
total_return = pd.DataFrame(total_return.values, columns=["累计收益"], index=total_return.index)
print(total_return)
输出结果:
600887.XSHG 600519.XSHG
date
2019-01-02 1.000000 1.000000
2019-01-03 0.991993 0.985008
2019-01-04 1.001778 1.005042
2019-01-07 1.012902 1.010869
2019-01-08 1.005339 1.009700
累计收益
600887.XSHG 1.076314
600519.XSHG 2.424605
回测年华收益率
代码:
# 查看数据长度
data_length = len(data_new)
print("数据长度:", data_length)
# 计算年化收益率
annual_return = pow(data_new.iloc[-1], 250 / data_length) - 1
# 调试输出
print("年化收益:")
print(annual_return)
调试输出:
数据长度: 487
年化收益:
600887.XSHG 0.455062
600519.XSHG 0.881225
Name: 2020-12-31 00:00:00, dtype: float64
最大回撤
代码:
# 计算累计最大值
max = data.cummax()
# 调试输出
print(data.head())
print(max.head())
# 计算每天回撤
drawdown_daily = (data.cummax() - data) / data.cummax()
# 调试输出
print(drawdown_daily.head())
# 计算最大回测
total_drawdown = drawdown_daily.max()
# 改成df
total_drawdown = pd.DataFrame([str(i * 100) + "%" for i in total_drawdown], columns=["最大回撤"], index= total_drawdown.index)
# 调试输出
print(total_drawdown.head())
输出结果:
# 计算累计最大值
max = data.cummax()
# 调试输出
print(data.head())
print(max.head())
# 计算每天回撤
drawdown_daily = (data.cummax() - data) / data.cummax()
# 调试输出
print(drawdown_daily.head())
# 计算最大回测
total_drawdown = drawdown_daily.max()
# 改成df
total_drawdown = pd.DataFrame([str(i * 100) + "%" for i in total_drawdown], columns=["最大回撤"], index= total_drawdown.index)
# 调试输出
print(total_drawdown.head())
输出结果:
600887.XSHG 600519.XSHG
date
2019-01-02 21.3696 583.4249
2019-01-03 21.1985 574.6781
2019-01-04 21.4076 586.3665
2019-01-07 21.6453 589.7659
2019-01-08 21.4837 589.0840
600887.XSHG 600519.XSHG
date
2019-01-02 21.3696 583.4249
2019-01-03 21.3696 583.4249
2019-01-04 21.4076 586.3665
2019-01-07 21.6453 589.7659
2019-01-08 21.6453 589.7659
600887.XSHG 600519.XSHG
date
2019-01-02 0.000000 0.000000
2019-01-03 0.008007 0.014992
2019-01-04 0.000000 0.000000
2019-01-07 0.000000 0.000000
2019-01-08 0.007466 0.001156
最大回撤
600887.XSHG 21.322799729408366%
600519.XSHG 19.270518955514174%
夏普比率
夏普比率指的是策略承受一单位总风险, 会产生多少的超额报酬.
- 假设无风险收益率为年化 3%
- 超额收益率以无风险收益率为基准
代码:
# 向后填补缺失值
data_fill = data.fillna(method='pad')
# 计算每日收益率
return_rate = data_fill.apply(lambda x: x / x.shift(1) - 1)[1:] # 去除首个NaN
# 调试输出
print(return_rate.head())
# 计算超额回报率
exReturn = return_rate - 0.03 / 250
#计算夏普比率
sharpe_rate = np.sqrt(len(exReturn)) * exReturn.mean() / exReturn.std()
#夏普比率的输出结果
SHR = pd.DataFrame(sharpe_rate,columns=['夏普比率'])
# 输出伊利和茅台的夏普比率
print(SHR)
输出结果:
| | |
|--|--|
| | |
600519.XSHG 600887.XSHG
date
2019-01-03 -0.014992 -0.008007
2019-01-04 0.020339 0.009864
2019-01-07 0.005797 0.011104
2019-01-08 -0.001156 -0.007466
2019-01-09 0.018734 0.008849
夏普比率
600519.XSHG 3.048383
600887.XSHG 1.715563
转载:https://blog.csdn.net/weixin_46274168/article/details/115652079
查看评论