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实现从字符串中找出字符1的位置以及个数的方法
Aug 25 Python
python元组操作实例解析
Sep 23 Python
python机器学习库常用汇总
Nov 15 Python
理解python中生成器用法
Dec 20 Python
Python wxPython库消息对话框MessageDialog用法示例
Sep 03 Python
python实现从文件中读取数据并绘制成 x y 轴图形的方法
Oct 14 Python
python实现对任意大小图片均匀切割的示例
Dec 05 Python
Python线性拟合实现函数与用法示例
Dec 13 Python
python打印异常信息的两种实现方式
Dec 24 Python
快速创建python 虚拟环境
Nov 28 Python
python基于Kivy写一个图形桌面时钟程序
Jan 28 Python
Python函数式编程中itertools模块详解
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
简单采集了yahoo的一些数据
2007/02/14 PHP
基于PHP编程注意事项的小结
2013/04/27 PHP
jQuery向下滚动即时加载内容实现的瀑布流效果
2016/01/07 PHP
php使用str_replace替换多维数组的实现方法分析
2017/06/15 PHP
PHP错误提示It is not safe to rely on the system……的解决方法
2019/03/25 PHP
php数组函数array_push()、array_pop()及array_shift()简单用法示例
2020/01/26 PHP
JavaScript 弹出窗体点击按钮返回选择数据的实现
2010/04/01 Javascript
JS实现仿雅虎首页快捷登录入口及导航模块效果
2015/09/19 Javascript
js正则表达式replace替换变量方法
2016/05/21 Javascript
js中获取时间new Date()的全面介绍
2016/06/20 Javascript
js检查是否关闭浏览器的方法
2016/08/02 Javascript
ionic在开发ios系统微信时键盘挡住输入框的解决方法(键盘弹出问题)
2016/09/06 Javascript
JavaScript每天必学之基础知识
2016/09/17 Javascript
微信js-sdk分享功能接口常用逻辑封装示例
2016/10/13 Javascript
微信小程序 教程之注册页面
2016/10/17 Javascript
React Native基础入门之初步使用Flexbox布局
2018/07/02 Javascript
VUE项目中加载已保存的笔记实例方法
2019/09/14 Javascript
VUE实现图片验证码功能
2020/11/18 Javascript
toString.call()通用的判断数据类型方法示例
2020/08/28 Javascript
Python模块搜索概念介绍及模块安装方法介绍
2015/06/03 Python
Python实现两款计算器功能示例
2017/12/19 Python
浅谈Python中的zip()与*zip()函数详解
2018/02/24 Python
Python Numpy库安装与基本操作示例
2019/01/08 Python
selenium+python自动化测试之环境搭建
2019/01/23 Python
JupyterNotebook 输出窗口的显示效果调整实现
2020/09/22 Python
CSS3中animation实现流光按钮效果
2020/12/21 HTML / CSS
耐克中国官方商城:Nike中国
2018/10/18 全球购物
卫校毕业生自我鉴定
2013/10/31 职场文书
酒店开业庆典主持词
2014/03/21 职场文书
社区食品安全实施方案
2014/03/28 职场文书
2015年班级元旦晚会活动总结
2014/11/28 职场文书
雷峰塔导游词
2015/02/09 职场文书
幼师大班个人总结
2015/02/13 职场文书
Vue2.0搭建脚手架
2022/03/13 Vue.js
安装Ruby和 Rails的详细步骤
2022/04/19 Ruby
python缺失值填充方法示例代码
2022/12/24 Python