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实现基本线性数据结构
Aug 22 Python
python制作websocket服务器实例分享
Nov 20 Python
python中文分词教程之前向最大正向匹配算法详解
Nov 02 Python
pandas 两列时间相减换算为秒的方法
Apr 20 Python
通过Python模块filecmp 对文件比较的实现方法
Jun 29 Python
Python实现的拉格朗日插值法示例
Jan 08 Python
python变量的存储原理详解
Jul 10 Python
pandas DataFrame行或列的删除方法的实现示例
Aug 02 Python
Python assert关键字原理及实例解析
Dec 13 Python
解决django的template中如果无法引用MEDIA_URL问题
Apr 07 Python
在pytorch中动态调整优化器的学习率方式
Jun 24 Python
pandas进行数据输入和输出的方法详解
Mar 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
Notice: Undefined index: page in E:\PHP\test.php on line 14
2010/11/02 PHP
php一次性删除前台checkbox多选内容的方法
2013/09/22 PHP
php显示时间常用方法小结
2015/06/05 PHP
PHP获取中国时间(上海时区时间)及美国时间的方法
2017/02/23 PHP
ThinkPHP5+UEditor图片上传到阿里云对象存储OSS功能示例
2019/08/05 PHP
laravel实现查询最后执行的一条sql语句的方法
2019/10/09 PHP
jQuery对表单的操作代码集合
2011/04/06 Javascript
javascript自适应宽度的瀑布流实现思路
2013/02/20 Javascript
关于JQuery($.load)事件的用法和分析
2013/04/09 Javascript
javascript在当前窗口关闭前检测窗口是否关闭
2014/09/29 Javascript
实现图片预加载的三大方法及优缺点分析
2014/11/19 Javascript
JS动态添加Table的TR,TD实现方法
2015/01/28 Javascript
jquery表单验证插件(jquery.validate.js)的3种使用方式
2015/03/28 Javascript
javascript实现获取服务器时间
2015/05/19 Javascript
JS使用parseInt解析数字实现求和的方法
2015/08/05 Javascript
Jquery中使用show()与hide()方法动画显示和隐藏图片
2015/10/08 Javascript
基于javascript实现图片滑动效果
2016/05/07 Javascript
纯JS代码实现隔行变色鼠标移入高亮
2016/11/23 Javascript
原生JS实现图片轮播切换效果
2016/12/15 Javascript
在一个页面重复使用一个js函数的方法详解
2016/12/26 Javascript
JQuery发送ajax请求时中文乱码问题解决
2019/11/14 jQuery
Vue实现图片轮播组件思路及实例解析
2020/05/11 Javascript
Python实现注册登录系统
2017/08/08 Python
python实现淘宝秒杀脚本
2020/06/23 Python
Django框架模型简单介绍与使用分析
2019/07/18 Python
Python随机数函数代码实例解析
2020/02/09 Python
Kathmandu英国网站:新西兰户外运动品牌
2017/03/27 全球购物
国外平面设计素材网站:The Hungry JPEG
2017/03/28 全球购物
教师自荐信范文
2013/12/09 职场文书
重阳节登山活动方案
2014/02/03 职场文书
护士岗位求职应聘自荐书范文
2014/02/12 职场文书
手术室护士长竞聘书
2014/03/31 职场文书
献爱心捐款倡议书
2014/05/14 职场文书
西游降魔篇观后感
2015/06/15 职场文书
anaconda python3.8安装后降级
2021/06/11 Python
pytorch中的 .view()函数的用法介绍
2022/03/17 Python