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í)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python新手學(xué)習(xí)過(guò)程記錄之基礎(chǔ)環(huán)境:環(huán)境變量、版本區(qū)分、虛擬環(huán)境
剛開(kāi)始接觸Python開(kāi)發(fā)語(yǔ)言,可能就會(huì)遇到一些棘手的問(wèn)題,比如電腦上不知不覺(jué)已經(jīng)安裝了多個(gè)python版本,python3.8/3.10/3.11,甚至一些軟件中也集成有python解釋器;那么我編寫(xiě)的python代碼,到底是使用哪個(gè)解釋器在執(zhí)行?我通過(guò)pip包管理工具安裝的依賴包到底在那個(gè)地方2024-05-05Python解析json時(shí)提示“string indices must be integers”問(wèn)題解決方法
這篇文章主要介紹了Python解析json時(shí)提示“string indices must be integers”問(wèn)題解決方法,結(jié)合實(shí)例形式分析了Python解析json字符串操作規(guī)范與相關(guān)使用技巧,需要的朋友可以參考下2019-07-07基于Python的網(wǎng)頁(yè)自動(dòng)化工具DrissionPage的使用詳解
DrissionPage 是一個(gè)基于 python 的網(wǎng)頁(yè)自動(dòng)化工具,它既能控制瀏覽器,也能收發(fā)數(shù)據(jù)包,還能把兩者合而為一,下面就跟隨小編一起來(lái)學(xué)習(xí)一下它的具體使用吧2024-01-01tensorflow 限制顯存大小的實(shí)現(xiàn)
今天小編就為大家分享一篇tensorflow 限制顯存大小的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02基于python實(shí)現(xiàn)生成指定大小txt文檔
這篇文章主要介紹了基于python實(shí)現(xiàn)生成指定大小txt文檔,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Python 安裝setuptools和pip工具操作方法(必看)
下面小編就為大家?guī)?lái)一篇Python 安裝setuptools和pip工具操作方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05