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

量化交易吧 /  源码分享 帖子:3363253 新帖:1

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

我是游客发表于:9 月 2 日 14:00回复(1)

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

from jqdata import jy
from jqdata import *
import pandas as pd

#注意申万指数在2014年有一次大改,聚源使用的是为改变之前的代码,官网包含更改前和更改后的代码,如果遇到找不到的标的可以根据需求自行查找
#如801124 >>801121食品加工II
def 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; }
ChiName PrevClosePrice OpenPrice HighPrice LowPrice ClosePrice TurnoverVolume TurnoverValue TurnoverDeals ChangePCT IndexPE IndexPB TotalMarketValue AShareTotalMV
TradingDay SecuCode
2017-12-29 801011 申银万国指数-林业 1800.092 1795.480 1845.60 1784.41 1802.798 2853232.0 3.554701e+07 None 0.0015 777.37 3.13 2036129.0 1121708.0
801012 申银万国指数-农产品加工 2551.776 2546.600 2573.80 2515.61 2556.512 93670965.0 1.234257e+09 None 0.0019 35.58 3.12 14529320.0 7936090.0
801013 申银万国指数-农业综合 2269.610 2260.530 2278.20 2254.30 2272.480 9084945.0 4.234125e+07 None 0.0013 125.71 2.92 867493.0 464852.0
801014 申银万国指数-饲料 3644.520 3646.568 3709.26 3610.05 3685.350 133928157.0 1.452713e+09 None 0.0112 23.78 2.71 21027565.0 8124732.0
801015 申银万国指数-渔业 1540.178 1535.748 1566.71 1528.66 1555.760 34618204.0 3.012524e+08 None 0.0101 43.95 2.52 5217722.0 2511131.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; }
ChiName OpenPrice HighPrice LowPrice ClosePrice TurnoverVolume TurnoverValue ChangeOF ChangePCT TotalMV IndexPERatio1 IndexPERatio2 IndexDYRatio1 IndexDYRatio2
TradingDay SecuCode
2019-01-17 000016 上海证券交易所50成份指数 2388.5925 2396.7993 2370.5809 2371.3481 2.155500e+09 2.696493e+10 -9.87 -0.41 4448625.30 10.07 10.26 3.43 3.08
2019-01-18 000001 上海证券交易所综合指数 2567.7386 2598.8836 2565.9043 2596.0056 1.907672e+10 1.512704e+11 36.37 1.42 28106437.15 12.89 12.89 2.60 2.60
000016 上海证券交易所50成份指数 2385.6219 2422.0290 2380.5624 2417.3630 3.008820e+09 3.749533e+10 46.01 1.94 4534948.67 10.25 10.46 3.36 3.02
2019-01-21 000001 上海证券交易所综合指数 2599.0575 2618.9801 2599.0575 2610.5094 1.634251e+10 1.399155e+11 14.50 0.56 28263759.06 12.97 12.97 2.58 2.58
000016 上海证券交易所50成份指数 2418.0047 2443.2254 2418.0047 2432.4870 2.623724e+09 3.391253e+10 15.12 0.63 4563321.25 10.32 10.52 3.34 3.00
 

全部回复

0/140

达人推荐

量化课程

    移动端课程