运行此代码,需获取token码,这里获取token码
# coding=utf-8
import math
import tushare as ts
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import talib
import pandas as pd
from datetime import datetime, date
matplotlib.rcParams['axes.unicode_minus']=False
plt.rcParams['font.sans-serif']=['SimHei']
ts.set_token('token码')
pro = ts.pro_api()
#读取数据
start_time='20060931'
end_time="20200931"
#dsc1 = pro.index_daily(ts_code='000300.SH', start_date=start_time,end_date=end_time,fields='ts_code,trade_date,close')
#dsp = pro.index_daily(ts_code='NHCI.NH', start_date=start_time, end_date=end_time,fields='ts_code,trade_date,close')
dsb = pro.index_daily(ts_code='000001.SH', start_date=start_time, end_date=end_time)
dsb.open=0
dsb=dsb.sort_index(ascending=False)
dsb.index=pd.to_datetime(dsb.trade_date,format="%Y-%m-%d")
T= 55
for i in range( math.floor(len(dsb)/T)-1):
dsb.open[(i+1)*T]=dsb.close[(i+1)*T]-dsb.close[i*T]
DF=dsb[dsb.open!=0].open
DF=pd.DataFrame(DF)
DF=DF.rename(columns={"open":"close_change"})
DD=DF.shift(1)
DD=pd.DataFrame(DD)
DD=DD.rename( columns={ "close_change":"close_change_shift1" } )
DF1=pd.concat([DF,DD],axis=1) #月度数据
print(DF1.dropna().corr())
输出
close_change close_change_shift1
close_change 1.000000 0.416991
close_change_shift1 0.416991 1.000000
具有正相关性
# coding=utf-8
import math
import tushare as ts
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import talib
import pandas as pd
from datetime import datetime, date
matplotlib.rcParams['axes.unicode_minus']=False
plt.rcParams['font.sans-serif']=['SimHei']
ts.set_token('输入token码')
pro = ts.pro_api()
start_time='20060204'
end_time="20200428"
dsc1 = pro.index_daily(ts_code='000300.SH', start_date=start_time, end_date=end_time,fields='ts_code,trade_date,close')
dc=dsc1.set_index(dsc1.trade_date).sort_index(ascending=True)
dc.index=pd.to_datetime(dc.index,format="%Y-%m-%d" )
ret=(dc.close-dc.close.shift(1))/dc.close.shift(1)
ret=ret.sort_index(ascending=True)
T=55
###############################信号判断#######################################
sig=pd.Series(0,index=dc.index)
for i in range( math.floor(len(ret)/T)-2):
if dc.close[(1+i)*T]>dc.close[i*T]+50:
for j in range((i+1)*T+1,(i+2)*T+1):
sig[j]=1
sig=sig.tail(len(sig)-T)
RET=ret*sig
cum=np.cumprod(RET+1).dropna()
#############################策略的年化统计######################################
def Tongji(cum):
cum=cum.sort_index()
NH=(cum[-1]-1)*100*252/len(cum.index)
BD=np.std(np.log(cum/cum.shift(-1)))*np.sqrt(252)*100
SR=(NH-4)/BD
return_list=cum
MHC=((np.maximum.accumulate(return_list) - return_list) / np.maximum.accumulate(return_list)).max()*100
print("年化收益率:{:.2f}%:,年化夏普率:{:.2f},波动率为:{:.2f}%,最大回撤:{:.2f}%".format( NH,SR,BD,MHC))
############################################################################
JZ=dc.close.tail(len(cum))/dc.close.tail(len(cum))[0]#上证指数净值
if __name__=="__main__":
print("组合策略:")
Tongji(cum)
print("直接持有:")
Tongji(JZ)
plt.plot(JZ,label="000001.SH",color='b',linestyle='-')
plt.plot(cum,label="策略",color='r',linestyle='-')
plt.title("净值走势")
plt.legend()
结果:
组合策略:
年化收益率:38.38%:,年化夏普率:1.72,波动率为:19.96%,最大回撤:45.97%
直接持有:
年化收益率:17.50%:,年化夏普率:0.48,波动率为:28.01%,最大回撤:72.30%
1.python量化——alpha股票-指数期货对冲策略
2.多因子选股策略
3.海龟交易策略
4.移动平均策略——单/双均线策略
5.改进的美林时钟(介绍)
6.改进的美林时钟策略(一)
7.改进的美林时钟策略(二)
8.改进的美林时钟策略(三)
9.F-F三因子(改进代码+结果)
10.移动波动率策略