🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 历史的十一月板块涨幅 > 来源:https://uqer.io/community/share/563c8065f9f06c713ddfeb6d 大盘又开始疯长了,还记嘚去年十一月吗 ? ```py #获得行业信息 def GetEquIndustry(universe,field): num = 100 cnt_num = len(universe)/num if cnt_num > 0: df = pd.DataFrame({}) for i in range(cnt_num) : sub_df = DataAPI.EquIndustryGet(secID=universe[i*num:(i+1)*num],field=field) df = pd.concat([df,sub_df]) if (i+1)*num != len(universe): sub_df = DataAPI.EquIndustryGet(secID=universe[(i+1)*num:],field=field) df = pd.concat([df,sub_df]) else: df = DataAPI.EquIndustryGet(secID=universe,field=field) return df ``` ```py from CAL.PyCAL import * import pandas as pd cal = Calendar('China.SSE') universe = DataAPI.EquGet(equTypeCD='A')['secID'].tolist() #获得全A股的secID id2nm = lambda x:x[0:6] tk_list_A = map(id2nm,universe) #获得全A股的ticker Ind_info = GetEquIndustry(universe = universe ,field=['ticker','secShortName','industryName2']) #获得个股的申万行业分类 Ind_info_gp = Ind_info.groupby('industryName2')#按照行业分组 Ind_tks_dic = {} #获得每个行业包含的股票 for ind_nm,sub_info in Ind_info_gp: Ind_tks_dic[ind_nm] = sub_info.drop_duplicates('ticker')['ticker'].tolist() ``` ```py from pandas import DataFrame,Series from CAL.PyCAL import * cal = Calendar('China.SSE') field = ['ticker','secShortName','tradeDate','preClosePrice','closePrice','turnoverValue'] #时间轴(开始时间) time = ['20141031', '20131031', '20121031', '20111031', '20101031'] #保存各个时间段的数据 Data_time = {} #保存各个时间段的股票名字 tk_nm_dic ={} # 时间稍慢 for s in time : Data_time[s] = DataFrame() for x in universe : try : data_temp = DataAPI.MktEqudAdjGet( secID = x , field =field , beginDate = s , endDate = cal.advanceDate(s,'1M', BizDayConvention.Following).strftime('%Y%m%d')) data_temp['marketValue'] = DataAPI.MktEqudGet(secID = x ,field ='marketValue' , beginDate = s , endDate = cal.advanceDate(s,'1M', BizDayConvention.Following).strftime('%Y%m%d')) Data_time[s] = pd.concat([Data_time[s],data_temp]) except : continue tk_nm_dic[s] = dict(zip(Data_time[s]['ticker'],Data_time[s]['secShortName'])) # 获得个股ticker与名称的对应字典 for s in Data_time.values() : s['tradeDate'] = pd.to_datetime(s['tradeDate']) # 将tradeDate这一列的格式由string改为datetime s['increase'] = s['closePrice']/s['preClosePrice'] # 获得个股每天的收益 ``` ```py # 股票数据统计 Stock_Data = {} for s in Data_time.keys() : Stock_dict = {'ticker':[],'income':[],'turnoverValue':[] ,'marketValue' :[]} # 获得每个时间段的Data计算个股的收益和平均市值 for tk,sub_info in Data_time[s].groupby('ticker') : income = sub_info['increase'].prod()-1 # 获得在这段时间内该股的涨幅 mkt_value = sub_info['marketValue'].sum()/len(sub_info) turnoverValue_avg = sub_info['turnoverValue'].sum()/len(sub_info) Stock_dict['ticker'].append(tk) Stock_dict['income'].append(income) Stock_dict['marketValue'].append(mkt_value) Stock_dict['turnoverValue'].append(turnoverValue_avg) # 返回时间为Key的个股数据 Stock_Data[s] = pd.DataFrame(Stock_dict) ``` ```py # 行业数据统计 Output_dicy = {} Output_dicy['industry'] = [] Output_dicy['Num'] = [] Output_dicy['Nov14'] = [] for ind,tks in Ind_tks_dic.items() : for table in Stock_Data.keys() : if not table in Output_dicy.keys() : Output_dicy[table] = [] sub_Industry = Stock_Data[table][Stock_Data[table]['ticker'].isin(tks)] # 成交量前三 bigstk = sub_Industry.sort(columns='turnoverValue',ascending=False)['ticker'][0:3].tolist() # 行业指数收益 if not sub_Industry['marketValue'].sum() == 0 : rtn_Industry = (sub_Industry['income']*sub_Industry['marketValue']).sum()/sub_Industry['marketValue'].sum() Output_dicy[table].append(rtn_Industry) if table == '20141031' : Output_dicy['Nov14'].append(map(lambda x:tk_nm_dic['20141031'][x],bigstk)) if not sub_Industry['marketValue'].sum() == 0 : #最新行业成分数量 Output_dicy['Num'].append(len(sub_Industry)) Output_dicy['industry'].append(ind) Output_table = pd.DataFrame(Output_dicy) ``` 去年十一月疯长的大盘 ```py # 统计并显示 Out_put = Output_table.loc[:,['industry','Num','20101031','20111031','20121031','20131031','20141031','Nov14']] Out_put.columns = [u'行业名称',u'该行业成分股数目(15年)',u'2010年十一月',u'2011年十一月',u'2012年十一月',u'2013年十一月',u'2014年十一月',u'2014年板块成交量前三'] Out_put[u'平均涨幅'] = (Out_put[u'2010年十一月']+Out_put[u'2011年十一月']+Out_put[u'2012年十一月']+Out_put[u'2013年十一月']+Out_put[u'2014年十一月']) / 5 print u'一共有%d个申万二级行业'%len(Out_put),u' : ' Out_put.sort(u'2014年十一月' , ascending = False).head(20) ``` ``` 一共有208个申万二级行业 : ``` | | 行业名称 | 该行业成分股数目(15年) | 2010年十一月 | 2011年十一月 | 2012年十一月 | 2013年十一月 | 2014年十一月 | 2014年板块成交量前三 | 平均涨幅 | | --- | --- | | 97 | 资本市场服务 | 18 | -0.141082 | -0.152839 | -0.133307 | 0.098754 | 0.511463 | [中信证券, 海通证券, 兴业证券] | 0.036598 | | 22 | 证券 | 20 | -0.142078 | -0.150951 | -0.129955 | 0.094950 | 0.493749 | [中信证券, 海通证券, 兴业证券] | 0.033143 | | 107 | 综合金融 | 21 | -0.139421 | -0.148480 | -0.132800 | 0.095897 | 0.477825 | [中信证券, 海通证券, 兴业证券] | 0.030604 | | 2 | 航空运输 | 6 | -0.179507 | -0.123943 | -0.094819 | 0.015406 | 0.406586 | [海南航空, 东方航空, 中信海直] | 0.004744 | | 149 | 航空运输业 | 10 | -0.173271 | -0.107966 | -0.082920 | 0.015486 | 0.343224 | [海南航空, 东方航空, 中信海直] | -0.001089 | | 38 | 保险业 | 3 | -0.095615 | -0.017941 | -0.035695 | 0.154288 | 0.323310 | [中国平安, 中国太保, 中国人寿] | 0.065669 | | 153 | 保险 | 4 | -0.095476 | -0.018500 | -0.035806 | 0.154006 | 0.323234 | [中国平安, 中国太保, 中国人寿] | 0.065492 | | 92 | 房屋建筑业 | 1 | 0.000000 | 0.000000 | -0.127193 | 0.202055 | 0.316129 | [高新发展] | 0.078198 | | 112 | 高速公路 | 23 | -0.108908 | -0.088902 | -0.073154 | 0.052344 | 0.272050 | [广发证券, 厦门港务, 五洲交通] | 0.010686 | | 193 | 石油加工、炼焦和核燃料加工业 | 18 | 0.007286 | -0.034520 | -0.127721 | 0.005377 | 0.258827 | [陕西黑猫, 上海石化, *ST华锦] | 0.021850 | | 122 | 建筑安装业 | 1 | -0.113664 | -0.006221 | -0.139037 | 0.020833 | 0.213740 | [中关村] | -0.004870 | | 139 | 基础建设 | 22 | -0.037736 | -0.088644 | 0.009290 | 0.011860 | 0.212001 | [中国中铁, 中国铁建, 中国交建] | 0.021354 | | 202 | 农、林、牧、渔服务业 | 1 | -0.013893 | 0.032037 | -0.106501 | 0.055081 | 0.210865 | [丰乐种业] | 0.035518 | | 113 | 房屋建设 | 4 | -0.046656 | -0.087118 | 0.006878 | 0.009602 | 0.202862 | [中国建筑, 上海建工, 宁波建工] | 0.017114 | | 176 | 园区开发 | 18 | -0.042658 | -0.077632 | -0.073093 | -0.033278 | 0.200555 | [陆家嘴, 外高桥, 金融街] | -0.005221 | | 39 | 租赁业 | 1 | -0.149817 | 0.011525 | -0.106517 | 0.043152 | 0.200312 | [渤海租赁] | -0.000269 | | 82 | 土木工程建筑业 | 45 | -0.028200 | -0.077634 | -0.003929 | 0.004428 | 0.181418 | [中国建筑, 中国中铁, 中国铁建] | 0.015217 | | 194 | 货币金融服务 | 16 | -0.060532 | -0.046490 | 0.017593 | 0.012884 | 0.181281 | [浦发银行, 兴业银行, 民生银行] | 0.020947 | | 101 | 银行 | 16 | -0.060532 | -0.046490 | 0.017593 | 0.012884 | 0.181281 | [浦发银行, 兴业银行, 民生银行] | 0.020947 | | 67 | 燃气生产和供应业 | 9 | -0.097077 | 0.016135 | -0.038805 | 0.005440 | 0.178853 | [大众公用, 重庆燃气, 申能股份] | 0.012909 | 五年间十一月平均涨幅 ```py Out_put.sort(u'平均涨幅' , ascending = False).head(20) ``` | | 行业名称 | 该行业成分股数目(15年) | 2010年十一月 | 2011年十一月 | 2012年十一月 | 2013年十一月 | 2014年十一月 | 2014年板块成交量前三 | 平均涨幅 | | --- | --- | | 92 | 房屋建筑业 | 1 | 0.000000 | 0.000000 | -0.127193 | 0.202055 | 0.316129 | [高新发展] | 0.078198 | | 38 | 保险业 | 3 | -0.095615 | -0.017941 | -0.035695 | 0.154288 | 0.323310 | [中国平安, 中国太保, 中国人寿] | 0.065669 | | 153 | 保险 | 4 | -0.095476 | -0.018500 | -0.035806 | 0.154006 | 0.323234 | [中国平安, 中国太保, 中国人寿] | 0.065492 | | 35 | 林业 | 5 | 0.010244 | 0.117180 | 0.028619 | 0.012805 | 0.085120 | [平潭发展, 永安林业, 吉林森工] | 0.050794 | | 200 | 运输设备 | 7 | 0.116078 | -0.056020 | 0.095749 | 0.071869 | 0.008633 | [晋西车轴, 北方创业, 康尼机电] | 0.047262 | | 97 | 资本市场服务 | 18 | -0.141082 | -0.152839 | -0.133307 | 0.098754 | 0.511463 | [中信证券, 海通证券, 兴业证券] | 0.036598 | | 202 | 农、林、牧、渔服务业 | 1 | -0.013893 | 0.032037 | -0.106501 | 0.055081 | 0.210865 | [丰乐种业] | 0.035518 | | 22 | 证券 | 20 | -0.142078 | -0.150951 | -0.129955 | 0.094950 | 0.493749 | [中信证券, 海通证券, 兴业证券] | 0.033143 | | 107 | 综合金融 | 21 | -0.139421 | -0.148480 | -0.132800 | 0.095897 | 0.477825 | [中信证券, 海通证券, 兴业证券] | 0.030604 | | 123 | 铁路运输业 | 3 | -0.074811 | -0.032704 | 0.033269 | 0.058432 | 0.158774 | [大秦铁路, 广深铁路, 铁龙物流] | 0.028592 | | 111 | 餐饮业 | 4 | 0.116088 | 0.030976 | -0.115720 | 0.094488 | 0.002694 | [*ST云网, 西安饮食, 全聚德] | 0.025705 | | 188 | 互联网和相关服务 | 12 | 0.063492 | 0.059126 | -0.123481 | 0.001113 | 0.127137 | [东方财富, 鹏博士, 海虹控股] | 0.025477 | | 182 | 电气自动化设备 | 30 | 0.181047 | 0.021313 | -0.121661 | 0.000477 | 0.045836 | [国电南瑞, 许继电气, 川仪股份] | 0.025402 | | 20 | 专业技术服务业 | 9 | 0.173828 | -0.040446 | -0.119783 | 0.046313 | 0.057355 | [中材节能, 设计股份, 三联虹普] | 0.023453 | | 193 | 石油加工、炼焦和核燃料加工业 | 18 | 0.007286 | -0.034520 | -0.127721 | 0.005377 | 0.258827 | [陕西黑猫, 上海石化, *ST华锦] | 0.021850 | | 4 | 铁路运输 | 3 | -0.074811 | -0.032704 | 0.028185 | 0.057472 | 0.130967 | [大秦铁路, 广深铁路, 铁龙物流] | 0.021822 | | 139 | 基础建设 | 22 | -0.037736 | -0.088644 | 0.009290 | 0.011860 | 0.212001 | [中国中铁, 中国铁建, 中国交建] | 0.021354 | | 194 | 货币金融服务 | 16 | -0.060532 | -0.046490 | 0.017593 | 0.012884 | 0.181281 | [浦发银行, 兴业银行, 民生银行] | 0.020947 | | 101 | 银行 | 16 | -0.060532 | -0.046490 | 0.017593 | 0.012884 | 0.181281 | [浦发银行, 兴业银行, 民生银行] | 0.020947 | | 44 | 食品制造业 | 22 | 0.150009 | 0.017352 | -0.080203 | -0.045608 | 0.059697 | [伊利股份, 光明乳业, 花园生物] | 0.020249 |