日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python找不到tushare_python stock数据包tushare

發(fā)布時間:2023/12/10 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python找不到tushare_python stock数据包tushare 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

TuShare是一個免費、開源的python財經(jīng)數(shù)據(jù)接口包。主要實現(xiàn)對股票等金融數(shù)據(jù)從數(shù)據(jù)采集、清洗加工 到 數(shù)據(jù)存儲的過程,能夠為金融分析人員提供快速、整潔、和多樣的便于分析的數(shù)據(jù),為他們在數(shù)據(jù)來源方面極大地減輕工作量,使他們更加專注于策略和模型的研究與實現(xiàn)上。考慮到Python pandas包在金融量化分析中體現(xiàn)出的優(yōu)勢,TuShare返回的絕大部分的數(shù)據(jù)格式都是pandas DataFrame類型,非常便于用pandas/NumPy/Matplotlib進行數(shù)據(jù)分析和可視化。

其支持獲取的股市數(shù)據(jù)有:交易數(shù)據(jù)、投資參考數(shù)據(jù)、股票分類數(shù)據(jù)、基本面數(shù)據(jù)、龍虎榜數(shù)據(jù)、宏觀經(jīng)濟數(shù)據(jù)、新聞事件數(shù)據(jù)、銀行間同業(yè)拆放利率等大類,每個大類下面又細分一些小類。

一、安裝與升級

同其他python模塊的安裝使用方法一樣,即可以通過pip、easy_install 工具包進行安裝,也可以通過源碼包進行安裝。

方式1:pip install tushare

從github上的源碼包可以看出,作者非常的勤奮,更新的速度非常快,所以也可以通過如下方法進行升級:

pip install tushare –upgrade

二、數(shù)據(jù)獲取相關

這里以最經(jīng)常使用的幾個交易指標為例,做下匯總。

1、歷史數(shù)據(jù)

import tushare as ts

ts.get_hist_data('600848') #一次性獲取全部日k線數(shù)據(jù)

ts.get_hist_data('600848',start='2015-05-01',end='2015-06-18') #指定時間區(qū)間

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ù)與個股一致,下同

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ù)。

2、實時數(shù)據(jù)

獲取當天所有的行情信息,無法指定具體某一支的行情

import tushare as ts

ts.get_today_all()

歷史分筆與實時分筆(買賣盤統(tǒng)計):

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個股票的實時分筆數(shù)據(jù)

3、大盤指數(shù)

import tushare as ts

df = ts.get_index()

4、新股數(shù)據(jù)

獲取打新數(shù)據(jù):

import tushare as ts

ts.new_stocks()

5、基本面數(shù)據(jù)

基本面數(shù)據(jù)里包含選股的很多依據(jù)指標,如:市盈率、市凈率、每股收益、凈利潤、季報、應收賬款周轉率、凈利潤增長率(%)、流動比率、速動比率、現(xiàn)金流量比率等。

import tushare as ts

ts.get_stock_basics()

#獲取2015年第1季度的業(yè)績報表數(shù)據(jù)

ts.get_report_data(2015,1)

#獲取2015年第1季度的盈利能力數(shù)據(jù)

ts.get_profit_data(2015,1)

#獲取2015年第1季度的營運能力數(shù)據(jù)

ts.get_operation_data(2015,1)

#獲取2015年第1季度的成長能力數(shù)據(jù)

ts.get_growth_data(2015,1)

#獲取2015年第1季度的償債能力數(shù)據(jù)

ts.get_debtpaying_data(2015,1)

#獲取2015年第1季度的現(xiàn)金流量數(shù)據(jù)

ts.get_cashflow_data(2015,1)

三、數(shù)據(jù)存儲

tushare自身提供了常用的數(shù)據(jù)保存格式:csv格式、excel格式、HDF5文件格式、JSON格式、mysql關系數(shù)據(jù)庫、nosql數(shù)據(jù)庫。

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'])

某些時候,可能需要將一些同類數(shù)據(jù)保存在一個大文件中,這時候就需要將數(shù)據(jù)追加在同一個文件里,簡單舉例如下:

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

#設定數(shù)據(jù)位置(從第3行,第6列開始插入數(shù)據(jù))

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

#存入數(shù)據(jù)庫

df.to_sql('tick_data',engine)

#追加數(shù)據(jù)到現(xiàn)有表

#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')))

四、數(shù)據(jù)繪圖

上面都是拾人牙慧的東西,這里來一點點干貨。由 tushare 處理輸出的格式已經(jīng)經(jīng)過整形,所以可以結合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')

# 指定繪圖的長寬尺度及背景網(wǎng)格

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函數(shù)保存圖片到指定的位置,代碼如下:

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的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。