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里隐藏的“禅”
Jun 16 Python
Python3基础之输入和输出实例分析
Aug 18 Python
使用Protocol Buffers的C语言拓展提速Python程序的示例
Apr 16 Python
在Windows系统上搭建Nginx+Python+MySQL环境的教程
Dec 25 Python
ubuntu环境下python虚拟环境的安装过程
Jan 07 Python
详解python中的json和字典dict
Jun 22 Python
python 解决动态的定义变量名,并给其赋值的方法(大数据处理)
Nov 10 Python
Python3.5基础之函数的定义与使用实例详解【参数、作用域、递归、重载等】
Apr 26 Python
pytorch之Resize()函数具体使用详解
Feb 27 Python
pandas.DataFrame.drop_duplicates 用法介绍
Jul 06 Python
Python SMTP发送电子邮件的示例
Sep 23 Python
Python 正则模块详情
Nov 02 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
用DBSQL类加快开发MySQL数据库程序的速度
2006/10/09 PHP
PHP应用JSON技巧讲解
2013/02/03 PHP
php获取域名的google收录示例
2014/03/24 PHP
php根据用户名和手机号查询是否存在手机号码
2017/02/16 PHP
js鼠标左右键 键盘值小结
2010/06/11 Javascript
json格式的时间显示为正常年月日的方法
2013/09/08 Javascript
js实现浏览本地文件并显示扩展名的方法
2015/08/17 Javascript
js接收并转化Java中的数组对象的方法
2016/08/11 Javascript
js中动态创建json,动态为json添加属性、属性值的实例
2016/12/02 Javascript
JavaScript中object和Object的区别(详解)
2017/02/27 Javascript
JavaScript实现父子dom同时绑定两个点击事件,一个用捕获,一个用冒泡时执行顺序的方法
2017/03/30 Javascript
微信小程序 商城开发(ecshop )简单实例
2017/04/07 Javascript
深入浅析AngularJS中的一次性数据绑定 (bindonce)
2017/05/11 Javascript
Express+Nodejs 下的登录拦截实现代码
2017/07/01 NodeJs
vue-router实现tab标签页(单页面)详解
2017/10/17 Javascript
浅谈Vue网络请求之interceptors实际应用
2018/02/28 Javascript
原生js实现的观察者和订阅者模式简单示例
2020/04/18 Javascript
vue实现购物车加减
2020/05/30 Javascript
swiper自定义分页器的样式
2020/09/14 Javascript
js异步接口并发数量控制的方法示例
2020/11/22 Javascript
[01:09:13]DOTA2-DPC中国联赛 正赛 CDEC vs XG BO3 第三场 1月19日
2021/03/11 DOTA
深入解析Python中的上下文管理器
2016/06/28 Python
Python中列表和元组的使用方法和区别详解
2020/12/30 Python
python中实现迭代器(iterator)的方法示例
2017/01/19 Python
python批量赋值操作实例
2018/10/22 Python
canvas如何绘制钟表的方法
2017/12/13 HTML / CSS
Hotels.com南非:酒店预订
2017/11/02 全球购物
全球性的女装店:storets
2019/06/12 全球购物
简单的JAVA编程面试题
2013/03/19 面试题
学习党课思想汇报
2013/12/29 职场文书
2014年招生工作总结
2014/11/26 职场文书
2014年食品安全工作总结
2014/12/04 职场文书
个人简历求职信范文
2015/03/20 职场文书
同学聚会开幕词
2019/04/02 职场文书
详解MySQL中的主键与事务
2021/05/27 MySQL
Python 数据可视化工具 Pyecharts 安装及应用
2022/04/20 Python