股票量化分析(11)——第二个策略(5日移动均线、双均线、MACD策略)

来源:互联网 发布:发布淘宝优惠券的工作 编辑:程序博客网 时间:2024/05/01 10:38

这里主要用的是talib这个库来求股票的指标,就免得造轮子。
移动均线有好几个策略,不过都是简单的策略,包括简单的移动均线策略,双均线交叉策略,macd线的策略。目前也就觉得macd线简单好用。

先放一个简单的策略——移动均线策略。这个策略是由下向上超过均线就买入,相反就卖出,最后会发现这个策略没什么卵用,明显低于股票自身的累计收益,不过方法可以参考一下。

import tushare as tsimport pandas as pdimport matplotlib.pyplot as pltimport numpy as npimport talibdf=ts.get_hist_data('600848',start='2015-01-01',end='2015-12-31')df=df.sort_index()df.index=pd.to_datetime(df.index,format='%Y-%m-%d')#收市股价close= df.close#每天的股价变动百分率ret=df.p_change/100# 10日的移动均线为目标df['SMA_10'] = talib.MA(np.array(close), timeperiod=10)close10=df.SMA_10#处理信号SmaSignal=pd.Series(0,index=close.index)for i in range(10,len(close)):    if all([close[i]>close10[i],close[i-1]<close10[i-1]]):        SmaSignal[i]=1    elif all([close[i]<close10[i],close[i-1]>close10[i-1]]):        SmaSignal[i]=-1SmaTrade=SmaSignal.shift(1).dropna()SmaBuy=SmaTrade[SmaTrade==1]SmaSell=SmaTrade[SmaTrade==-1]SmaRet=ret*SmaTrade.dropna()#累积收益表现#股票累积收益率cumStock=np.cumprod(1+ret[SmaRet.index[0]:])-1#策略累积收益率cumTrade=np.cumprod(1+SmaRet)-1plt.rcParams['font.sans-serif']=['SimHei']plt.plot(cumStock,label="cumStock",color='k')plt.plot(cumTrade,label="cumTrade",color='r',linestyle=':')plt.title("股票累积收益率与10日平均策略收益率")plt.legend()

这里写图片描述

双均线策略:即5日短线移动平均超过30日长线移动平均即买入,其余时候空仓。

import tushare as tsimport pandas as pdimport matplotlib.pyplot as pltimport numpy as npimport talibdf=ts.get_hist_data('600848',start='2015-01-01',end='2015-12-31')df=df.sort_index()df.index=pd.to_datetime(df.index,format='%Y-%m-%d')#收市股价close= df.close#每天的股价变动百分率ret=df.p_change/100# 5\30日的移动均线为目标df['SMA_5'] = talib.MA(np.array(close), timeperiod=5)df['SMA_30'] = talib.MA(np.array(close), timeperiod=30)close5=df.SMA_5close30=df.SMA_30#处理信号SmaSignal=pd.Series(0,index=close.index)for i in range(10,len(close)):    if all([close5[i]>close30[i]]):        SmaSignal[i]=1SmaTrade=SmaSignal.shift(1).dropna()SmaRet=ret*SmaTrade.dropna()#累积收益表现#股票累积收益率cumStock=np.cumprod(1+ret[SmaRet.index[0]:])-1#策略累积收益率cumTrade=np.cumprod(1+SmaRet)-1plt.rcParams['font.sans-serif']=['SimHei']plt.plot(cumStock,label="cumStock",color='k')plt.plot(cumTrade,label="cumTrade",color='r',linestyle=':')plt.title("股票累积收益率与双均线策略收益率")plt.legend()

这里写图片描述

结果发现大部分时候双均线交易策略的收益小于股票本身的累计收益。

下面看看MACD交易策略:即DIFF大于DEA的时候买入,其他时候卖出。

import tushare as tsimport pandas as pdimport matplotlib.pyplot as pltimport numpy as npimport talibdf=ts.get_hist_data('600848',start='2015-01-01',end='2015-12-31')df=df.sort_index()df.index=pd.to_datetime(df.index,format='%Y-%m-%d')#收市股价close= df.close#每天的股价变动百分率ret=df.p_change/100 # 调用talib计算MACD指标df['DIFF'],df['DEA'],df['MACD'] = talib.MACD(np.array(close),                            fastperiod=12, slowperiod=26, signalperiod=9)diff=df.DIFFdea=df.DEA#处理信号macdSignal=pd.Series(0,index=close.index)for i in range(10,len(close)):    #if all([diff[i]>dea[i]>0,diff[i-1]<dea[i-1]]):    if all([diff[i]>dea[i]]):        macdSignal[i]=1    #elif all([diff[i]<dea[i]<0,diff[i-1]>dea[i-1]]):        #macdSignal[i]=-1macdTrade=macdSignal.shift(1).dropna()macdRet=ret*macdTrade.dropna()#累积收益表现#股票累积收益率cumStock=np.cumprod(1+ret[macdRet.index[0]:])-1#策略累积收益率cumTrade=np.cumprod(1+macdRet)-1plt.rcParams['font.sans-serif']=['SimHei']plt.plot(cumStock,label="cumStock",color='k')plt.plot(cumTrade,label="cumTrade",color='r',linestyle=':')plt.title("股票累积收益率与macd策略收益率")plt.legend()

这里写图片描述
从图中可以看出,macd策略成功躲避了暴涨暴跌,但最后收益稍微好于双移动均线,如果能够继续优化一下,能够避免那个波谷阶段的暴跌,收益还是很可观的,这就需要继续优化策略。所以学习量化分析能够让你冷静的看待自己的交易策略,并不断优化它。

阅读全文
0 0
原创粉丝点击