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的Tornado框架实现一个简单的WebQQ机器人
Apr 24 Python
在Linux下使用Python的matplotlib绘制数据图的教程
Jun 11 Python
Python入门_学会创建并调用函数的方法
May 16 Python
python合并同类型excel表格的方法
Apr 01 Python
Python3.6笔记之将程序运行结果输出到文件的方法
Apr 22 Python
Python字符串、整数、和浮点型数相互转换实例
Aug 04 Python
Python图像处理之图像的缩放、旋转与翻转实现方法示例
Jan 04 Python
详解用python生成随机数的几种方法
Aug 04 Python
用python实现英文字母和相应序数转换的方法
Sep 18 Python
python实现操作文件(文件夹)
Oct 31 Python
如何在python中处理配置文件代码实例
Sep 27 Python
Python自动化爬取天眼查数据的实现
Jun 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
PHP 遍历文件实现代码
2011/05/04 PHP
PHP用星号隐藏部份用户名、身份证、IP、手机号等实例
2014/04/08 PHP
javascript window对象属性整理
2009/10/24 Javascript
!DOCTYPE声明对JavaScript的影响分析
2010/04/12 Javascript
JS获取并操作iframe中元素的方法
2013/03/21 Javascript
自己编写的类似JS的trim方法
2013/10/09 Javascript
js实现选中页面文字将其分享到新浪微博
2015/11/05 Javascript
jquery判断复选框选中状态以及区分attr和prop
2015/12/18 Javascript
浅析Angular19 自定义表单控件
2018/01/31 Javascript
Vue实现6位数密码效果
2018/08/18 Javascript
10分钟彻底搞懂Http的强制缓存和协商缓存(小结)
2018/08/30 Javascript
深入理解Vue 的钩子函数
2018/09/05 Javascript
Vue实现导航栏的显示开关控制
2019/11/01 Javascript
JavaScript 异步时序问题
2020/11/20 Javascript
Python实现字典的key和values的交换
2015/08/04 Python
Python实现进程同步和通信的方法
2018/01/02 Python
Python实现App自动签到领取积分功能
2018/09/29 Python
对Python 3.2 迭代器的next函数实例讲解
2018/10/18 Python
关于python之字典的嵌套,递归调用方法
2019/01/21 Python
python基础梳理(一)(推荐)
2019/04/06 Python
Python如何处理大数据?3个技巧效率提升攻略(推荐)
2019/04/15 Python
python的re模块使用方法详解
2019/07/26 Python
如何使用amaze ui的分页样式封装一个通用的JS分页控件
2020/08/21 HTML / CSS
国外最大的眼镜网站:Coastal
2017/08/09 全球购物
丝芙兰香港官网:Sephora香港
2018/03/13 全球购物
Brydge英国:适用于Apple iPad和Microsoft Surface Pro的蓝牙键盘
2019/05/16 全球购物
Windows和Linux动态库应用异同
2016/04/17 面试题
公司面试感谢信
2014/02/01 职场文书
竞聘演讲稿
2014/04/24 职场文书
五一劳动节慰问信
2015/02/14 职场文书
2015年全国“爱牙日”宣传活动总结
2015/03/23 职场文书
刑事附带民事代理词
2015/05/25 职场文书
解决numpy数组互换两行及赋值的问题
2021/04/17 Python
在pyCharm中下载第三方库的方法
2021/04/18 Python
python中if和elif的区别介绍
2021/11/07 Python
使用CSS设置滚动条样式
2022/01/18 HTML / CSS