|
TuShare是一個(gè)免費(fèi)、開源的python財(cái)經(jīng)數(shù)據(jù)接口包。主要實(shí)現(xiàn)對(duì)股票等金融數(shù)據(jù)從數(shù)據(jù)采集、清洗加工到數(shù)據(jù)存儲(chǔ)的過程,能夠?yàn)榻鹑诜治鋈藛T提供快速、整潔、和多樣的便于分析的數(shù)據(jù)。 考慮到python pandas包在金融量化分析中體現(xiàn)出的優(yōu)勢(shì),TuShare返回的絕大部分的數(shù)據(jù)格式都是pandas DataFrame類型,非常便于用pandas/NumPy/Matplotlib進(jìn)行數(shù)據(jù)分析和可視化。
工具/原料Python 2.x / 3.x pandas lxml 安裝和體驗(yàn)安裝TuShare方式1:pip install tushare 方式2:訪問https://pypi./pypi/tushare/下載安裝 方式3:將源代碼下載到本地python setup.py install 升級(jí)TuShare1、先查看本地與線上的版本版本號(hào): pip search tushare2、升級(jí)TuShare: pip install tushare --upgrade確認(rèn)安裝成功import tushare as ts print ts.__version__ 獲取歷史交易數(shù)據(jù)import tushare as ts df = ts.get_hist_data('600848') ts.get_hist_data('600848',ktype='W') #獲取周k線數(shù)據(jù) ts.get_hist_data('600848',ktype='M') #獲取月k線數(shù)據(jù) ts.get_hist_data('600848',ktype='5') #獲取5分鐘k線數(shù)據(jù) ts.get_hist_data('600848',ktype='15') #獲取15分鐘k線數(shù)據(jù) ts.get_hist_data('600848',ktype='30') #獲取30分鐘k線數(shù)據(jù) ts.get_hist_data('600848',ktype='60') #獲取60分鐘k線數(shù)據(jù) ts.get_hist_data('sh')#獲取上證指數(shù)k線數(shù)據(jù),其它參數(shù)與個(gè)股一致,下同 ts.get_hist_data('sz')#獲取深圳成指k線數(shù)據(jù) ts.get_hist_data('hs300')#獲取滬深300指數(shù)k線數(shù)據(jù) ts.get_hist_data('sz50')#獲取上證50指數(shù)k線數(shù)據(jù) ts.get_hist_data('zxb')#獲取中小板指數(shù)k線數(shù)據(jù) ts.get_hist_data('cyb')#獲取創(chuàng)業(yè)板指數(shù)k線數(shù)據(jù)
獲取歷史分筆數(shù)據(jù)df = ts.get_tick_data('000756','2015-03-27') df.head(10)
獲取實(shí)時(shí)分筆數(shù)據(jù)df = ts.get_realtime_quotes('000581') print df[['code','name','price','bid','ask','volume','amount','time']] 返回值說明: 0:name,股票名字 1:open,今日開盤價(jià) 2:pre_close,昨日收盤價(jià) 3:price,當(dāng)前價(jià)格 4:high,今日最高價(jià) 5:low,今日最低價(jià) 6:bid,競(jìng)買價(jià),即“買一”報(bào)價(jià) 7:ask,競(jìng)賣價(jià),即“賣一”報(bào)價(jià) 8:volumn,成交量 maybe you need do volumn/100 9:amount,成交金額(元 CNY) 10:b1_v,委買一(筆數(shù) bid volume) 11:b1_p,委買一(價(jià)格 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,委賣一(筆數(shù) ask volume) 21:a1_p,委賣一(價(jià)格 ask price) ... 30:date,日期 31:time,時(shí)間
獲取其他數(shù)據(jù)股票分?jǐn)?shù)數(shù)據(jù)行業(yè)分類ts.get_industry_classified() 概念分類,所有股票炒作概念,比如蘋果、特斯拉等ts.get_concept_classified() 地域分類ts.get_area_classified() 中小板分類ts.get_sme_classified() 創(chuàng)業(yè)板分類ts.get_gem_classified() 風(fēng)險(xiǎn)警示板分類ts.get_st_classified() 滬深300成份股及權(quán)重ts.get_hs300s() 上證50成份股ts.get_sz50s() 基本面數(shù)據(jù)滬深股票列表(基礎(chǔ)數(shù)據(jù),滬深所有股票情況)ts.get_stock_basics() 業(yè)績(jī)報(bào)告(主表)#獲取2014年第3季度的業(yè)績(jī)報(bào)表數(shù)據(jù) ts.get_report_data(2014,3) 盈利能力數(shù)據(jù)#獲取2014年第3季度的盈利能力數(shù)據(jù) ts.get_profit_data(2014,3) 營運(yùn)能力數(shù)據(jù)#獲取2014年第3季度的營運(yùn)能力數(shù)據(jù) ts.get_operation_data(2014,3) 成長(zhǎng)能力數(shù)據(jù)ts.get_growth_data(2014,3) 償債能力數(shù)據(jù)ts.get_debtpaying_data(2014,3) 現(xiàn)金流量數(shù)據(jù)ts.get_cashflow_data(2014,3) 宏觀經(jīng)濟(jì)數(shù)據(jù)目前宏觀經(jīng)濟(jì)數(shù)據(jù)主要包括以下方面: 金融信息數(shù)據(jù) 國民經(jīng)濟(jì)數(shù)據(jù) 價(jià)格指數(shù)數(shù)據(jù) 景氣指數(shù)數(shù)據(jù) 對(duì)外經(jīng)濟(jì)貿(mào)易數(shù)據(jù)
數(shù)據(jù)存儲(chǔ)保存為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')#設(shè)定數(shù)據(jù)位置(從第3行,第6列開始插入數(shù)據(jù)) 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數(shù)據(jù)庫pandas提供了將數(shù)據(jù)便捷存入關(guān)系型數(shù)據(jù)庫的方法,在新版的pandas中,主要是已sqlalchemy方式與數(shù)據(jù)建立連接,支持MySQL、Postgresql、Oracle、MS SQLServer、SQLite等主流數(shù)據(jù)庫。本例以MySQL數(shù)據(jù)庫為代表,展示將獲取到的股票數(shù)據(jù)存入數(shù)據(jù)庫的方法,其他類型數(shù)據(jù)庫請(qǐng)參考sqlalchemy官網(wǎng)文檔的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:passwd@127.0.0.1/db_name?charset=utf8') #存入數(shù)據(jù)庫 df.to_sql('tick_data',engine) #追加數(shù)據(jù)到現(xiàn)有表 #df.to_sql('tick_data',engine,if_exists='append')
存入MongoDBimport 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')))
|
|
|