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中的CURL PycURL使用例子
Jun 01 Python
研究Python的ORM框架中的SQLAlchemy库的映射关系
Apr 25 Python
深入解读Python解析XML的几种方式
Feb 16 Python
Python 性能优化技巧总结
Nov 01 Python
python批量读取txt文件为DataFrame的方法
Apr 03 Python
Pycharm 操作Django Model的简单运用方法
May 23 Python
使用python对多个txt文件中的数据进行筛选的方法
Jul 10 Python
TensorBoard 计算图的可视化实现
Feb 15 Python
Python基于yield遍历多个可迭代对象
Mar 12 Python
python 在threading中如何处理主进程和子线程的关系
Apr 25 Python
基于django2.2连oracle11g解决版本冲突的问题
Jul 02 Python
从Pytorch模型pth文件中读取参数成numpy矩阵的操作
Mar 04 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
函数中使用require_once问题深入探讨 优雅的配置文件定义方法推荐
2014/07/02 PHP
PHP 只允许指定IP访问(允许*号通配符过滤IP)
2014/07/08 PHP
php版微信公众平台回复中文出现乱码问题的解决方法
2016/09/22 PHP
PHP实现简单登录界面
2019/10/23 PHP
javascript this用法小结
2008/12/19 Javascript
利用jQuery 实现GridView异步排序、分页的代码
2010/02/06 Javascript
javascript面向对象之Javascript 继承
2010/05/04 Javascript
模仿百度三维地图的js数据分享
2011/05/12 Javascript
jquery获取tr并更改tr内容示例代码
2014/02/13 Javascript
js实现横向伸展开的二级导航菜单代码
2015/08/28 Javascript
详解JavaScript的Date对象(制作简易钟表)
2020/04/07 Javascript
如何解决easyui自定义标签 datagrid edit combobox 手动输入保存不上
2015/12/26 Javascript
全面了解javascript三元运算符
2016/06/27 Javascript
Bootstrap表单布局
2016/07/19 Javascript
jquery结合html实现中英文页面切换
2016/11/29 Javascript
node.js程序作为服务并在windows下开机自启动(用forever)
2017/03/29 Javascript
webpack学习笔记之代码分割和按需加载的实例详解
2017/07/20 Javascript
详解如何在React组件“外”使用父组件的Props
2018/01/12 Javascript
vue中Axios的封装与API接口的管理详解
2018/08/09 Javascript
vue展示dicom文件医疗系统的实现代码
2018/08/27 Javascript
vue调试工具vue-devtools安装及使用方法
2018/11/07 Javascript
Python性能提升之延迟初始化
2016/12/04 Python
Python实现小数转化为百分数的格式化输出方法示例
2017/09/20 Python
Python实现简易Web爬虫详解
2018/01/03 Python
TensorFlow入门使用 tf.train.Saver()保存模型
2018/04/24 Python
Python实现扫码工具的示例代码
2020/10/09 Python
python利用opencv保存、播放视频
2020/11/02 Python
用Python实现童年贪吃蛇小游戏功能的实例代码
2020/12/07 Python
python3中celery异步框架简单使用+守护进程方式启动
2021/01/20 Python
教师旷工检讨书
2014/01/18 职场文书
房地产推广策划方案
2014/05/19 职场文书
党员个人对照检查材料范文
2014/09/24 职场文书
领导班子整改措施
2014/10/24 职场文书
应聘教师自荐信
2015/03/26 职场文书
2019年行政人事个人工作总结范本!
2019/07/19 职场文书
用php如何解决大文件分片上传问题
2021/07/07 PHP