1. 程式人生 > >Python財經資料介面包TuShare的使用

Python財經資料介面包TuShare的使用

安裝TuShare

方式1:pip install tushare

方式2:訪問https://pypi.python.org/pypi/tushare/下載安裝

方式3:將原始碼下載到本地python setup.py install

 

升級TuShare

1、先檢視本地與線上的版本版本號:

pip search tushare

2、升級TuShare:

pip install tushare --upgrade

 

確認安裝成功

import tushare as ts

print ts.__version__

 

獲取歷史交易資料

import tushare as ts

df = ts.get_hist_data('600848')

ts.get_hist_data('600848',ktype='W') #獲取周k線資料

ts.get_hist_data('600848',ktype='M') #獲取月k線資料

ts.get_hist_data('600848',ktype='5') #獲取5分鐘k線資料

ts.get_hist_data('600848',ktype='15') #獲取15分鐘k線資料

ts.get_hist_data('600848',ktype='30') #獲取30分鐘k線資料

ts.get_hist_data('600848',ktype='60') #獲取60分鐘k線資料

ts.get_hist_data('sh')#獲取上證指數k線資料,其它引數與個股一致,下同

ts.get_hist_data('sz')#獲取深圳成指k線資料 ts.get_hist_data('hs300')#獲取滬深300指數k線資料

ts.get_hist_data('sz50')#獲取上證50指數k線資料

ts.get_hist_data('zxb')#獲取中小板指數k線資料

ts.get_hist_data('cyb')#獲取創業板指數k線資料

 

獲取歷史分筆資料

df = ts.get_tick_data('000756','2015-03-27')

df.head(10)

 

獲取實時分筆資料

df = ts.get_realtime_quotes('000581') 

 

print df[['code','name','price','bid','ask','volume','amount','time']]

 

返回值說明:

0:name,股票名字

1:open,今日開盤價

2:pre_close,昨日收盤價

3:price,當前價格

4:high,今日最高價

5:low,今日最低價

6:bid,競買價,即“買一”報價

7:ask,競賣價,即“賣一”報價

8:volumn,成交量 maybe you need do volumn/100

9:amount,成交金額(元 CNY)

10:b1_v,委買一(筆數 bid volume)

11:b1_p,委買一(價格 bid price)

12:b2_v,“買二”

13:b2_p,“買二”

14:b3_v,“買三”

15:b3_p,“買三”

16:b4_v,“買四”

17:b4_p,“買四”

18:b5_v,“買五”

19:b5_p,“買五”

20:a1_v,委賣一(筆數 ask volume)

21:a1_p,委賣一(價格 ask price)

...

30:date,日期

31:time,時間

 

獲取其他資料

  • 股票分數資料 

    行業分類

    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()

  • 基本面資料

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

    ts.get_stock_basics()

    業績報告(主表)

    #獲取2014年第3季度的業績報表資料

    ts.get_report_data(2014,3)

    盈利能力資料

    #獲取2014年第3季度的盈利能力資料

    ts.get_profit_data(2014,3)

    營運能力資料

    #獲取2014年第3季度的營運能力資料

    ts.get_operation_data(2014,3)

    成長能力資料

    ts.get_growth_data(2014,3)

    償債能力資料

    ts.get_debtpaying_data(2014,3)

    現金流量資料

    ts.get_cashflow_data(2014,3)

  • 巨集觀經濟資料

    目前巨集觀經濟資料主要包括以下方面:

    金融資訊資料

    國民經濟資料

    價格指數資料

    景氣指數資料

    對外經濟貿易資料

    END

資料儲存

  • 儲存為csv格式

    import tushare as ts

    df = ts.get_hist_data('000875')#直接儲存

    df.to_csv('c:/day/000875.csv')#選擇儲存

    df.to_csv('c:/day/000875.csv',columns=['open','high','low','close'])

    儲存為Excel格式

    df = ts.get_hist_data('000875')#直接儲存

    df.to_excel('c:/day/000875.xlsx')#設定資料位置(從第3行,第6列開始插入資料) 

    df.to_excel('c:/day/000875.xlsx', startrow=2,startcol=5)

    儲存為HDF5檔案格式

    df = ts.get_hist_data('000875')

    df.to_hdf('c:/day/hdf.h5','000875') 

    儲存為JSON格式

    df = ts.get_hist_data('000875')

    df.to_json('c:/day/000875.json',orient='records')

  • MySQL資料庫

    pandas提供了將資料便捷存入關係型資料庫的方法,在新版的pandas中,主要是已sqlalchemy方式與資料建立連線,支援MySQL、Postgresql、Oracle、MS SQLServer、SQLite等主流資料庫。本例以MySQL資料庫為代表,展示將獲取到的股票資料存入資料庫的方法,其他型別資料庫請參考sqlalchemy官網文件的create_engine部分。  

    from sqlalchemy import create_engine 

    import tushare as ts

    df = ts.get_tick_data('600848',date='2014-12-22')

    engine = create_engine('mysql://user:[email protected]/db_name?charset=utf8')

    #存入資料庫

    df.to_sql('tick_data',engine)

    #追加資料到現有表

    #df.to_sql('tick_data',engine,if_exists='append')

     

存入MongoDB

 

import pymongo

import json

conn = pymongo.Connection('127.0.0.1', port=27017)

df = ts.get_tick_data('600848',date='2014-12-22')

conn.db.tickdata.insert(json.loads(df.to_json(orient='records')))

  原文來自:https://jingyan.baidu.com/article/3065b3b68d7fb5becff8a494.html