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中不同进制互相转换(二进制、八进制、十进制和十六进制)
Apr 05 Python
linux平台使用Python制作BT种子并获取BT种子信息的方法
Jan 20 Python
python开发之anaconda以及win7下安装gensim的方法
Jul 05 Python
Python 3 判断2个字典相同
Aug 06 Python
python获取网络图片方法及整理过程详解
Dec 20 Python
matlab灰度图像调整及imadjust函数的用法详解
Feb 27 Python
基于Python第三方插件实现西游记章节标注汉语拼音的方法
May 22 Python
Keras-多输入多输出实例(多任务)
Jun 22 Python
python+requests接口自动化框架的实现
Aug 31 Python
如何实现一个python函数装饰器(Decorator)
Oct 12 Python
Python中Pyspider爬虫框架的基本使用详解
Jan 27 Python
使用python绘制分组对比柱状图
Apr 21 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
php横向重复区域显示二法
2008/09/25 PHP
php 安全过滤函数代码
2011/05/07 PHP
php/js获取客户端mac地址的实现代码
2013/07/08 PHP
PHP中使用imagick生成PSD文件缩略图教程
2015/01/26 PHP
PHP预定义变量9大超全局数组用法详解
2016/04/23 PHP
Laravel框架实现model层的增删改查(CURD)操作示例
2018/05/12 PHP
javascript 面向对象编程 function也是类
2009/09/17 Javascript
JavaScript 实现类的多种方法实例
2013/05/01 Javascript
对比分析json及XML
2014/11/28 Javascript
JS+CSS实现Li列表隔行换色效果的方法
2015/02/16 Javascript
javascript实现淘宝幻灯片广告展示效果
2015/04/27 Javascript
yui3的AOP(面向切面编程)和OOP(面向对象编程)
2015/05/01 Javascript
移动端基础事件总结与应用
2017/01/12 Javascript
javascript防篡改对象实例详解
2017/04/10 Javascript
Python中用Ctrl+C终止多线程程序的问题解决
2013/03/30 Python
python urllib urlopen()对象方法/代理的补充说明
2017/06/29 Python
详解Django之admin组件的使用和源码剖析
2018/05/04 Python
Python统计python文件中代码,注释及空白对应的行数示例【测试可用】
2018/07/25 Python
python通过robert、sobel、Laplace算子实现图像边缘提取详解
2019/08/21 Python
python数据持久存储 pickle模块的基本使用方法解析
2019/08/30 Python
tensorflow多维张量计算实例
2020/02/11 Python
python torch.utils.data.DataLoader使用方法
2020/04/02 Python
Python字符串格式化常用手段及注意事项
2020/06/17 Python
读取nii或nii.gz文件中的信息即输出图像操作
2020/07/01 Python
目前不被任何主流浏览器支持的CSS3属性汇总
2014/07/21 HTML / CSS
The Athlete’s Foot新西兰:新西兰最大的运动鞋零售商
2019/12/23 全球购物
美国婴儿服装购物网站:Gerber Childrenswear
2020/05/06 全球购物
个人简历自荐信
2013/12/05 职场文书
党员入党表决心的话
2014/03/11 职场文书
农村婚庆司仪主持词
2014/03/15 职场文书
篮球比赛口号
2014/06/10 职场文书
珍惜资源的建议书
2014/08/26 职场文书
上课说话检讨书500字
2014/11/01 职场文书
党员自我评价2015
2015/03/03 职场文书
药品开票员岗位职责
2015/04/15 职场文书
SQLServer中exists和except用法介绍
2021/12/04 SQL Server