@1007477689
2020-07-30T17:42:06.000000Z
字数 8193
阅读 625
量化
本文为个人对广发研报《指数高阶矩择时策略》的复现,纯代码+结果。
报告认为“高阶矩”可以刻画资产价格的变化,并且有一定的领先性,可以以此构造指数择时策略,原理见研报(在公众号后台回复“高阶矩”获取研报和代码)
文章为个人对报告的理解,结果并不准确,有问题请指出
python 版本:3.6
数据来源:tushare
回测时间段:20050408——20180630
数据来自 tushare
import pandas as pd
import numpy as np
import math
import datetime
import matplotlib.pyplot as plt
import tushare as ts
%matplotlib inline
dateStart = datetime.date(2005, 4, 8)
dateEnd = datetime.date(2018, 6, 30)
HS300 = ts.get_k_data(code = '000300', index = True, start = f'{dateStart}', end = f'{dateEnd}')
HS300
>>> date open close high low volume code
0 2005-04-08 984.66 1003.45 1003.70 979.53 14762500.0 sh000300
1 2005-04-11 1003.88 995.42 1008.73 992.77 15936100.0 sh000300
2 2005-04-12 993.71 978.70 993.71 978.20 10226200.0 sh000300
3 2005-04-13 987.95 1000.90 1006.50 987.95 16071700.0 sh000300
4 2005-04-14 1004.64 986.97 1006.42 985.58 12945700.0 sh000300
5 2005-04-15 982.61 974.08 982.61 971.93 10409000.0 sh000300
xticks = np.arange(start = 0, stop = HS300.shape[0], step = int(HS300.shape[0]/7))
xticklabel = pd.Series(data = HS300.date[xticks])
plt.figure(figsize = (20,5))
fig = plt.axes()
plt.plot(x = np.arange(HS300.shape[0]), y = HS300.close, color = 'red', label = 'close')
fig.set_xticks(xticks)
fig.set_xticklabels(HS300.date[xticks], rotation = 60)
plt.show()
矩,是统计学中的一个常用的指标,用来反映数据分布的形态特点。矩,也被称为“动差”,它代表总体数据中所有变量值与任意一个给定常熟的差的 次方的算术平均数。
矩有原点矩、中心距等不同类型,在平时的统计中,我们比较常用的是原点矩和中心距。
原点矩是检验变量关于 0 的偏离程度,具体定义如下:
中心矩检验的是变量关于 “期望” 的偏离程度,具体定义如下:
其中,
可见,一阶中心距为 0,二阶中心矩就是方差。
def getHighMoment(EarnRate, k, N = 20):
```
EarnRate: 变量x
k: 所求矩的阶数k
```
HighMoment = (EarnRate**k).rolling(window = N).mean()
for i in range(1, N-1):
HighMoment[i] = (EarnRate[:i+1]**k).mean()
HighMoment.fillna(value = 0)
return HighMoment
def getEMA(moment, alpha, N = 120):
```
EMA: Exponential Moving Average
moment:
alpha: Specify smoothing factor alpha directly, 0 < alpha ≤ 1.
```
EMA = pd.DataFrame.ewm(moment, alpha = alpha, adjust = False).mean()
return EMA
报告认为,奇数阶矩对于市场走势具备一定的领先性,即:在市场即将出现上升或下降前,有明显数量级变化,我们做出 - 阶矩与指数收盘价的对比图。
其中,日收益率通过每日收盘价计算,高阶矩通过日收益率计算, 通过高阶矩计算。
# EarnRate = HS300.close.pct_change(1).fillna(0)
EarnRate = HS300['close'].pct_change(periods = 1).fillna(value = 0)
EarnRate
# .pct_change() 表示当前第 i 个元素与先前的第 i-1 个元素的相差百分比,
# 当然如果指定 periods=n,表示当前元素与先前 n 个元素的相差百分比。
>>> 0 0.000000
1 -0.008002
2 -0.016797
3 0.022683
4 -0.013917
5 -0.013060
...
Moment_20_2 = getHighMoment(EarnRate = EarnRate, k = 2)
Moment_20_3 = getHighMoment(EarnRate = EarnRate, k = 3)
Moment_20_4 = getHighMoment(EarnRate = EarnRate, k = 4)
Moment_20_5 = getHighMoment(EarnRate = EarnRate, k = 5)
Moment_20_6 = getHighMoment(EarnRate = EarnRate, k = 6)
Moment_20_7 = getHighMoment(EarnRate = EarnRate, k = 7)
五阶矩
HS = plt.subplot(111)
s1 = HS.plot(HS300.close, label = 'HS300')
HS.set_xticks(xticks)
HS.set_xticklabels(HS300.date[xticks], rotation = 60)
ax2 = HS.twinx()
s2 = ax2.plot(Moment_20_5, 'r',label = 'Moment-5')
s = s1 + s2
lns = [l.get_label() for l in s]
plt.legend(s, lns)
plt.show()
2 至 7 阶矩,从上往下依次
HS = plt.subplot(611)
HS.plot(HS300.close)
HS.set_xticks(xticks)
HS.set_xticklabels([])
ax2 = HS.twinx()
ax2.plot(Moment_20_2, 'r')
HS = plt.subplot(612)
HS.plot(HS300.close)
HS.set_xticks(xticks)
HS.set_xticklabels([])
ax2 = HS.twinx()
ax2.plot(Moment_20_3, 'r')
HS = plt.subplot(613)
HS.plot(HS300.close)
HS.set_xticks(xticks)
HS.set_xticklabels([])
ax2 = HS.twinx()
ax2.plot(Moment_20_4, 'r')
HS = plt.subplot(614)
HS.plot(HS300.close)
HS.set_xticks(xticks)
HS.set_xticklabels([])
ax2 = HS.twinx()
ax2.plot(Moment_20_5, 'r')
HS = plt.subplot(615)
HS.plot(HS300.close)
HS.set_xticks(xticks)
HS.set_xticklabels([])
ax2 = HS.twinx()
ax2.plot(Moment_20_6, 'r')
HS = plt.subplot(616)
HS.plot(HS300.close)
HS.set_xticks(xticks)
HS.set_xticklabels(HS300.date[xticks],rotation=60)
ax2 = HS.twinx()
ax2.plot(Moment_20_7, 'r')
plt.show
<function matplotlib.pyplot.show>
alpha = 0.2,0.4时的EMA
EMA_02 = getEMA(Moment_20_5,0.2)
EMA_04 = getEMA(Moment_20_5,0.4)
HS = plt.subplot(111)
HS.plot(HS300.close)
HS.set_xticks(xticks)
HS.set_xticklabels(HS300.date[xticks],rotation=60)
ax2 = HS.twinx()
ax2.plot(EMA_02, 'r')
ax3 = ax2.twinx()
ax3.plot(EMA_04, 'y')
plt.title('Moment-5')
plt.show()
切线法寻优函数
输入变量:
输出变量:
def qiexian(profitrate, Moment, EMA):
flag = np.zeros(len(profitrate))
cumrate = np.ones(len(profitrate))
for i in [i for i in range(len(profitrate)) if i > 1]:
flag[i] = 1 if EMA[i - 1] > EMA[i-2] else flag[i] = -1
# if EMA[i - 1] > EMA[i-2]:
# flag[i] = 1
# else:
# flag[i] = -1
strategy_rate = profitrate * flag + 1
totalprofit = sum (strategy_rate - 1)
return totalprofit
基本高阶矩择时模型中的交易函数
交易逻辑见报告第13页
Sharp:夏普比,年交易天数按250计,MDD:最大回撤率,其他输入输出参数意义同上
def BuySell(profitrate, Moment, EMA):
flag = np.zeros(len(profitrate))
cumrate = np.ones(len(profitrate))
lossflag = 0
# 计算单次择时损失,超过10%平仓
flagloss = 0
# 单次亏损超过10%时的信号方向
# 计算最优alpha
alpha_all = np.arange(0.05,0.5,0.05)
for i in range(2,len(profitrate)):
if i%90 ==0 :
cumrate_all = [qiexian(list(profitrate[i - 90:i]),Moment[i - 90:i].values,
getEMA(Moment,alpha_all[j])[i - 90:i].values) for j in range(len(alpha_all))]
# 取使90天内累计收益率最大的alpha作为后续计算的alpha,并计算相应的EMA
alpha = alpha_all[np.argmax(cumrate_all)]
EMA = getEMA(Moment, alpha)
EMA = getEMA(Moment, alpha)
if lossflag < -0.1 :
flag[i] = 0
flagloss = flag[i-1]
lossflag = 0
continue
if EMA[i - 1] > EMA[i-2] and flagloss != 1:
flag[i] = 1
flagloss = 0
if EMA[i - 1] < EMA[i-2] and flagloss != -1:
flag[i] = -1
flagloss = 0
if flag[i] == flag[i-1] :
lossflag = lossflag + profitrate[i]*flag[i]
lossflag = min(lossflag,0)
else:
lossflag = 0
strategy_rate = profitrate * flag
nav = (1+strategy_rate).cumprod()
cumrate = nav - 1
totalprofit = nav[len(nav)-1] - 1
# 交易次数/择时次数
num = 0
for i in range(len(flag) - 1):
if (flag[i+1]!= flag[i]) :
num+=1
Sharp = strategy_rate.mean()/strategy_rate.std()* 250**0.5
MDD = max(1-nav/nav.cummax())
return cumrate, num, totalprofit, flag, Sharp, MDD
拓展后高阶矩交易模型的交易函数
交易逻辑见报告第14页
k:阈值
其他参数含义同上
def BuySell_1(profitrate,Moment,EMA,k):
flag = np.zeros(len(profitrate))
cumrate = np.ones(len(profitrate))
lossflag = 0 # 计算单次择时损失,超过10%平仓
flagloss = 0 # 单次亏损超过10%时的信号方向
# 计算最优alpha
alpha_all = np.arange(0.05,0.5,0.05)
for i in range(2,len(profitrate)):
if i%90 ==0 :
cumrate_all = [qiexian(list(profitrate[i - 90:i]),Moment[i - 90:i].values,
getEMA(Moment,alpha_all[j])[i - 90:i].values) for j in range(len(alpha_all))]
# 取使90天内累计收益率最大的alpha作为后续计算的alpha,并计算相应的EMA
alpha = alpha_all[np.argmax(cumrate_all)]
EMA = getEMA(Moment,alpha)
if lossflag < -0.1 :
flag[i] = 0
flagloss = flag[i-1]
lossflag = 0
continue
if EMA[i - 1] > EMA[i-2]*(1+k) and flagloss != 1:
flag[i] = 1
flagloss = 0
if EMA[i - 1] < EMA[i-2]*(1-k) and flagloss != -1:
flag[i] = -1
flagloss = 0
if EMA[i - 1] > EMA[i-2]*(1-k) and EMA[i - 1] < EMA[i-2]*(1+k) :
flag[i] = 0
flagloss = 0
if flag[i] == flag[i-1] :
lossflag = lossflag + profitrate[i]*flag[i]
lossflag = min(lossflag,0)
else:
lossflag = 0
strategy_rate = profitrate * flag
nav = (1+strategy_rate).cumprod()
cumrate = nav - 1
totalprofit = nav[len(nav)-1] - 1
# 交易次数/择时次数
num = 0
for i in range(len(flag) - 1):
if (flag[i+1]!= flag[i]) :
num+=1
Sharp = strategy_rate.mean()/strategy_rate.std()* 250**0.5
MDD = max(1-nav/nav.cummax())
return cumrate, num, totalprofit, flag, Sharp, MDD
回测1:基本的高阶矩择时模型,EMA 初始 alpha=0.4
回测2:阈值k=1%,其他同上
回测3:阈值k=2%,其他同上
cumrate,num,totalprofit,flag,Sharp,MDD = BuySell(EarnRate,Moment_20_5,EMA_04)
cumrate_01,num_01,totalprofit_01,flag_02,Sharp_01,MDD_01 = BuySell_1(EarnRate,Moment_20_5,EMA_04,0.01)
cumrate_02,num_02,totalprofit_02,flag_02,Sharp_02,MDD_02 = BuySell_1(EarnRate,Moment_20_5,EMA_04,0.02)
print('回测1交易次数为:%s' % num)
print('回测2交易次数为:%s' % num_01)
print('回测3交易次数为:%s' % num_02)
print('')
print('回测1总收益率为:%s%%' % (totalprofit*100))
print('回测2总收益率为:%s%%' % (totalprofit_01*100))
print('回测3总收益率为:%s%%' % (totalprofit_02*100))
print('')
print('回测1夏普比为:%s' % Sharp)
print('回测2夏普比为:%s' % Sharp_01)
print('回测3夏普比为:%s' % Sharp_02)
print('')
print('回测1最大回撤为:%s%%' % (MDD*100))
print('回测2最大回撤为:%s%%' % (MDD_01*100))
print('回测3最大回撤为:%s%%' % (MDD_02*100))
回测1交易次数为:298
回测2交易次数为:353
回测3交易次数为:402
回测1总收益率为:870.434213293%
回测2总收益率为:859.177156583%
回测3总收益率为:571.69369844%
回测1夏普比为:0.8071530425449338
回测2夏普比为:0.8120229809371885
回测3夏普比为:0.7049768658906784
回测1最大回撤为:40.25782265430625%
回测2最大回撤为:34.72891044547697%
回测3最大回撤为:39.44870302175746%
以HS300为基准,回测1累计收益率展示
HS = plt.subplot(111)
HS.plot(HS300.close)
HS.set_xticks(xticks)
HS.set_xticklabels(HS300.date[xticks],rotation=60)
ax2 = HS.twinx()
ax2.plot(cumrate, 'r')
不同阈值下的累计净值曲线对比
HS = plt.subplot(111)
HS.plot(cumrate,'r',cumrate_01,'g',cumrate_02,'k')
HS.set_xticks(xticks)
HS.set_xticklabels(HS300.date[xticks],rotation=60)
plt.legend(['0%','1%','2%'])
plt.show()
总体来看,策略有明显超额收益的,但与文章结果出入较大,说明代码中可能有一些与报告原意不符的地方,此外,报告只回测到2015年,而从上图可以看出,策略2016年之后出现了较大回撤。