Python绘制股票移动均线的实例


Posted in Python onAugust 24, 2019

1. 前沿

移动均线是股票最进本的指标,本文采用numpy.convolve计算股票的移动均线

2. numpy.convolve

numpy.convolve(a, v, mode='full')

Returns the discrete, linear convolution of two one-dimensional sequences.

The convolution operator is often seen in signal processing, where it models the effect of a linear time-invariant system on a signal [R17]. In probability theory, the sum of two independent random variables is distributed according to the convolution of their individual distributions.

If v is longer than a, the arrays are swapped before computation.

Parameters:

a : (N,) array_like

 First one-dimensional input array.

 v : (M,) array_like

 Second one-dimensional input array.

 mode : {‘full', ‘valid', ‘same'}, optional

 ‘full':

  By default, mode is ‘full'. This returns the convolution at each point of overlap, with an output shape of (N+M-1,). At the end-points of the convolution, the signals do not overlap completely, and boundary effects may be seen.
 ‘same':

  Mode same returns output of length max(M, N). Boundary effects are still visible.
 ‘valid':

  Mode valid returns output of length max(M, N) - min(M, N) + 1. The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect.

Returns:

out : ndarray

 Discrete, linear convolution of a and v.

计算公式:

Python绘制股票移动均线的实例

eg:

>>> import numpy as np
>>> 
>>> np_list = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> 
>>> np_list
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x = np.convolve(np_list, 2)
>>> x
array([ 2, 4, 6, 8, 10, 12, 14, 16, 18])
>>> x = np.convolve(np_list, [0.5, 0.5])
>>> x
array([ 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 4.5])

3. 移动均线计算

def moving_average(x, n, type='simple'):
 x = np.asarray(x)
 if type == 'simple':
  weights = np.ones(n)
 else:
  weights = np.exp(np.linspace(-1., 0., n))

 weights /= weights.sum()

 a = np.convolve(x, weights, mode='full')[:len(x)]
 a[:n] = a[n]
 return a
ma10 = moving_average(close_data, 10, 'simple')
 ma20 = moving_average(close_data, 20, 'simple')

 ax1.plot(data['date'], ma10, color='c', lw=2, label='MA (10)')
 ax1.plot(data['date'], ma20, color='red', lw=2, label='MA (20)')

4. 效果图

Python绘制股票移动均线的实例

以上这篇Python绘制股票移动均线的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python多重继承实例
Oct 11 Python
详解Python Socket网络编程
Jan 05 Python
Python线性回归实战分析
Feb 01 Python
Python创建普通菜单示例【基于win32ui模块】
May 09 Python
PyCharm更改字体和界面样式的方法步骤
Sep 27 Python
详解pycharm连接不上mysql数据库的解决办法
Jan 10 Python
Python线程条件变量Condition原理解析
Jan 20 Python
详解有关PyCharm安装库失败的问题的解决方法
Feb 02 Python
keras自定义损失函数并且模型加载的写法介绍
Jun 15 Python
python实现定时发送邮件到指定邮箱
Dec 23 Python
Python Pandas 删除列操作
Mar 16 Python
分享Python获取本机IP地址的几种方法
Mar 17 Python
python+selenium 鼠标事件操作方法
Aug 24 #Python
python+selenium select下拉选择框定位处理方法
Aug 24 #Python
Python封装成可带参数的EXE安装包实例
Aug 24 #Python
python识别文字(基于tesseract)代码实例
Aug 24 #Python
python图片二值化提高识别率代码实例
Aug 24 #Python
关于Python形参打包与解包小技巧分享
Aug 24 #Python
python-序列解包(对可迭代元素的快速取值方法)
Aug 24 #Python
You might like
深入PHP购物车模块功能分析(函数讲解,附源码)
2013/06/25 PHP
PHP PDOStatement:bindParam插入数据错误问题分析
2013/11/13 PHP
Codeigniter操作数据库表的优化写法总结
2014/06/12 PHP
通过php删除xml文档内容的方法
2015/01/23 PHP
PHP全局变量与超级全局变量区别分析
2016/04/01 PHP
Thinkphp框架开发移动端接口(2)
2016/08/18 PHP
PhpStorm本地断点调试的方法步骤
2018/05/21 PHP
自己的js工具_Form 封装
2009/08/21 Javascript
juqery 学习之六 CSS--css、位置、宽高
2011/02/11 Javascript
EasyUI中combobox默认值注意事项
2015/03/01 Javascript
JS面向对象编程详解
2016/03/06 Javascript
vue之浏览器存储方法封装实例
2018/03/15 Javascript
浅谈Vue下使用百度地图的简易方法
2018/03/23 Javascript
微信小程序-可移动菜单的实现过程详解
2019/06/24 Javascript
JavaScript中clientWidth,offsetWidth,scrollWidth的区别
2021/01/25 Javascript
[52:15]2014 DOTA2国际邀请赛中国区预选赛5.21 HGT VS LGD-GAMING
2014/05/23 DOTA
[01:58]2018DOTA2亚洲邀请赛趣味视频——交流
2018/04/03 DOTA
Python Web框架Flask中使用七牛云存储实例
2015/02/08 Python
Python实现的数据结构与算法之基本搜索详解
2015/04/22 Python
浅谈python中截取字符函数strip,lstrip,rstrip
2015/07/17 Python
Python实现的简单计算器功能详解
2018/08/25 Python
解决nohup执行python程序log文件写入不及时的问题
2019/01/14 Python
python tkinter控件布局项目实例
2019/11/04 Python
基于python实现MQTT发布订阅过程原理解析
2020/07/27 Python
加拿大廉价机票预订网站:CheapOair.ca
2018/03/04 全球购物
激光脱毛、蓝光和护肤:Tria Beauty
2019/03/28 全球购物
瑞士隐形眼镜和护理产品网上商店:Linsenklick
2019/10/21 全球购物
局部内部类是否可以访问非final变量?
2013/04/20 面试题
酒店办公室文员岗位职责
2013/12/18 职场文书
投标承诺书范本
2014/03/27 职场文书
党员教师四风问题对照检查材料
2014/09/26 职场文书
2014年小学校长工作总结
2014/12/08 职场文书
金秋助学感谢信
2015/01/21 职场文书
读书笔记怎么写
2015/07/01 职场文书
创业计划书之花店
2019/09/20 职场文书
解析Java异步之call future
2021/06/14 Java/Android