板块异动类
来源:https://uqer.io/community/share/54ccf06af9f06c276f651a5a
本代码主要实现以下功能
- 由
DataAPI.EquIndustryGet
获得每只个股的所属行业,这里采用的是申万二级分类;
- 根据个股行业获得所有行业的成分股
- 根据成分股的每天涨幅和市值,获得主题的加权涨幅,将其排序,即得到每日涨跌幅最大的行业前十
- 根据成分股出现的涨跌停次数,获得涨跌停比例最大的行业前十
- 根据成分股的换手率,获得换手率最大和最小的行业前十
- 将每个行业所包含的个股,储存到csv文件中,如果对某个行业感兴趣,可以进一步查看其成分股
此处定义了一些函数,使得代码功能更明确
def GetIndInfo(universe,field):
num = 100
count_num = len(universe)/num
if count_num>0:
indus_df = pd.DataFrame({})
for i in range(count_num):
sub_ind = DataAPI.EquIndustryGet(secID=universe[i*num:(i+1)*num],field=field)
indus_df = pd.concat([indus_df,sub_ind])
sub_ind = DataAPI.EquIndustryGet(secID=universe[(i+1)*num:],field=field)
indus_df = pd.concat([indus_df,sub_ind])
else:
indus_df = DataAPI.EquIndustryGet(secID=universe,field=field)
filed_new = ['secID']+field
indus_df = indus_df[filed_new]
return indus_df
def GetMktInfo(secID,beginDate,endDate,field):
num = 50
count_num = len(secID)/num
if count_num>0:
MktInfo_df = pd.DataFrame({})
for i in range(count_num):
sub_info = DataAPI.MktEqudGet(secID=secID[i*num:(i+1)*num],beginDate=beginDate,endDate=endDate,field=field)
MktInfo_df = pd.concat([MktInfo_df,sub_info])
sub_info = DataAPI.MktEqudGet(secID=secID[(i+1)*num:],beginDate=beginDate,endDate=endDate,field=field)
MktInfo_df = pd.concat([MktInfo_df,sub_info])
else:
MktInfo_df = DataAPI.MktEqudGet(secID=secID,beginDate=beginDate,endDate=endDate,field=field)
return MktInfo_df
def CountTime():
today = datetime.today()
cal_date = Date.fromDateTime(today)
if cal.isBizDay(cal_date):
today_str = today.strftime("%Y%m%d")
time1=" 15:05:00"
ben_time = datetime.strptime(today_str+time1,"%Y%m%d %H:%M:%S")
if today>ben_time:
date = today_str
else:
cal_wd = cal.adjustDate(cal_date,BizDayConvention.Preceding)
dtime_wd = cal_wd.toDateTime()
date = dtime_wd.strftime("%Y%m%d")
return date
获得个股的行情数据,并以此来计算主题的:涨幅、涨跌停比例、换手率
from datetime import timedelta
cal = Calendar('China.SSE')
universe = set_universe('A')
indus_df = GetIndInfo(universe=universe,field =['secShortName','industryName2'])
cnt_date = CountTime()
field_mkt = ['preClosePrice','openPrice','highestPrice','lowestPrice','closePrice','turnoverRate','marketValue']
MktInfo_df = GetMktInfo(secID=universe,beginDate=cnt_date,endDate=cnt_date,field=field_mkt)
ind_inc_dic = {}
ind_gb_dic = {}
ind_turn_dic = {}
ind_tknm_dic = {}
grouped = indus_df.groupby('industryName2')
for name,group in grouped:
ind_tknm_dic[name] = list(group['secShortName'])
stk_list = list(group['secID'])
sub_mkt_info = MktInfo_df[MktInfo_df.secID.isin(stk_list)]
sub_mkt_info['inc_rate'] = (sub_mkt_info['closePrice']-sub_mkt_info['preClosePrice'])/sub_mkt_info['preClosePrice']
ind_inc = (sub_mkt_info['inc_rate']*sub_mkt_info['marketValue']).sum()/sub_mkt_info['marketValue'].sum()
ind_inc_dic[name] = ind_inc
num_good = len(sub_mkt_info[((sub_mkt_info['closePrice']-sub_mkt_info['preClosePrice'])/sub_mkt_info['preClosePrice']).round(2)==0.1])
num_bad = len(sub_mkt_info[((sub_mkt_info['preClosePrice']-sub_mkt_info['closePrice'])/sub_mkt_info['preClosePrice']).round(2)==0.1])
ind_gb_dic[name] = (num_good-num_bad)*1.0/len(group)
turnover = sub_mkt_info['turnoverRate'].mean()
ind_turn_dic[name] = turnover
以下是将结果进行展示
ind_turn_pd = pd.DataFrame.from_dict(ind_turn_dic,orient='index')
ind_turn_pd.rename(columns={0:u'换手率'},inplace=True)
ind_turn_pd = ind_turn_pd.sort(columns=u'换手率',ascending=False)
ind_turn_pd1 = ind_turn_pd.sort(columns=u'换手率',ascending=True)
print cnt_date+'换手率最大的行业前十:'
ind_turn_pd[0:10]
20150130换手率最大的行业前十:
|
换手率 |
视听器材 |
0.046510 |
基础建设 |
0.042633 |
房屋建设 |
0.036725 |
计算机应用 |
0.036130 |
环保工程及服务 |
0.035021 |
营销传播 |
0.034763 |
畜禽养殖 |
0.034093 |
电力 |
0.033552 |
农业综合 |
0.032450 |
装修装饰 |
0.032230 |
print cnt_date+'换手率最小的行业前十:'
ind_turn_pd1[0:10]
20150130换手率最小的行业前十:
|
换手率 |
石油开采 |
0.000900 |
银行 |
0.008894 |
机场 |
0.009800 |
航空运输 |
0.010020 |
饲料 |
0.010518 |
高速公路 |
0.010583 |
汽车整车 |
0.011491 |
煤炭开采 |
0.011964 |
其他交运设备 |
0.012071 |
餐饮 |
0.012150 |
ind_gb_pd = pd.DataFrame.from_dict(ind_gb_dic,orient='index')
ind_gb_pd.rename(columns={0:u'涨跌停比例'},inplace=True)
ind_gb_pd = ind_gb_pd.sort(columns=u'涨跌停比例',ascending=False)
ind_gb_pd1 = ind_gb_pd.sort(columns=u'涨跌停比例',ascending=True)
print cnt_date+'涨停比例最大的行业前十:'
ind_gb_pd[0:10]
20150130涨停比例最大的行业前十:
|
涨跌停比例 |
视听器材 |
0.200000 |
贸易 |
0.086957 |
物流 |
0.055556 |
专业工程 |
0.055556 |
互联网传媒 |
0.045455 |
塑料 |
0.045455 |
房地产开发 |
0.029630 |
电力 |
0.017241 |
家用轻工 |
0.000000 |
保险 |
0.000000 |
print cnt_date+'跌停比例最大的行业前十:'
ind_gb_pd1[0:10]
20150130跌停比例最大的行业前十:
|
涨跌停比例 |
旅游综合 |
-0.066667 |
计算机设备 |
-0.051282 |
电子制造 |
-0.032258 |
光学光电子 |
-0.024390 |
中药 |
-0.017857 |
化学制品 |
-0.006993 |
专用设备 |
0.000000 |
航运 |
0.000000 |
农业综合 |
0.000000 |
采掘服务 |
0.000000 |
ind_inc_pd = pd.DataFrame.from_dict(ind_inc_dic,orient='index')
ind_inc_pd = ind_inc_pd.sort(columns=0,ascending=False)
ind_inc_pd.rename(columns={0:u'涨跌幅'},inplace=True)
ind_inc_pd1 = ind_inc_pd.sort(columns=u'涨跌幅')
print cnt_date+'涨幅最大的行业前十:'
ind_inc_pd[0:10]
|
涨跌幅 |
视听器材 |
0.036822 |
燃气 |
0.018286 |
种植业 |
0.015623 |
房地产开发 |
0.006603 |
农业综合 |
0.005786 |
水务 |
0.005265 |
餐饮 |
0.004425 |
动物保健 |
0.004262 |
饮料制造 |
0.003649 |
汽车服务 |
0.003630 |
print cnt_date+'跌幅最大的行业前十:'
ind_inc_pd1[:10]
20150130跌幅最大的行业前十:
|
涨跌幅 |
运输设备 |
-0.071812 |
基础建设 |
-0.049886 |
多元金融 |
-0.041817 |
铁路运输 |
-0.040228 |
保险 |
-0.036876 |
房屋建设 |
-0.035251 |
计算机应用 |
-0.032599 |
石油开采 |
-0.028381 |
林业 |
-0.028153 |
航空运输 |
-0.025830 |
将行业包含的个股信息储存到csv文件中,可以进行更细致的查看行业信息
ind_tk_pd = pd.DataFrame({})
for ind_nm,tk_list in ind_tknm_dic.items():
sub_pd = pd.DataFrame(tk_list)
sub_pd[u'行业名称'] = ind_nm
ind_tk_pd = pd.concat([ind_tk_pd,sub_pd])
ind_tk_pd.rename(columns={0:u'成分股'},inplace=True)
ind_tk_pd = ind_tk_pd.loc[:,[u'行业名称',u'成分股']]
ind_tk_pd.to_csv('ind_tk.csv',encoding='GBK',index=False)