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 相关文章推荐
PyQt5利用QPainter绘制各种图形的实例
Oct 19 Python
将Dataframe数据转化为ndarry数据的方法
Jun 28 Python
详解python中的hashlib模块的使用
Apr 22 Python
PyQt5 QTable插入图片并动态更新的实例
Jun 18 Python
python利用7z批量解压rar的实现
Aug 07 Python
python 实现多线程下载m3u8格式视频并使用fmmpeg合并
Nov 15 Python
python通过安装itchat包实现微信自动回复收到的春节祝福
Jan 19 Python
Python进程的通信Queue、Pipe实例分析
Mar 30 Python
python selenium 获取接口数据的实现
Dec 07 Python
Flask中jinja2的继承实现方法及实例
Mar 03 Python
pytorch常用数据类型所占字节数对照表一览
May 17 Python
python周期任务调度工具Schedule使用详解
Nov 23 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的FTP学习(四)
2006/10/09 PHP
php代码书写习惯优化小结
2013/06/20 PHP
php自定义函数截取汉字长度
2014/05/15 PHP
CI框架中libraries,helpers,hooks文件夹详细说明
2014/06/10 PHP
thinkphp获取栏目和文章当前位置的方法
2014/10/29 PHP
避免Smarty与CSS语法冲突的方法
2015/03/02 PHP
屏蔽PHP默认设置中的Notice警告的方法
2016/05/20 PHP
10个值得深思的PHP面试题
2016/11/14 PHP
php版微信自定义回复功能示例
2016/12/05 PHP
JavaScript创建命名空间(namespace)的最简实现
2007/12/11 Javascript
js获取select默认选中的Option并不是当前选中值
2014/05/07 Javascript
js实现下拉框效果(select)
2017/03/28 Javascript
JS利用cookies设置每隔24小时弹出框
2017/04/20 Javascript
JS 组件系列之Bootstrap Table的冻结列功能彻底解决高度问题
2017/06/30 Javascript
jquery+ajaxform+springboot控件实现数据更新功能
2018/01/22 jQuery
Vux+Axios拦截器增加loading的问题及实现方法
2018/11/08 Javascript
vue指令之表单控件绑定v-model v-model与v-bind结合使用
2019/04/17 Javascript
layui实现tab的添加拒绝重复的方法
2019/09/04 Javascript
nodejs实现UDP组播示例方法
2019/11/04 NodeJs
[59:15]完美世界DOTA2联赛PWL S2 LBZS vs FTD.C 第一场 11.20
2020/11/20 DOTA
Python减少循环层次和缩进的技巧分析
2016/03/15 Python
python读取中文txt文本的方法
2018/04/12 Python
Python hashlib模块用法实例分析
2018/06/12 Python
pyqt实现.ui文件批量转换为对应.py文件脚本
2019/06/19 Python
使用Django xadmin 实现修改时间选择器为不可输入状态
2020/03/30 Python
Python如何将函数值赋给变量
2020/04/28 Python
Python 实现国产SM3加密算法的示例代码
2020/09/21 Python
在PyCharm中安装PaddlePaddle的方法
2021/02/05 Python
解决import tensorflow导致jupyter内核死亡的问题
2021/02/06 Python
详解HTML5中div和section以及article的区别
2015/07/14 HTML / CSS
node中使用shell脚本的方法步骤
2021/03/23 Javascript
2015年企业员工工作总结范文
2015/05/21 职场文书
初二数学教学反思
2016/02/17 职场文书
优秀范文:读《红岩》有感3篇
2019/10/14 职场文书
Go遍历struct,map,slice的实现
2021/06/13 Golang
JS实现九宫格拼图游戏
2022/06/28 Javascript