Python绘制股票移动均线的实例

作者:微岩 时间:2023-07-15 10:31:06 

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绘制股票移动均线的实例

来源:https://blog.csdn.net/matrix_laboratory/article/details/50700018

标签:Python,股票,移动,均线
0
投稿

猜你喜欢

  • 一个非常有代表性的javascript简易拖动类

    2009-05-25 12:44:00
  • 超级链接中MailTo的语法

    2008-08-29 13:00:00
  • CentOS 7安装Mysql并设置开机自启动的方法

    2024-01-27 05:32:47
  • 通过实例解析python subprocess模块原理及用法

    2022-03-26 06:21:51
  • python的格式化输出(format,%)实例详解

    2022-03-20 04:17:57
  • Python 中 Shutil 模块详情

    2022-12-20 15:25:58
  • perl文件读取的几种处理方式小结

    2023-03-03 20:36:43
  • sqlserver建立新用户及关联数据库教程

    2024-01-23 10:09:53
  • 深入浅析Python 中的sklearn模型选择

    2023-05-15 19:12:00
  • SQLServer中JSON文档型数据的查询问题解决

    2024-01-19 00:41:49
  • Mysql允许外网访问设置步骤

    2024-01-26 18:31:17
  • python数据分析之员工个人信息可视化

    2023-08-05 02:32:26
  • python中的插入排序的简单用法

    2023-09-30 07:57:51
  • Python phone模块获取手机号归属地 区号 运营商等信息demo

    2023-10-09 10:36:08
  • Pandas对数值进行分箱操作的4种方法总结

    2023-08-09 02:52:04
  • 目前最全的浏览器/CSS选择器兼容性总结(包括Safari 4 beta)

    2009-02-26 15:26:00
  • Python matplotlib绘图建立画布及坐标系

    2021-03-28 02:56:07
  • Python Pandas高级教程之时间处理

    2021-08-12 14:04:49
  • 保护SQL服务器的安全 用户识别问题

    2008-12-24 15:26:00
  • SQL Server远程定时备份数据库脚本分享

    2024-01-24 12:20:31
  • asp之家 网络编程 m.aspxhome.com