根據(jù)大家的要求,及時(shí)更新了AQF編程語言Python丨金融數(shù)據(jù)獲取之tushare (下)~歷史閱讀:金融數(shù)據(jù)獲取之tushare(上)~
2 投資參考數(shù)據(jù)
import tushare as tsimport pandas as pd
2.1 分配預(yù)案
# argm: top=n,最近公布的那條數(shù)據(jù)# return: divi:每10股分紅金額# return: shares:每10股轉(zhuǎn)增和送股數(shù)df = ts.profit_data(year=2017, top=25)
df.head()

圖2.1
2.2 業(yè)績(jī)預(yù)告
# 獲取2016年年報(bào)(第四季度)預(yù)報(bào)# return:report_date: 預(yù)報(bào)發(fā)布時(shí)間# return:pre_eps:去年同期每股收益# return:range:預(yù)報(bào)業(yè)績(jī)變動(dòng)范圍df = ts.forecast_data(year=2017, quarter=3)
df.head()

圖2.2
2.3 限售股解禁
# 獲取特定年月限售股解禁數(shù)據(jù)# return: count:解禁數(shù)量# return: ratio:占總股本比例df = ts.xsg_data(year=2017, month=1)
df.head()

圖2.3
2.4 基金持股
# 獲取特定季度基金持有上市公司股票的數(shù)據(jù)# return: nums:基金家數(shù)# return:nlast: 與上期相比持倉(cāng)基金家數(shù)變動(dòng)# return:count:基金持股數(shù)(萬股)# return:clast: 與上期相比基金持股數(shù)量變動(dòng)# return:amount:基金持股市值# return:ratio:基金持股占流通股比例df = ts.fund_holdings(year=2016, quarter=4)
df.head()

圖2.4.1
# 獲取IPO數(shù)據(jù)# return: ipo_date: 上網(wǎng)發(fā)行日期# return:issue_date: 上市日期# return: amount: 發(fā)行數(shù)量(萬股)# return:markets: 上網(wǎng)發(fā)行數(shù)量(萬股)# return:price:發(fā)行價(jià)格# return:pe: 發(fā)行市盈率# return:limit:個(gè)人申購(gòu)上線(萬股)# return:funds:募集資金(億元)# return: ballot: 網(wǎng)上中簽率(%)df = ts.new_stocks()
df.head()

圖2.4.2
2.5 融資融券
2.5.1 匯總數(shù)據(jù)
# 獲取融資融券數(shù)據(jù)# return: rzye: 當(dāng)日融資余額(元)# return: rzmre: 當(dāng)日融資買入金額(元)# return: rqyl: 當(dāng)日融券余量# return: rqylje: 當(dāng)日融券余量金額(元)# return: rqmcl: 當(dāng)日融券賣出量# return: rzrqjyzl: 當(dāng)日融資融券余額
# 滬市融資融券數(shù)據(jù)df = ts.sh_margins(start='2017-01-01', end='2017-01-31')
df.head()
.jpg)
圖2.5.1.1
# 深市融資融券數(shù)據(jù)df = ts.sz_margins(start='2017-01-01', end='2017-01-31')
df.head()

圖2.5.1.2
2.5.2 明細(xì)數(shù)據(jù)
# 獲取特定標(biāo)的物的融資融券數(shù)據(jù)# return: rzche: 當(dāng)日融資償還額(元)# return:rqchl: 當(dāng)日融券償還量
# 滬市融資融券明細(xì)df = ts.sh_margin_details(start='2017-01-01', end='2017-01-31', symbol='600018')
df.head()

圖2.5.2.1
# 深市融資融券明細(xì)# 深市融資融券名字每次只能獲取一天的數(shù)據(jù)df = ts.sz_margin_details('2017-09-01')
df.head()

圖2.5.2.2
2.6 應(yīng)用實(shí)例
統(tǒng)計(jì)2016年末基金持倉(cāng)比例較高和最低的100只股票在其后一個(gè)月和三個(gè)月的漲跌幅情況。
import tushare as tsimport pandas as pd
import seabornimport matplotlib.pyplot as plt
fund_holdings = ts.fund_holdings(year=2016, quarter=4)fund_holdings = fund_holdings[(fund_holdings.code.map(len)==6) & (fund_holdings.name.map(len)<5)]fund_holdings = fund_holdings.astype({'code': str, 'ratio':float}, copy=True)fund_holdings.head()

圖2.6.1
fund_holdings = fund_holdings.sort_values('ratio')
bottom_stocks = fund_holdings.code[:100].tolist()top_stocks = fund_holdings.code[-100:].tolist()top_stocks[:5]
['002321', '600759', '600502', '002117', '600118']
series_list = []for code in top_stocks+bottom_stocks: s = ts.bar(code, '2016-12-30', '2017-03-31', adj='qfq').close s.name = code series_list.append(s)close = pd.DataFrame(series_list).Tclose.head()

圖2.6.2
def cum_return(close_df, start:str, end:str):
close_df = close_df.astype('float')
close_df.index = pd.to_datetime(close_df.index)
change = close_df/close_df.shift(1)
start_dt, end_dt = pd.to_datetime((start, end))
change_part = change[start_dt:end_dt]
period_return = change_part.aggregate('prod') return period_return - 1def calc_return(close_df, top, bottom, start:str, end:str):
period_return = cum_return(close_df, start, end)
top_return = period_return[top].reset_index(drop=True)
bottom_return = period_return[bottom].reset_index(drop=True) return pd.DataFrame({'top': top_return, 'bottom':bottom_return})
return1 = calc_return(close, top_stocks, bottom_stocks, '2016-12-30','2017-01-31')return3 = calc_return(close, top_stocks, bottom_stocks, '2016-12-30','2017-03-31')return1.head()

圖2.6.3
fig, ax = plt.subplots(1,2)fig.figsize = (90,45)ax[0].set_title('1 month return')ax[1].set_title('3 month return')ax[0].set_xlabel('period return')ax[1].set_xlabel('period return')return3.plot.hist(alpha=0.5, ax=ax[1])return1.plot.hist(alpha=0.5, ax=ax[0])ylim = ax[1].get_ylim()ax[0].set_ylim(ylim)plt.show()

圖2.6.5
從以上分布圖可以看到,2016年底基金持倉(cāng)比例前100名的個(gè)股在之后一個(gè)月和三個(gè)月的總體表現(xiàn)都優(yōu)于基金持倉(cāng)比例最后100名的個(gè)股。需要指出的是,由于樣本有限,這里得到的結(jié)果并不具有統(tǒng)計(jì)意義。此處僅提供一個(gè)可能的研究方向和代碼實(shí)現(xiàn)思路,希望可以對(duì)大家有所幫助。附上GIF動(dòng)圖版,讓你能更直觀了解Python那些事兒~那么,就祝大家學(xué)習(xí)愉快! >>>點(diǎn)擊咨詢Python金融應(yīng)用實(shí)戰(zhàn)
.jpg)
金程推薦: AQF考試 AQF報(bào)名 量化金融分析師
量化金融分析師AQF交流答疑群:737550026
聲明▎更多內(nèi)容請(qǐng)關(guān)注微信號(hào)量化金融分析師。原創(chuàng)文章,歡迎分享,若需引用或轉(zhuǎn)載請(qǐng)保留此處信息。




