请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  源码分享 帖子:3353378 新帖:24

共享函数 | 获取中证指数/行业的行情,pe,pb,

Peace发表于:5 月 10 日 05:00回复(1)

对于常见但是获取比较不太方便的数据我们将会写成一系列共享函数方便大家直接复制使用。
本系列将持续更新,标题为 【共享函数】


有些方法也可以直接在社区搜索到哦!

其中:
申万指数:
TotalMarketValue :  总市值(万元)
AShareTotalMV  : A股流通市值(万元)

中证指数:
IndexPERatio1 :  指数市盈率(I)是按总股本计算的市盈率,
IndexPERatio2 :  指数市盈率(II)是按照中证指数调整后的股本计算的市盈率;

IndexDYRatio1 : 指数股息率(I)是按总股本计算的股息率,
IndexDYRatio2 : 指数股息率(II)是按照中证指数调整后的股本计算的股息率。以上股本调整规则详见中证指数官网对各个指数的编制规则:

获取申万行业指数的行情,pe,pb,总市值(及A股流通市值)数据¶

from jqdata import jyfrom jqdata import *import pandas as pd#注意申万指数在2014年有一次大改,聚源使用的是为改变之前的代码,官网包含更改前和更改后的代码,如果遇到找不到的标的可以根据需求自行查找#如801124 >>801121食品加工IIdef get_sw_quote(code,end_date=None,count=None,start_date=None):'''获取申万指数行情,返回panel结构,总市值和流通市值的单位为(万元)'''if isinstance(code,str):code=[code]days = get_trade_days(start_date,end_date,count)code_df = jy.run_query(query( jy.SecuMain.InnerCode,jy.SecuMain.SecuCode,jy.SecuMain.ChiName).filter(jy.SecuMain.SecuCode.in_(code)))df = jy.run_query(query( jy.QT_SYWGIndexQuote).filter(jy.QT_SYWGIndexQuote.InnerCode.in_(code_df.InnerCode),jy.QT_SYWGIndexQuote.TradingDay.in_(days),))df2  = pd.merge(code_df, df, on='InnerCode').set_index(['TradingDay','SecuCode'])df2.drop(['InnerCode','ID','UpdateTime','JSID','RightLevel'],axis=1,inplace=True)return df2.to_panel()code = get_industries(name='sw_l2').index[:5]panel = get_sw_quote(code,end_date='2018-01-01',count=10)panel.to_frame(False).tail()
/opt/conda/lib/python3.5/site-packages/ipykernel_launcher.py:24: DeprecationWarning: 
Panel is deprecated and will be removed in a future version.
The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method
Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.
Pandas provides a `.to_xarray()` method to help automate this conversion.

.dataframe thead tr:only-child th {        text-align: right;    }    .dataframe thead th {        text-align: left;    }    .dataframe tbody tr th {        vertical-align: top;    }



ChiNamePrevClosePriceOpenPriceHighPriceLowPriceClosePriceTurnoverVolumeTurnoverValueTurnoverDealsChangePCTIndexPEIndexPBTotalMarketValueAShareTotalMV
TradingDaySecuCode













2017-12-29801011申银万国指数-林业1800.0921795.4801845.601784.411802.7982853232.03.554701e+07None0.0015777.373.132036129.01121708.0
801012申银万国指数-农产品加工2551.7762546.6002573.802515.612556.51293670965.01.234257e+09None0.001935.583.1214529320.07936090.0
801013申银万国指数-农业综合2269.6102260.5302278.202254.302272.4809084945.04.234125e+07None0.0013125.712.92867493.0464852.0
801014申银万国指数-饲料3644.5203646.5683709.263610.053685.350133928157.01.452713e+09None0.011223.782.7121027565.08124732.0
801015申银万国指数-渔业1540.1781535.7481566.711528.661555.76034618204.03.012524e+08None0.010143.952.525217722.02511131.0

获取中证指数行情,pe,指数市值及股息率¶

def get_zz_quote(code,end_date=None,count=None,start_date=None):'''获取中证指数行情,返回panel结构'''if isinstance(code,str):code=[code]code.sort()code = [x[:6] for x in code]days = get_trade_days(start_date,end_date,count)code_df = jy.run_query(query( jy.SecuMain.InnerCode,jy.SecuMain.SecuCode,jy.SecuMain.ChiName).filter(jy.SecuMain.SecuCode.in_(code)).order_by(jy.SecuMain.SecuCode))df = jy.run_query(query( jy.QT_CSIIndexQuote).filter(jy.QT_CSIIndexQuote.IndexCode.in_(code_df.InnerCode),jy.QT_CSIIndexQuote.TradingDay.in_(days),))df2  = pd.merge(code_df, df, left_on='InnerCode',right_on='IndexCode').set_index(['TradingDay','SecuCode'])df2.drop(['InnerCode','IndexCode','ID','UpdateTime','JSID','OpenInterest','SettleValue','IndexCSIType'],axis=1,inplace=True)return df2.to_panel()panel = get_zz_quote(['000016.XSHG','000001.XSHG'],end_date='2019-01-21',count=10)panel.to_frame(False).tail()
/opt/conda/lib/python3.5/site-packages/ipykernel_launcher.py:19: DeprecationWarning: 
Panel is deprecated and will be removed in a future version.
The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method
Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.
Pandas provides a `.to_xarray()` method to help automate this conversion.

.dataframe thead tr:only-child th {        text-align: right;    }    .dataframe thead th {        text-align: left;    }    .dataframe tbody tr th {        vertical-align: top;    }



ChiNameOpenPriceHighPriceLowPriceClosePriceTurnoverVolumeTurnoverValueChangeOFChangePCTTotalMVIndexPERatio1IndexPERatio2IndexDYRatio1IndexDYRatio2
TradingDaySecuCode













2019-01-17000016上海证券交易所50成份指数2388.59252396.79932370.58092371.34812.155500e+092.696493e+10-9.87-0.414448625.3010.0710.263.433.08
2019-01-18000001上海证券交易所综合指数2567.73862598.88362565.90432596.00561.907672e+101.512704e+1136.371.4228106437.1512.8912.892.602.60
000016上海证券交易所50成份指数2385.62192422.02902380.56242417.36303.008820e+093.749533e+1046.011.944534948.6710.2510.463.363.02
2019-01-21000001上海证券交易所综合指数2599.05752618.98012599.05752610.50941.634251e+101.399155e+1114.500.5628263759.0612.9712.972.582.58
000016上海证券交易所50成份指数2418.00472443.22542418.00472432.48702.623724e+093.391253e+1015.120.634563321.2510.3210.523.343.00
 

全部回复

0/140

量化课程

    移动端课程