Python股票數(shù)據(jù)分析(tushare/seaborn)

python版本:3.4
最近在學(xué)習(xí)基于python的股票數(shù)據(jù)分析,其中主要用到了tushare和seaborn。tushare是一款財經(jīng)類數(shù)據(jù)接口包,國內(nèi)的股票數(shù)據(jù)還是比較全的,官網(wǎng)地址:http://tushare.waditu.com/index.html#id5 。seaborn則是一款繪圖庫,通過seaborn可以輕松地畫出簡潔漂亮的圖表,而且?guī)毂旧砭哂幸欢ǖ慕y(tǒng)計功能。
導(dǎo)入的模塊:
import matplotlib.pyplot as plt
import seaborn as sns
import seaborn.linearmodels as snsl

from datetime import datetime
import tushare as ts
代碼部分:
股票收盤價走勢曲線
<code>sns.set_style("whitegrid")</code>
end = datetime.today() #開始時間結(jié)束時間,選取最近一年的數(shù)據(jù)
start = datetime(end.year-1,end.month,end.day)
end = str(end)[0:10]
start = str(start)[0:10]

stock = ts.get_hist_data('300104',start,end)#選取一支股票
stock['close'].plot(legend=True ,figsize=(10,4))
<code>plt.show()</code>

股票日線

同理,可以做出5日均線、10日均線以及20日均線
stock[['close','ma5','ma10','ma20']].plot(legend=True ,figsize=(10,4))

日線、5日均線、10日均線、20日均線

股票每日漲跌幅度
stock['Daily Return'] = stock['close'].pct_change()
stock['Daily Return'].plot(legend=True,figsize=(10,4))

每日漲跌幅

核密度估計
sns.kdeplot(stock['Daily Return'].dropna())

核密度估計

核密度估計+統(tǒng)計柱狀圖
sns.distplot(stock['Daily Return'].dropna(),bins=100)

核密度+柱狀圖

兩支股票的皮爾森相關(guān)系數(shù)
sns.jointplot(stock['Daily Return'],stock['Daily Return'],alpha=0.2)

皮爾森相關(guān)系數(shù)

多只股票相關(guān)性計算
<code>stock_lis=['300113','300343','300295','300315`] #隨便選取了四支互聯(lián)網(wǎng)相關(guān)的股票</code>
<code>df=pd.DataFrame()</code>

     closing_df = ts.get_hist_data(stock,start,end)['close']
     df = df.join(pd.DataFrame({stock:closing_df}),how='outer')```
```tech_rets = df.pct_change()```
<code>snsl.corrplot(tech_rets.dropna())</code>


![相關(guān)性](http://upload-images.jianshu.io/upload_images/2166775-e37e7396d6b405b9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

簡單地計算股票的收益與風(fēng)險,衡量股票收益與風(fēng)險的數(shù)值分別為股票漲跌的平均值以及標(biāo)準(zhǔn)差,平均值為正則說明收益是正的,標(biāo)準(zhǔn)差越大則說明股票波動大,風(fēng)險也大。
```rets = tech_rets.dropna()```
<code>plt.scatter(rets.mean(),rets.std())</code>
```plt.xlabel('Excepted Return')```
<code>plt.ylabel('Risk')</code>
```for label,x,y in zip(rets.columns,rets.mean(),rets.std()):#添加標(biāo)注
     plt.annotate(
                  label,
                  xy =(x,y),xytext=(15,15),
                  textcoords = 'offset points',
                  arrowprops = dict(arrowstyle = '-',connectionstyle = 'arc3,rad=-0.3'))```


![收益風(fēng)險](http://upload-images.jianshu.io/upload_images/2166775-2d99abea2190b8e0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容