python找不到tushare_python stock数据包tushare
TuShare是一個免費、開源的python財經數據接口包。主要實現對股票等金融數據從數據采集、清洗加工 到 數據存儲的過程,能夠為金融分析人員提供快速、整潔、和多樣的便于分析的數據,為他們在數據來源方面極大地減輕工作量,使他們更加專注于策略和模型的研究與實現上。考慮到Python pandas包在金融量化分析中體現出的優勢,TuShare返回的絕大部分的數據格式都是pandas DataFrame類型,非常便于用pandas/NumPy/Matplotlib進行數據分析和可視化。
其支持獲取的股市數據有:交易數據、投資參考數據、股票分類數據、基本面數據、龍虎榜數據、宏觀經濟數據、新聞事件數據、銀行間同業拆放利率等大類,每個大類下面又細分一些小類。
一、安裝與升級
同其他python模塊的安裝使用方法一樣,即可以通過pip、easy_install 工具包進行安裝,也可以通過源碼包進行安裝。
方式1:pip install tushare
從github上的源碼包可以看出,作者非常的勤奮,更新的速度非常快,所以也可以通過如下方法進行升級:
pip install tushare –upgrade
二、數據獲取相關
這里以最經常使用的幾個交易指標為例,做下匯總。
1、歷史數據
import tushare as ts
ts.get_hist_data('600848') #一次性獲取全部日k線數據
ts.get_hist_data('600848',start='2015-05-01',end='2015-06-18') #指定時間區間
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線數據
關于復權的概念不了解,這里略過。接下來看實時數據。
2、實時數據
獲取當天所有的行情信息,無法指定具體某一支的行情
import tushare as ts
ts.get_today_all()
歷史分筆與實時分筆(買賣盤統計):
import tushare as ts
df = ts.get_tick_data('600848',date='2014-01-09')
df.head(10)
df = ts.get_today_ticks('601333') #當天歷史分筆
df.head(10)
import tushare as ts
df = ts.get_realtime_quotes('000581') #Single stock symbol
df[['code','name','price','bid','ask','volume','amount','time']]
#symbols from a list
ts.get_realtime_quotes(['600848','000980','000981'])
#from a Series
ts.get_realtime_quotes(df['code'].tail(10)) #一次獲取10個股票的實時分筆數據
3、大盤指數
import tushare as ts
df = ts.get_index()
4、新股數據
獲取打新數據:
import tushare as ts
ts.new_stocks()
5、基本面數據
基本面數據里包含選股的很多依據指標,如:市盈率、市凈率、每股收益、凈利潤、季報、應收賬款周轉率、凈利潤增長率(%)、流動比率、速動比率、現金流量比率等。
import tushare as ts
ts.get_stock_basics()
#獲取2015年第1季度的業績報表數據
ts.get_report_data(2015,1)
#獲取2015年第1季度的盈利能力數據
ts.get_profit_data(2015,1)
#獲取2015年第1季度的營運能力數據
ts.get_operation_data(2015,1)
#獲取2015年第1季度的成長能力數據
ts.get_growth_data(2015,1)
#獲取2015年第1季度的償債能力數據
ts.get_debtpaying_data(2015,1)
#獲取2015年第1季度的現金流量數據
ts.get_cashflow_data(2015,1)
三、數據存儲
tushare自身提供了常用的數據保存格式:csv格式、excel格式、HDF5文件格式、JSON格式、mysql關系數據庫、nosql數據庫。
1、to_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'])
某些時候,可能需要將一些同類數據保存在一個大文件中,這時候就需要將數據追加在同一個文件里,簡單舉例如下:
import tushare as ts
import os
filename = 'c:/day/bigfile.csv'
for code in ['000875', '600848', '000981']:
df = ts.get_hist_data(code)
if os.path.exists(filename):
df.to_csv(filename, mode='a', header=None)
else:
df.to_csv(filename)
2、to_excel方法
import tushare as ts
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)
3、to_hdf方法
import tushare as ts
df = ts.get_hist_data('000875')
df.to_hdf('c:/day/hdf.h5','000875')
或
import tushare as ts
df = ts.get_hist_data('000875')
store = HDFStore('c:/day/store.h5')
store['000875'] = df
store.close()
4、to_json方法
import tushare as ts
df = ts.get_hist_data('000875')
df.to_json('c:/day/000875.json',orient='records')
#或者直接使用
print df.to_json(orient='records')
5、to_sql方法
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')
#存入數據庫
df.to_sql('tick_data',engine)
#追加數據到現有表
#df.to_sql('tick_data',engine,if_exists='append')
如下圖:
5、寫入mongodb
通過官方的示例來看,并沒有直接提供寫入mongodb的方法,不過mongodb支持json格式的輸入,這里“曲線救國 ” 下:
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')))
四、數據繪圖
上面都是拾人牙慧的東西,這里來一點點干貨。由 tushare 處理輸出的格式已經經過整形,所以可以結合pandas模塊可以很好的進行匯圖,如下:
import tushare as ts
import pandas as pd
df=ts.get_hist_data('600415',start='2015-04-01',end='2015-06-18')
# 所有的結果匯圖
df.plot()
# 只將stock最高值進行匯圖
df.high.plot()
# 指定繪圖的四個量,并指定線條顏色
with pd.plot_params.use('x_compat', True):
df.open.plot(color='g')
df.close.plot(color='y')
df.high.plot(color='r')
df.low.plot(color='b')
# 指定繪圖的長寬尺度及背景網格
with pd.plot_params.use('x_compat', True):
df.high.plot(color='r',figsize=(10,4),grid='on')
df.low.plot(color='b',figsize=(10,4),grid='on')
上面繪制了四個圖,這里只選取第四張圖具體可以看下效果:
默認上面的方法,只會輸出圖片,無法保存圖片,所以可以通過matplotlib模塊的savefig函數保存圖片到指定的位置,代碼如下:
import matplotlib
import tushare as ts
import pandas as pd
fig = matplotlib.pyplot.gcf()
df=ts.get_hist_data('600415',start='2015-04-01',end='2015-06-18')
with pd.plot_params.use('x_compat', True):
df.high.plot(color='r',figsize=(10,4),grid='on')
df.low.plot(color='b',figsize=(10,4),grid='on')
fig.savefig('F:/graph.png')
matplotlib模塊繪圖部分可以參看如下頁面:
總結
以上是生活随笔為你收集整理的python找不到tushare_python stock数据包tushare的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 数据库连接实例,Java连接各
- 下一篇: 如何在使用摩托罗拉上的RSS阅读器应用进