策略如下:
- 回測區(qū)間為2016年10月10日至2017年10月13日,選擇滬深300進(jìn)行回測。
- 記錄所有當(dāng)天5日滑動平均價格高于20日滑動平均價格的股票
- 將總資金額的一半n/2用于買入股票,每一支股票按照等額購入
- 買入后的第二天清倉
回測過程:
數(shù)據(jù)獲?。ǐ@取股票收盤價stock_price、大盤收盤價benchmark_price以及公司名稱stocks):
def getPrice():
stocks_name='滬深300'
start_date='2016-10-01'
end_date='2017-10-13'
fields=['ClosingPx']
#選擇滬深300里面所有的股票代碼
stocks=index_components(stocks_name)
#正式獲取股票價格
stock_price=get_price(stocks,start_date=start_date,end_date=end_date,fields=fields)
#獲得滬深300指數(shù)
benchmark_name='399300.XSHE'
benchmark_price=get_price(benchmark_name,start_date=start_date,end_date=end_date,fields=fields)
return stock_price,benchmark_price,stocks
這里對滬深300指數(shù)解釋一下:對樣本空間內(nèi)股票在最近一年(新股為上市以來)的日均成交金額由高到低進(jìn)行排名,剔除排名在后50%的股票,然后對剩余股票按照日均總市值由高到低進(jìn)行排名,選取排名在前300名的股票作為指數(shù)樣本。
數(shù)據(jù)處理——計算滑動平均數(shù)(長短期):def getRolling(data,shortPeriod,longPeriod):
for i in range(len(data.ix[1])):
col=data.ix[:,i]
name=data.columns[i]
data[name+'_'+str(shortPeriod)+'days']=col.rolling(window=shortPeriod).mean()
data[name+'_'+str(longPeriod)+'days']=col.rolling(window=longPeriod).mean()
return data
主體計算過程:由于每次僅用總資金額的一半即n/2,而且購買后第二天賣出,因此每次購買時資金額都是充足。而且每一支股票投入相同資金,因此直接將所有股票當(dāng)天收益率取平均作為當(dāng)天整體的收益率。循環(huán)過程從20天滑動平均收盤價開始,第i天作為得到信息的時間,第i+1天購入,第i+2天賣出。
def calculate(data,benchmark_price,stocks):
#初始化累積收益率等
IRR=1
benchmark_revenue=1
victories=0
profits=[]
benchmark_profits=[]
#計算交易的日數(shù)
length=0
for i in range(len(data.index)-2):
portfolio=[]
print(data.index[i])
for j,company in enumerate(stocks):
if(data.ix[i,company+'_5days']>data.ix[i,company+'_20days']):
portfolio.append(company)
#如果當(dāng)天存在出現(xiàn)該信號的公司,則購買
if(portfolio):
length=length+1
#計算策略當(dāng)日收益率
new=data.ix[i+2,portfolio]/data.ix[i+1,portfolio]
#計算大盤當(dāng)日收益率
benchmark_new=benchmark_price.ix[i+2]/benchmark_price.ix[i+1]
benchmark_revenue=benchmark_revenue*benchmark_new
data.ix[i+2,'benchmark_profits']=benchmark_revenue
#計算勝率
if(new.mean()>(benchmark_new)):
victories=victories+1
#固定印花說0.1%,手續(xù)費0.08%和滑點0.246%
IRR=IRR*(new.mean()-(0.001+0.0008+0.00246))
data.ix[i+2,'profits']=IRR
#如果當(dāng)天不出現(xiàn)信號,策略的累計收益率不變,大盤的依然要更新
else:
data.ix[i+2,'profits']=data.ix[i+1,'profits']
benchmark_new=benchmark_price.ix[i+2]/benchmark_price.ix[i+1]
benchmark_revenue=benchmark_revenue*benchmark_new
data.ix[i+2,'benchmark_profits']=benchmark_revenue
#計算最大回撤
Max_drawdown=max_drawdown(data['profits'])
#為了便于畫圖,我先將策略累計收益率和大盤結(jié)合
plotData=pd.concat([data['profits'],data['benchmark_profits']],axis=1)
plotData.plot()
#計算夏普率
Sharpo_Ratio=Sharpo(data['profits'],data['benchmark_profits'])
return [IRR,victories/length,Sharpo_Ratio,Max_drawdown]
最大回撤計算:
def max_drawdown(timeseries):
max=-100
for i in np.arange(0,len(timeseries)-1,1):
for j in np.arange(i,len(timeseries),1):
if((timeseries.iloc[i]-timeseries.iloc[j])/timeseries.iloc[j])>max:
max=(timeseries.iloc[i]-timeseries.iloc[j])/timeseries.iloc[j]
return max
夏普率:def Sharpo(strategy_profits,benchmark_profits):
Sharpo_ratio=(np.mean(strategy_profits)-np.mean(benchmark_profits))/np.std(strategy_profits)
return Sharpo_ratio*np.sqrt(252)
勝率計算:VictoryRatio=日收益率大于大盤的日數(shù)\策略真正交易的日數(shù)
講解結(jié)束,全部代碼如下:
import pandas as pd
import numpy as np
def max_drawdown(timeseries):
max=-100
for i in np.arange(0,len(timeseries)-1,1):
for j in np.arange(i,len(timeseries),1):
if((timeseries.iloc[i]-timeseries.iloc[j])/timeseries.iloc[j])>max:
max=(timeseries.iloc[i]-timeseries.iloc[j])/timeseries.iloc[j]
return max
def Sharpo(strategy_profits,benchmark_profits):
Sharpo_ratio=(np.mean(strategy_profits)-np.mean(benchmark_profits))/np.std(strategy_profits)
return Sharpo_ratio
def getPrice():
stocks_name='滬深300'
start_date='2016-10-01'
end_date='2017-10-13'
fields=['ClosingPx']
#選擇滬深300里面所有的股票代碼
stocks=index_components(stocks_name)
#正式獲取股票價格(不過不知道為什么只能從2016-10-10開始)
stock_price=get_price(stocks,start_date=start_date,end_date=end_date,fields=fields)
#獲得滬深300指數(shù)(對樣本空間內(nèi)股票在最近一年(新股為上市以來)的日均成交金額由高到低進(jìn)行排名,剔除排名在后50%的股票,然后對剩余股票按照日均總市值由高到低進(jìn)行排名,選取排名在前300名的股票作為指數(shù)樣本。)
benchmark_name='399300.XSHE'
benchmark_price=get_price(benchmark_name,start_date=start_date,end_date=end_date,fields=fields)
return stock_price,benchmark_price,stocks
def getRolling(data,shortPeriod,longPeriod):
for i in range(len(data.ix[1])):
col=data.ix[:,i]
name=data.columns[i]
data[name+'_'+str(shortPeriod)+'days']=col.rolling(window=shortPeriod).mean()
data[name+'_'+str(longPeriod)+'days']=col.rolling(window=longPeriod).mean()
return data
def calculate(data,benchmark_price,stocks):
#初始化累積收益率等
IRR=1
benchmark_revenue=1
victories=0
profits=[]
benchmark_profits=[]
#計算交易的日數(shù)
length=0
for i in range(len(data.index)-2):
portfolio=[]
print(data.index[i])
for j,company in enumerate(stocks):
if(data.ix[i,company+'_5days']>data.ix[i,company+'_20days']):
portfolio.append(company)
#如果當(dāng)天存在出現(xiàn)該信號的公司,則購買
if(portfolio):
length=length+1
#計算策略當(dāng)日收益率
new=data.ix[i+2,portfolio]/data.ix[i+1,portfolio]
#計算大盤當(dāng)日收益率
benchmark_new=benchmark_price.ix[i+2]/benchmark_price.ix[i+1]
benchmark_revenue=benchmark_revenue*benchmark_new
data.ix[i+2,'benchmark_profits']=benchmark_revenue
#計算勝率
if(new.mean()>(benchmark_new)):
victories=victories+1
#固定印花說0.1%,手續(xù)費0.08%和滑點0.246%
IRR=IRR*(new.mean()-(0.001+0.0008+0.00246))
data.ix[i+2,'profits']=IRR
#如果當(dāng)天不出現(xiàn)信號,策略的累計收益率不變,大盤的依然要更新
else:
data.ix[i+2,'profits']=data.ix[i+1,'profits']
benchmark_new=benchmark_price.ix[i+2]/benchmark_price.ix[i+1]
benchmark_revenue=benchmark_revenue*benchmark_new
data.ix[i+2,'benchmark_profits']=benchmark_revenue
#計算最大回撤
Max_drawdown=max_drawdown(data['profits'])
#為了便于畫圖,我先將策略累計收益率和大盤結(jié)合
plotData=pd.concat([data['profits'],data['benchmark_profits']],axis=1)
plotData.plot()
#計算夏普率
Sharpo_Ratio=Sharpo(data['profits'],data['benchmark_profits'])
return [IRR,victories/length,Sharpo_Ratio,Max_drawdown]
stock_price,benchmark_price,stocks=getPrice()
stock_price_rolling=getRolling(stock_price,5,20)
IRR,victories,Sharpo_Ratio,Max_drawdown=calculate(stock_price_rolling[19:],benchmark_price[19:],stocks)
print([IRR,victories,Sharpo_Ratio*np.sqrt(252),Max_drawdown])
輸出結(jié)果為:[0.42318161427509665, 0.5043859649122807, -37.242237755917365, 1.395223166619767]
最后計算得到年化收益率為-57.7%,勝率為50.4%,夏普率為-37.2,最大回撤為139.5%
策略和大盤的累計收益圖片:

忽略了交易成本,輸出結(jié)果為:[1.1195674560662801, 0.5043859649122807, -8.8431527706262312, 0.092890344378694187]
最后計算得到年化收益率為11.9%,勝率為50.4%,夏普率為-8.84,最大回撤為9.2%

總結(jié):從不計交易成本的輸出結(jié)果我們可以看出,最終策略的累積收益率還是跑不贏大盤,策略本身對于累積收益率提升的貢獻(xiàn)并不大。同時由于策略交易過于頻繁,持有時間過于短暫(一天),每次交易的獲益基本都會被交易成本吸收掉,導(dǎo)致累積收益率一路走低。