Python繪制股票移動(dòng)均線的實(shí)例
1. 前沿
移動(dòng)均線是股票最進(jìn)本的指標(biāo),本文采用numpy.convolve計(jì)算股票的移動(dòng)均線
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.
計(jì)算公式:

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. 移動(dòng)均線計(jì)算
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繪制股票移動(dòng)均線的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Django表單提交后實(shí)現(xiàn)獲取相同name的不同value值
這篇文章主要介紹了Django表單提交后實(shí)現(xiàn)獲取相同name的不同value值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
python中的Numpy二維數(shù)組遍歷與二維數(shù)組切片后遍歷效率比較
這篇文章主要介紹了python中的Numpy二維數(shù)組遍歷與二維數(shù)組切片后遍歷效率比較,在python-numpy使用中,可以用雙層?for循環(huán)對(duì)數(shù)組元素進(jìn)行訪問,也可以切片成每一行后進(jìn)行一維數(shù)組的遍歷,下面小編擊來舉例介紹吧,需要的朋友可以參考一下2022-03-03
對(duì)python中的six.moves模塊的下載函數(shù)urlretrieve詳解
今天小編就為大家分享一篇對(duì)python中的six.moves模塊的下載函數(shù)urlretrieve詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python 調(diào)用C++封裝的進(jìn)一步探索交流
這篇文章主要介紹了Python 調(diào)用C++封裝的進(jìn)一步探索交流,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03

