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 相关文章推荐
用生成器来改写直接返回列表的函数方法
May 25 Python
Python实现读取TXT文件数据并存进内置数据库SQLite3的方法
Aug 08 Python
Python实现动态添加属性和方法操作示例
Jul 25 Python
python 监听salt job状态,并任务数据推送到redis中的方法
Jan 14 Python
Python中使用遍历在列表中添加字典遇到的坑
Feb 27 Python
用Pytorch训练CNN(数据集MNIST,使用GPU的方法)
Aug 19 Python
docker django无法访问redis容器的解决方法
Aug 21 Python
基于tensorflow指定GPU运行及GPU资源分配的几种方式小结
Feb 03 Python
解决Jupyter Notebook使用parser.parse_args出现错误问题
Apr 20 Python
Python 中如何写注释
Aug 28 Python
Manjaro、pip、conda更换国内源的方法
Nov 17 Python
Python 机器学习工具包SKlearn的安装与使用
May 14 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
腾讯微博提示missing parameter errorcode 102 错误的解决方法
2014/12/22 PHP
php文档工具PHP Documentor安装与使用方法
2016/01/25 PHP
PHP生成各种随机验证码的方法总结【附demo源码】
2017/06/05 PHP
PHP使用pdo连接access数据库并循环显示数据操作示例
2018/06/05 PHP
JS Jquery 遍历,筛选页面元素 自动完成(实现代码)
2013/07/08 Javascript
JQuery 使用attr方法实现下拉列表选中
2014/10/13 Javascript
jQuery手机拨号界面特效代码分享
2015/08/27 Javascript
JS实现响应鼠标点击动画渐变弹出层效果代码
2016/03/25 Javascript
jQuery绑定事件的几种实现方式
2016/05/09 Javascript
Angularjs过滤器使用详解
2016/05/25 Javascript
React组件的三种写法总结
2017/01/12 Javascript
AngularJS入门教程二:在路由中传递参数的方法分析
2017/05/27 Javascript
nodejs 搭建简易服务器的图文教程(推荐)
2017/07/18 NodeJs
js 开发之autocomplete="off"在chrom中失效的解决办法
2017/09/28 Javascript
详解Nodejs get获取远程服务器接口数据
2019/03/26 NodeJs
VUE项目中加载已保存的笔记实例方法
2019/09/14 Javascript
python简单实现刷新智联简历
2016/03/30 Python
Python字符编码判断方法分析
2016/07/01 Python
python中Pycharm 输出中文或打印中文乱码现象的解决办法
2017/06/16 Python
Python DataFrame 设置输出不显示index(索引)值的方法
2018/06/07 Python
python 实现语音聊天机器人的示例代码
2018/12/02 Python
Python进阶之全面解读高级特性之切片
2019/02/19 Python
python 实现兔子生兔子示例
2019/11/21 Python
Python自动化测试笔试面试题精选
2020/03/12 Python
Django 设置admin后台表和App(应用)为中文名的操作方法
2020/05/10 Python
设计师家具购买和委托在线市场:Viyet
2016/11/16 全球购物
中国旅游网站:途牛旅游网
2019/09/29 全球购物
可靠的数据流传输TCP
2016/03/15 面试题
如何开启linux的ssh服务
2015/02/14 面试题
致跳高运动员广播稿
2014/01/13 职场文书
《雨霖铃》听课反思
2014/02/13 职场文书
毕业生简历自我评价范文
2014/04/09 职场文书
购房协议书范本(无房产证)
2014/10/07 职场文书
技术员个人工作总结
2015/03/03 职场文书
如何利用JavaScript实现二叉搜索树
2021/04/02 Javascript
电脑无法安装Windows 11怎么办?无法安装Win11的解决方法
2021/11/21 数码科技