股票量化分析(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
- 股票量化分析(11)——第二个策略(5日移动均线、双均线、MACD策略)
- 股票量化分析(10)——第一个策略(rsi强度策略)
- 股票量化分析(12)——第三个策略(kdj策略)
- 量化分析(7)——移动均线、macd线
- 量化分析(9)——借助talib库来直接获得MACD、动量、rsi、移动均线
- 股票策略的选择(平均移动均线)
- 量化进阶——量化交易策略之羊驼和均线策略
- 量化交易——传统技术分析平滑异同移动平均线MACD的原理及实现
- 量化投资(二):五日均价策略
- 股票量化分析(1)准备——tushare安装
- 股票量化分析(2)准备——Ipython安装
- 量化策略分析师
- 量化进阶—— 高胜算交易策略(布林线)
- 4月8日股票分析以及明日策略
- 4月14日股票分析以及后市操作策略
- 量化进阶——多策略量化回测实录(一)
- 量化进阶——多策略量化回测实录(二)
- 量化策略——市盈率策略选股
- 安卓(JAVA)读取Mp3文件中的歌曲信息(及readLine()出现乱码问题的解决方法)
- char *string="xxxxxxxxx" 与 char string[]="xxxxx"的区别
- Oracle之分区表效率与普通表比较
- 电路分析-第四章 分解方法及单口网络
- Ubuntu为安装的软件添加启动图标
- 股票量化分析(11)——第二个策略(5日移动均线、双均线、MACD策略)
- 移动端1px边框的实现
- 如何 在Spring MVC中 使用多个Spring和MyBatis的xml配置文件(多模块配置)
- 仟滋佰味商城
- MySQL概述
- 在ubuntu V16.04 中 编译树莓派2 linu内核
- 用开源软件将shp文件导入MySQL,并通过geoserver发布地图服务
- KMP算法中next数组和nextval数组值的推导
- Unity_通讯案例_Udp