1. 程式人生 > >Anaconda 環境下 對Tushare進行測試

Anaconda 環境下 對Tushare進行測試

Anaconda下安裝好Tushare後,就搬一搬Tushare的磚吧(搬磚的內容是在 https://jingyan.baidu.com/article/3065b3b68d7fb5becff8a494.html 進行學習和總結的!)

一 歷史交易資料獲取

import tushare as ts
df = ts.get_hist_data('601998') #獲取股票程式碼為601998的歷史資料
ts.get_hist_data('601998',ktype='5')
ts.get_hist_data('cyb') df.tail(5)

(獲取指數k線資料 sz sh hs300 sz50 zxb cyb

 資料框列標題代表含義 open:開盤價 high:最高價 close:收盤價 low:最低價 volume:成交量 price_change:價格變動 p_change:漲跌幅 ma5:最近5日收盤價平均值

ma10:最近10日收盤價平均值 ma20:最近20日收盤價平均值 v_ma5:5日均量 v_ma10:10日均量 v_ma20:20日均量)

 

 

二 獲取歷史分筆資料
df = ts.get_hist_data('601998','2018-05-10')
df.head(10) #(獲取前十條資料)

 

 

三 獲取實時單筆資料
df = ts.get_realtime_quotes('601998')
result = df[['code','name','amount','b1_v','high']]
print(result)

 


(返回值 name:股票名字 :open:今日開盤價 pre_close:昨日收盤價 price:當前價格 high:今日最高價 low:今日最低價 bid:競買價,即“買一”報價 ask:競賣價,即“賣一”報價 volumn:成交量 maybe you need do volumn/100 amount:成交金額(元 CNY) b1_v:委買一(筆數 bid volume) b1_p,委買一(價格 bid price) b2_v:“買二” b2_p:“買二” b3_v:“買三” b3_p:“買三” b4_v:“買四” b4_p:“買四” b5_v:“買五” b5_p:“買五” a1_v:委賣一(筆數 ask volume) a1_p:委賣一(價格 ask price) date:日期 time:時間)

 

 

 四 獲取其他資料
df = ts.get_hs300s() #獲取滬深300成份股及權重
df.head(10)#提取前十條資料

 (行業分類:ts.get_industry_classified()
概念分類:ts.get_concept_classified()
地域分類:ts.get_area_classified()
中小板分類:ts.get_sme_classified()
創業板分類:ts.get_gem_classified()
風險警示板分類:ts.get_st_classified()
滬深300成份股及權重:ts.get_hs300s()
上證50成份股:ts.get_sz50s() )

 

五 基本面資料
df = ts.get_report_data(2015,4) #獲取2015年4月的業績報告
df.head(10)

 

(滬深股票列表(基礎資料,滬深所有股票情況) ts.get_stock_basics()

業績報告(主表) ts.get_report_data(年,月)

盈利能力資料 ts.get_profit_data(年,月)

營運能力資料 ts.get_operation_data(年,月)

成長能力資料 ts.get_growth_data(年,月)

償債能力資料 ts.get_debtpaying_data(年,月)

現金流量資料 ts.get_cashflow_data(年,月)  )

 

六 資料儲存
# 1.csv 格式
import tushare as ts
df = ts.get_hist_data('601998')
df.to_csv('C:/Users/lalala/Desktop/Tomato/Hundreds of millions/2018.10.17/c.csv',columns=['open','high','low','close']) # colunms 儲存指定的列索引

# 2.儲存excel格式
import tushare as ts
df = ts.get_hist_data('601998')
df.to_excel('C:/Users/lalala/Desktop/Tomato/Hundreds of millions/2018.10.17/e.xlsx',startrow=3,startcol=6) #startrow=3 startcol=6 是指從第三行第六列開始插入資料

# 3.儲存成json格式
import tushare as ts
df = ts.get_hist_data('601998')
df.to_json('C:/Users/lalala/Desktop/Tomato/Hundreds of millions/2018.10.17/j.json')

# 4.儲存成HDF5格式
import tushare as ts
df = ts.get_hist_data('601998')
df.to_hdf('C:/Users/lalala/Desktop/Tomato/Hundreds of millions/2018.10.17/j.h5','601998') # 注意在儲存hdf的格式時 ,要在路徑後加上查詢的股票程式碼