根据沪深300成分股权重,如何转化成行业权重?

成分股
沪深300
标签: #<Tag:0x00007fcf61270b90> #<Tag:0x00007fcf612709d8>

(鲁鲁) #1

这里的行业采取申万一级行业分类标准,谢谢!


(iQuant) #2
克隆策略
In [19]:
#导入需要的包
from pandas import DataFrame
import numpy as np
import pandas as pd
from datetime import datetime
date='2017-11-23' # 日期

#获取沪深300成分股权重,权重以流通市值作为依据
def get_hs300weigehts(date):
    df = D.history_data(D.instruments(date, date),date,date,fields=['in_csi300'])
    hs300 = df[df['in_csi300']==1]['instrument']
    stocklist=hs300.tolist()
    liutongshizhi=D.history_data(stocklist, start_date=date, end_date=date,fields=['market_cap_float'])  #获取某一时间的流通市值
    liutongshizhi['weigehts']=liutongshizhi['market_cap_float']/liutongshizhi['market_cap_float'].sum()
    return liutongshizhi.loc[:,['instrument','weigehts']]  

industry_weights = get_hs300weigehts(date)
In [59]:
industry_info =  D.history_data(D.instruments(),date,date,['industry_sw_level1','industry_sw_level2'])
# 银行、证券、保险等采取申万二级行业,其他采取申万一级行业分类
def g(x):
    if x.industry_sw_level1 == 490000:  
        return x.industry_sw_level2
    else:
        return x.industry_sw_level1
    
industry_info['industry_sw_level'] = industry_info.apply(g,axis=1) 
In [75]:
left_df = industry_weights
right_df = industry_info[['instrument','industry_sw_level']]
my_data = pd.merge(left_df,right_df,how='inner')
In [77]:
my_data.groupby("industry_sw_level").sum() 
Out[77]:
weigehts
industry_sw_level
110000 0.004753
210000 0.074572
220000 0.037663
230000 0.012048
240000 0.021226
270000 0.038208
280000 0.031685
330000 0.031676
340000 0.059867
350000 0.002315
360000 0.000099
370000 0.037223
410000 0.023954
420000 0.035369
430000 0.040226
450000 0.008883
460000 0.004001
480000 0.257343
490100 0.054212
490200 0.074091
490300 0.003867
510000 0.004345
610000 0.004513
620000 0.043044
630000 0.011709
640000 0.017850
650000 0.014095
710000 0.015013
720000 0.018436
730000 0.017715
In [80]:
# SHENWAN_INDUSTRY_MAP = {
#        210000:"采掘",
#        220000:"化工",
#        230000:"钢铁",
#        240000:"有色金属",
#        610000:"建筑材料/建筑建材",
#        620000:"建筑装饰",
#        630000:"电气设备",
#        640000:"机械设备",
#        650000:"国防军工",
#        280000:"汽车/交运设备",
#        330000:"家用电器",
#        360000:"轻工制造",
#        110000:"农林牧渔",
#        340000:"食品饮料",
#        350000:"纺织服装",
#        370000:"医药生物",
#        450000:"商业贸易",
#        460000:"休闲服务",
#        270000:"电子",
#        710000:"计算机",
#        720000:"传媒/信息服务",
#        730000:"通信",
#        410000:"公用事业",
#        420000:"交通运输",
#        430000:"房地产",
#        480000:"银行",
#        490100:"证券",
#        490200:"保险",
#        490300:"多元金融",
#        510000:"综合"  
#      }
# SHENWAN_INDUSTRY_MAP = {SHENWAN_INDUSTRY_MAP[i]:i for i in SHENWAN_INDUSTRY_MAP.keys() }