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 相关文章推荐
利用Fn.py库在Python中进行函数式编程
Apr 22 Python
Python虚拟环境Virtualenv使用教程
May 18 Python
python使用Image处理图片常用技巧分析
Jun 01 Python
使用python3.5仿微软记事本notepad
Jun 15 Python
python多进程和多线程究竟谁更快(详解)
May 29 Python
使用Python & Flask 实现RESTful Web API的实例
Sep 19 Python
Python  unittest单元测试框架的使用
Sep 08 Python
python集合比较(交集,并集,差集)方法详解
Sep 13 Python
python实现简单井字棋小游戏
Mar 05 Python
Python闭包与装饰器原理及实例解析
Apr 30 Python
python中return如何写
Jun 18 Python
python -v 报错问题的解决方法
Sep 15 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
虹吸式咖啡壶操作
2021/03/03 冲泡冲煮
不用GD库生成当前时间的PNG格式图象的程序
2006/10/09 PHP
PHP date函数参数详解
2006/11/27 PHP
PHP jQuery表单,带验证具体实现方法
2014/02/15 PHP
解决cPanel无法安装php5.2.17
2014/06/22 PHP
PHP将MySQL的查询结果转换为数组并用where拼接的示例
2016/05/13 PHP
如何让PHP编码更加好看利于阅读
2019/05/12 PHP
accesskey 提交
2006/06/26 Javascript
Javascript调用C#代码
2011/01/17 Javascript
jquery.blockUI.js上传滚动等待效果实现思路及代码
2013/03/18 Javascript
基于jquery的手风琴图片展示效果实现方法
2014/12/16 Javascript
JS中获取函数调用链所有参数的方法
2015/05/07 Javascript
实例详解jQuery Mockjax 插件模拟 Ajax 请求
2016/01/12 Javascript
Web Uploader文件上传插件使用详解
2016/05/10 Javascript
jQuery Validate格式验证功能实例代码(包括重名验证)
2017/07/18 jQuery
JS实现图片手风琴效果
2020/04/17 Javascript
浅谈angular4实际项目搭建总结
2017/12/01 Javascript
nodejs结合Socket.IO实现的即时通讯功能详解
2018/01/12 NodeJs
微信小程序获取复选框全选反选选中的值(实例代码)
2019/12/17 Javascript
Python检测网站链接是否已存在
2016/04/07 Python
使用python编写udp协议的ping程序方法
2018/04/22 Python
Python中logging实例讲解
2019/01/17 Python
Python获取网段内ping通IP的方法
2019/01/31 Python
python实现输出一个序列的所有子序列示例
2019/11/18 Python
解决Python3下map函数的显示问题
2019/12/04 Python
python程序输出无内容的解决方式
2020/04/09 Python
Python Pivot table透视表使用方法解析
2020/09/11 Python
联想新加坡官方网站:Lenovo Singapore
2017/10/24 全球购物
英国第一摩托车和摩托车越野配件商店:GhostBikes
2019/03/10 全球购物
生物制药毕业生自荐信
2013/10/16 职场文书
实习教师个人的自我评价
2013/11/08 职场文书
财务分析个人的自荐书范文
2013/11/24 职场文书
庆元旦活动总结
2014/07/09 职场文书
纪检干部现实表现材料
2014/08/21 职场文书
网络新闻该怎么写?这些写作技巧你都知道吗?
2019/08/26 职场文书
DSP接收机前端设想
2022/04/05 无线电