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 队列详解及实例代码
Oct 18 Python
Python实现读写sqlite3数据库并将统计数据写入Excel的方法示例
Aug 07 Python
利用Python进行异常值分析实例代码
Dec 07 Python
Django中间件工作流程及写法实例代码
Feb 06 Python
Python 实现Windows开机运行某软件的方法
Oct 14 Python
对python GUI实现完美进度条的示例详解
Dec 13 Python
python使用插值法画出平滑曲线
Dec 15 Python
Pycharm激活码激活两种快速方式(附最新激活码和插件)
Mar 12 Python
浅析PyCharm 的初始设置(知道)
Oct 12 Python
Python eval函数介绍及用法
Nov 09 Python
还在手动盖楼抽奖?教你用Python实现自动评论盖楼抽奖(一)
Jun 07 Python
在python中读取和写入CSV文件详情
Jun 28 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 MemCached高级缓存配置图文教程
2010/08/05 PHP
Yii数据库缓存实例分析
2016/03/29 PHP
PHP+Ajax+JS实现多图上传
2016/05/07 PHP
Windows服务器中PHP如何安装redis扩展
2019/09/27 PHP
脚本吧 - 幻宇工作室用到js,超强推荐base.js
2006/12/23 Javascript
jquery 插件实现图片延迟加载效果代码
2010/02/06 Javascript
JavaScript 读取元素的CSS信息的代码
2010/02/07 Javascript
jQuery AJAX回调函数this指向问题
2010/02/08 Javascript
iframe的onload在Chrome/Opera中执行两次Bug的解决方法
2011/03/17 Javascript
一款简单的jQuery图片标注效果附源码下载
2016/03/22 Javascript
Node.js connect ECONNREFUSED错误解决办法
2016/09/15 Javascript
Javascript获取background属性中url的值
2016/10/17 Javascript
基于jQuery实现的查看全文功能【实用】
2016/12/11 Javascript
利用node实现一个批量重命名文件的函数
2017/12/21 Javascript
Node.js log4js日志管理详解
2018/07/31 Javascript
webpack4 处理CSS的方法示例
2018/09/03 Javascript
NodeJS加密解密及node-rsa加密解密用法详解
2018/10/12 NodeJs
node脚手架搭建服务器实现token验证的方法
2021/01/20 Javascript
[03:56]显微镜下的DOTA2第十一期——鬼畜的死亡先知播音员
2014/06/23 DOTA
[04:01]2014DOTA2国际邀请赛 TITAN告别Ohaiyo期望明年再战
2014/07/15 DOTA
python3.5仿微软计算器程序
2020/03/30 Python
Python排序搜索基本算法之希尔排序实例分析
2017/12/09 Python
python web框架中实现原生分页
2019/09/08 Python
Pycharm最常用的快捷键及使用技巧
2020/03/05 Python
python中温度单位转换的实例方法
2020/12/27 Python
使用css3实现超炫的loading加载动画效果
2014/05/07 HTML / CSS
html5指南-5.使用web storage存储键值对的数据
2013/01/07 HTML / CSS
基于Modernizr 让网站进行优雅降级的分析
2013/04/21 HTML / CSS
给物业的表扬信
2014/01/21 职场文书
奥巴马胜选演讲稿
2014/05/15 职场文书
大学生学雷锋活动总结
2014/06/26 职场文书
机动车登记业务委托书
2014/10/08 职场文书
2015年学校少先队工作总结
2015/07/20 职场文书
初一语文教学反思
2016/03/03 职场文书
CSS3 菱形拼图实现只旋转div 背景图片不旋转功能
2021/03/30 HTML / CSS
2022微信温控新功能上线
2022/05/09 数码科技