python股票分析系列_Python股票分析系列——基础股票数据操作(二).p4
該系列視頻已經(jīng)搬運(yùn)至bilibili:?點(diǎn)擊查看
歡迎來(lái)到Python for Finance教程系列的第4部分。在本教程中,我們將基于Adj Close列創(chuàng)建燭臺(tái)/ OHLC圖,這將允許我介紹重新采樣和其他一些數(shù)據(jù)可視化概念。
名為燭臺(tái)圖的OHLC圖表是一種將開(kāi)盤(pán)價(jià)Open,最高價(jià)High,最低價(jià)Low和收盤(pán)價(jià)Close數(shù)據(jù)全部集中在一個(gè)很好的格式中的圖表。另外,它使得漂亮的顏色,并記住我告訴你關(guān)于美觀的圖表?
在之前的教程中已經(jīng)涉及到了這一點(diǎn):
importdatetime as dtimportmatplotlib.pyplot as pltfrom matplotlib importstyleimportpandas as pdimportpandas_datareader.data as web
style.use('ggplot')
df= pd.read_csv('tsla.csv', parse_dates=True, index_col=0)
不幸的是,即使創(chuàng)建OHLC數(shù)據(jù),直接從Pandas制作燭臺(tái)圖也不是內(nèi)置的。未來(lái)我確信這個(gè)圖表類(lèi)型將會(huì)被提供,但現(xiàn)在不是。沒(méi)關(guān)系,we can make it!首先,我們需要import兩個(gè)新的庫(kù):
from matplotlib.finance importcandlestick_ohlcimport matplotlib.dates as mdates
第一個(gè)導(dǎo)入是來(lái)自matplotlib的OHLC圖形類(lèi)型,第二個(gè)導(dǎo)入是特殊的mdates類(lèi)型,它大多只是一個(gè)屁股疼痛,但這是matplotlib圖形的日期類(lèi)型。pandas會(huì)自動(dòng)為你處理,但就像我說(shuō)的那樣,我們沒(méi)有燭臺(tái)的奢侈品。
首先,我們需要適當(dāng)?shù)腛HLC數(shù)據(jù)。我們目前的數(shù)據(jù)確實(shí)有OHLC的價(jià)值,除非我錯(cuò)了,特斯拉從來(lái)沒(méi)有分裂過(guò),但是你永遠(yuǎn)不會(huì)這么幸運(yùn)。因此,我們將創(chuàng)建我們自己的OHLC數(shù)據(jù),這也將允許我們展示另一個(gè)來(lái)自Pandas的數(shù)據(jù)轉(zhuǎn)換:
df_ohlc = df['Adj Close'].resample('10D').ohlc()
我們?cè)谶@里所做的是創(chuàng)建一個(gè)基于df ['Adj Close']列的新數(shù)據(jù)框,重新封裝10天的窗口,并且重采樣是一個(gè)ohlc(開(kāi)高低關(guān)閉)。我們也可以用.mean()或.sum()做10天的平均值或10天的總和。請(qǐng)記住,這10天的平均值是10天的平均值,而不是平均值。由于我們的數(shù)據(jù)是每日數(shù)據(jù),因此將其重新采樣為10天的數(shù)據(jù)會(huì)顯著縮小數(shù)據(jù)的大小。這是你可以如何規(guī)范化多個(gè)數(shù)據(jù)集。有時(shí),您可能會(huì)在每個(gè)月的一個(gè)月中記錄一次每月記錄的數(shù)據(jù),每個(gè)月末記錄的其他數(shù)據(jù),以及最終每周記錄一些數(shù)據(jù)。您可以每個(gè)月對(duì)該數(shù)據(jù)幀重新采樣到月末,并有效地將其標(biāo)準(zhǔn)化!如果你喜歡的話,這是更高級(jí)的熊貓功能,你可以從熊貓系列中了解更多。
我們想要繪制燭臺(tái)數(shù)據(jù)以及體積數(shù)據(jù)。我們不必重新采樣數(shù)據(jù),但我們應(yīng)該,因?yàn)樗c我們的10D定價(jià)數(shù)據(jù)相比太細(xì)致。
df_volume = df['Volume'].resample('10D').sum()
我們?cè)谶@里使用金額,因?yàn)槲覀兇_實(shí)想知道在這10天內(nèi)交易的總量,但您也可以使用平均值。現(xiàn)在如果我們這樣做:
print(df_ohlc.head())
我們得到:
open high low close
Date2010-06-29 23.889999 23.889999 15.800000 17.459999
2010-07-09 17.400000 20.639999 17.049999 20.639999
2010-07-19 21.910000 21.910000 20.219999 20.719999
2010-07-29 20.350000 21.950001 19.590000 19.590000
2010-08-08 19.600000 19.600000 17.600000 19.150000
這是預(yù)期的,但是,我們現(xiàn)在要將這些信息移動(dòng)到matplotlib中,并將日期轉(zhuǎn)換為mdates版本。由于我們只是要在Matplotlib中繪制列,所以我們實(shí)際上不希望日期成為索引,所以我們可以這樣做:
df_ohlc = df_ohlc.reset_index()
現(xiàn)在的日期只是一個(gè)普通的專欄。接下來(lái),我們要轉(zhuǎn)換它:
df_ohlc ['Date'] = df_ohlc ['Date']。map(mdates.date2num)
現(xiàn)在我們要設(shè)置這個(gè)數(shù)字:
fig =plt.figure()
ax1= plt.subplot2grid((6,1),(0,0),rowspan = 5,colspan = 1)
ax2= plt.subplot2grid((6,1),(5,0),rowspan = 1,colspan = 1,sharex =ax1)
ax1.xaxis_date()
除了ax1.xaxis_date()之外,您已經(jīng)看到過(guò)的所有內(nèi)容。這對(duì)我們來(lái)說(shuō)就是將軸從原始的生成號(hào)碼轉(zhuǎn)換為日期。
現(xiàn)在我們可以繪制燭臺(tái)圖:
candlestick_ohlc(ax1,df_ohlc.values,width = 2,colorup ='g')
然后做量:
ax2.fill_between(df_volume.index.map(mdates.date2num),df_volume.values,0)
fill_between函數(shù)將繪制x,y,然后填充/之間的內(nèi)容。在我們的例子中,我們選擇0。
plt.show()
完整代碼:
importdatetime as dtimportmatplotlib.pyplot as pltfrom matplotlib importstylefrom matplotlib.finance importcandlestick_ohlcimportmatplotlib.dates as mdatesimportpandas as pdimportpandas_datareader.data as web
style.use('ggplot')
df= pd.read_csv('tsla.csv', parse_dates=True, index_col=0)
df_ohlc= df['Adj Close'].resample('10D').ohlc()
df_volume= df['Volume'].resample('10D').sum()
df_ohlc.reset_index(inplace=True)
df_ohlc['Date'] = df_ohlc['Date'].map(mdates.date2num)
ax1= plt.subplot2grid((6,1), (0,0), rowspan=5, colspan=1)
ax2= plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1, sharex=ax1)
ax1.xaxis_date()
candlestick_ohlc(ax1, df_ohlc.values, width=5, colorup='g')
ax2.fill_between(df_volume.index.map(mdates.date2num), df_volume.values, 0)
plt.show()
在接下來(lái)的幾個(gè)教程中,我們將留下可視化位,稍微討論一下獲取數(shù)據(jù)和處理數(shù)據(jù)。
總結(jié)
以上是生活随笔為你收集整理的python股票分析系列_Python股票分析系列——基础股票数据操作(二).p4的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: K8S—理论知识
- 下一篇: 将 CARLA egg 文件安装到 Py