摘要:本例以數(shù)據(jù)庫為代表,展示將獲取到的股票數(shù)據(jù)存入數(shù)據(jù)庫的方法其他類型數(shù)據(jù)庫請(qǐng)參考官網(wǎng)文檔的部分。存入數(shù)據(jù)庫追加數(shù)據(jù)到現(xiàn)有表財(cái)經(jīng)數(shù)據(jù)接口包的使用存入財(cái)經(jīng)數(shù)據(jù)接口包的使用
安裝TuShare
方式1:pip install tushare 方式2:訪問https://pypi.python.org/pypi/tushare/下載安裝 方式3:將源代碼下載到本地python setup.py install
安裝lxml
詳細(xì)信息看 百度:https://jingyan.baidu.com/art...
更多可以看:http://tushare.org/
為防止百度404把百度的說明copy了過來
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ù) Python財(cái)經(jīng)數(shù)據(jù)接口包TuShare的使用 獲取歷史分筆數(shù)據(jù) df = ts.get_tick_data("000756","2015-03-27") df.head(10)
Python財(cái)經(jīng)數(shù)據(jù)接口包TuShare的使用
獲取實(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í)間
END
獲取其他數(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è)績報(bào)告(主表) #獲取2014年第3季度的業(yè)績報(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) 成長能力數(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ù)
END
數(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:[email protected]/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") Python財(cái)經(jīng)數(shù)據(jù)接口包TuShare的使用 存入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"))) Python財(cái)經(jīng)數(shù)據(jù)接口包TuShare的使用
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/44475.html
摘要:數(shù)據(jù)分析是的一大應(yīng)用領(lǐng)域。這其中,又有相當(dāng)一部分人是涉及金融相關(guān)行業(yè),有從業(yè)人員,有學(xué)生,還有對(duì)此具有興趣的愛好者。那么,想要學(xué)習(xí)用做數(shù)據(jù)分析該從何入手既然是數(shù)據(jù)分析,首先,你得有數(shù)據(jù)。今天就來介紹一個(gè)很好用的財(cái)經(jīng)數(shù)據(jù)包 數(shù)據(jù)分析是 Python 的一大應(yīng)用領(lǐng)域。據(jù)我所知,本教室的讀者中有不少學(xué)習(xí) Python 就是為了在工作中能用它分析數(shù)據(jù)。這其中,又有相當(dāng)一部分人是涉及金融相關(guān)行業(yè)...
摘要:作者,數(shù)據(jù)游戲優(yōu)勝隊(duì)伍成員前陣子報(bào)名參加了數(shù)據(jù)游戲比賽,題目是預(yù)測(cè)月號(hào)星期三招商銀行的股價(jià),截止時(shí)間是在月號(hào)星期天。 作者:Max,「數(shù)據(jù)游戲」優(yōu)勝隊(duì)伍成員 前陣子報(bào)名參加了「數(shù)據(jù)游戲」比賽,題目是預(yù)測(cè)5月15號(hào)(星期三)招商銀行的股價(jià),截止時(shí)間是在5月12號(hào)(星期天)。在本次預(yù)測(cè)中,我用到的是嶺回歸。 嶺回歸 嶺回歸是回歸的一種,它解決回歸中重大疑難問題:排除多重共線性,進(jìn)行變量的選...
摘要:作者,數(shù)據(jù)游戲優(yōu)勝隊(duì)伍成員前陣子報(bào)名參加了數(shù)據(jù)游戲比賽,題目是預(yù)測(cè)月號(hào)星期三招商銀行的股價(jià),截止時(shí)間是在月號(hào)星期天。 作者:Max,「數(shù)據(jù)游戲」優(yōu)勝隊(duì)伍成員 前陣子報(bào)名參加了「數(shù)據(jù)游戲」比賽,題目是預(yù)測(cè)5月15號(hào)(星期三)招商銀行的股價(jià),截止時(shí)間是在5月12號(hào)(星期天)。在本次預(yù)測(cè)中,我用到的是嶺回歸。 嶺回歸 嶺回歸是回歸的一種,它解決回歸中重大疑難問題:排除多重共線性,進(jìn)行變量的選...
目錄Numpy簡(jiǎn)介Numpy操作集合1、不同維度數(shù)據(jù)的表示1.1 一維數(shù)據(jù)的表示1.2 二維數(shù)據(jù)的表示1.3 三維數(shù)據(jù)的表示2、 為什么要使用Numpy2.1、Numpy的ndarray具有廣播功能2.2 Numpy數(shù)組的性能比Python原生數(shù)據(jù)類型高3 ndarray的屬性和基本操作3.1 ndarray的基本屬性3.2 ndarray元素類型3.3 創(chuàng)建ndarray的方式3.4 ndarr...
摘要:再平衡策略基本就是以固定收益為標(biāo)準(zhǔn),圍繞其做波動(dòng),而波動(dòng)的影響就是股市的漲跌。 賺錢是個(gè)俗氣的話題,但又是人人都繞不開的事情。我今天來 科學(xué) 地觸碰下這個(gè)話題。 談賺錢,就會(huì)談到理財(cái)、投資,談到炒股。有這樣一個(gè)笑話: 問:如何成為百萬富翁? 答:帶一千萬進(jìn)入股市。 那么你有沒有炒過股?有沒有虧過錢? 股市雖然是個(gè)充滿造富神話的地方,但對(duì)于大部分參與者來說,風(fēng)險(xiǎn)都是極高的,所謂 七...
閱讀 4736·2021-11-18 13:23
閱讀 910·2021-09-22 15:24
閱讀 1934·2021-09-06 15:00
閱讀 2638·2021-09-03 10:30
閱讀 1294·2021-09-02 15:15
閱讀 2084·2019-08-30 15:54
閱讀 3041·2019-08-30 15:44
閱讀 1462·2019-08-29 15:12