博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
滑动平均线的notebook画法
阅读量:7117 次
发布时间:2019-06-28

本文共 2781 字,大约阅读时间需要 9 分钟。

滑动平均线,本程序解决了如何在matplotlib中使用中文显示,环境python2.7 最好使用 anaconda 环境

使用sns似使得图片更加美观,不多说,上代码

import tushare as tsimport pandas as pdimport matplotlib.pyplot as pltfrom matplotlib import rcrc('mathtext', default='regular')from matplotlib import datesimport matplotlib as mplimport seaborn as snssns.set_style('dark')%matplotlib inlinefont =mpl.font_manager.FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=14)stock_data = ts.get_k_data("600600")# 将数据按照交易日期从远到近排序stock_data.sort_values('date', inplace=True)# ========== 计算移动平均线# 分别计算5日、20日、60日的移动平均线# 计算简单算术移动平均线MA - 注意:stock_data['close']为股票每天的收盘价ma_list = [5, 10, 20, 60]for ma in ma_list:    stock_data['ma' + str(ma)] = stock_data.close.rolling(window=ma, center=False).mean()# 计算指数平滑移动平均线EMAfor ma in ma_list:    stock_data['ema' + str(ma)] = stock_data.close.ewm(ignore_na=False,span=ma,min_periods=0,adjust=True).mean()bar_data = stock_data[['date','volume','close','ma5','ma10','ma20','ma60','ema5','ema10','ema20','ema60']]bar_data = bar_data[60:60+340]bar_data.index = range(len(bar_data))fig = plt.figure(figsize=(14,10))fig.set_tight_layout(True)ax1 = fig.add_subplot(211)ax1.bar(bar_data.index, bar_data.volume, align='center', width=0.4)ax2 = ax1.twinx()ax2.plot(bar_data.index, bar_data.close, '-', color='r')ax2.plot(bar_data.index, bar_data.ma5, '-', color='w')ax2.plot(bar_data.index, bar_data.ma10, '-', color='y')ax2.plot(bar_data.index, bar_data.ma20, '-', color='m')ax2.plot(bar_data.index, bar_data.ma60, '-', color='g')ax1.set_ylabel(u"成交量(万)",fontproperties=font, fontsize=16)ax2.set_ylabel(u"均线 ",fontproperties=font, fontsize=16)ax1.set_title(u"蓝色柱子(左轴)为成交量,曲线为均线",fontproperties=font,fontsize=16)# plt.xticks(bar_data.index.values, bar_data.barNo.values)ax1.set_xlabel(u"平均线",fontproperties=font, fontsize=16)ax1.set_xlim(left=-1, right=len(bar_data))ax2.set_ylim(bottom=-0.5*max(bar_data.close))ax1.grid()ax1 = fig.add_subplot(212)ax1.bar(bar_data.index, bar_data.volume, align='center', width=0.4)ax2 = ax1.twinx()ax2.plot(bar_data.index, bar_data.ema5, '--', color='w')ax2.plot(bar_data.index, bar_data.ema10, '--', color='y')ax2.plot(bar_data.index, bar_data.ema20, '--', color='m')ax2.plot(bar_data.index, bar_data.ema60, '--', color='g')ax1.set_ylabel(u"成交量(万)",fontproperties=font, fontsize=16)ax2.set_ylabel(u"滑动平均线",fontproperties=font, fontsize=16)ax1.set_title(u"蓝色柱子(左轴)为成交量,曲线为滑动平均线",fontproperties=font, fontsize=16)# plt.xticks(bar_data.index.values, bar_data.barNo.values)ax1.set_xlabel(u"滑动平均线",fontproperties=font,fontsize=16)ax1.set_xlim(left=-1,right=len(bar_data))# ax2.set_ylim(bottom=-0.5*max(bar_data.smartS))ax1.grid()

运行结果如下

结果图

作者:readilen
链接:http://www.jianshu.com/p/2050d6c54d59
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

转载于:https://www.cnblogs.com/zhanglong8681/p/7569214.html

你可能感兴趣的文章
debian6 Redis+phpredis安装
查看>>
Google Auth+openssh
查看>>
NFS服务器配置及客户端挂载
查看>>
ELK(elasticsearch+logstash+kibana)开源日志分析平台搭建
查看>>
Debian 8.0桌面系统root用户登录和root用户自动登录
查看>>
Windows 8 新启动方式:混合启动(Hybrid Boot)
查看>>
*.manifest 文件
查看>>
要在jsp界面上显示一行三个控件
查看>>
我的linux学习之路-文件的创建于删除
查看>>
Linux日志分析
查看>>
ORA-00600: internal error code, arguments: [kcratr_nab_less_than_odr]
查看>>
发布/订阅模式
查看>>
RHCE证书的获得过程--1
查看>>
Java (基础自总结)
查看>>
eyoucms uihtml 带html富文本可视化标签
查看>>
SAMBA服务的搭建和访问
查看>>
nginx+webdav
查看>>
Oracle排错工具oerr
查看>>
JSP中出现According to TLD or attribute directive i...
查看>>
css !important用法CSS样式使用优先级判断
查看>>