最近似乎市场上流传着炒国防军工的说法。最有名的应该是华泰证券痛斥A股不合理,茅台+五粮液+洋河=军工市值。接着又是国产大飞机,中印边境各种新闻,接着又说军工今年跌了多少,该超跌反弹了,我们用数据来分析是否可以真正做多军工板块。
克隆策略
获取所有数据¶
In [69]:
start_date = '2017-08-09'
end_date = '2017-08-09'
instrument = D.instruments(start_date=start_date, end_date=end_date)
df = D.history_data(instrument, start_date=start_date, end_date=end_date, fields=['industry_sw_level1'])
In [13]:
stock_list = list(df[df['industry_sw_level1'] == 650000].instrument)
stock_name_info = D.history_data(stock_list, start_date=start_date, end_date=end_date, fields=['name'])
stock_name_info.head(10)
Out[13]:
计算国防军工最近的行业平均市盈率¶
In [16]:
stock_pe_info = D.history_data(stock_list, start_date=start_date, end_date=end_date, fields=['name','pe_ttm'])
stock_pe_info.pe_ttm.mean()
Out[16]:
可以看出,目前国防军工的平均市盈率并不算低啊~~~
计算多个一级行业分类的市盈率¶
In [68]:
# 240000 有色金属
# 630000 电气设备
# 650000 国防军工
# 640000 机械设备
# 710000 计算机
# 730000 通信
# 210000 采掘
sw_level1 = [650000, 240000, 630000, 640000, 710000, 730000, 210000]
def calcu_industry_pe(industry_code):
stock_list = list(df[df['industry_sw_level1'] == industry_code].instrument)
stock_name_info = D.history_data(stock_list, start_date=start_date, end_date=end_date, fields=['name'])
stock_pe_info = D.history_data(stock_list, start_date=start_date, end_date=end_date, fields=['name','pe_ttm'])
return stock_pe_info.pe_ttm.mean()
pe_data = {}
for j in sw_level1:
pe_data[j] = calcu_industry_pe(j)
pd.Series(pe_data)
Out[68]:
我们比较了国防军工和其他几个一级行业分类的行业平均市盈率,发现,只有机械设备的平均市盈率高于国防军工,其他几个行业的市盈率都比国防军工低。
查看国防军工2017年以来的行业市盈率走势¶
In [64]:
dates = D.trading_days(market='CN', start_date='2017-01-01', end_date='2017-08-09')
dates = dates.date.map(lambda x:x.strftime('%Y-%m-%d'))
# 计算某个行业某天的行业平均市盈率,以650000举例
def calcu_pe(industry_code, date):
start_date = date
end_date = date
instrument = D.instruments(start_date=start_date, end_date=end_date)
df = D.history_data(instrument, start_date=start_date, end_date=end_date, fields=['industry_sw_level1'])
stock_list = list(df[df['industry_sw_level1'] == industry_code].instrument)
stock_pe_info = D.history_data(stock_list, start_date=start_date, end_date=end_date, fields=['name','pe_ttm'])
return stock_pe_info.pe_ttm.mean()
pe = {}
for dt in dates:
pe[dt] = calcu_pe(650000, dt)
T.plot(pd.DataFrame(pd.Series(pe)), title='市盈率走势')
2017年第一季度国防军工市盈率不断上行,但是在四月底五月初短短的时间内,市盈率较今年最高值下降了50%,即使这样也不能说明国防军工现在处于低估阶段。
绘制多个行业的归一化价格走势¶
In [45]:
adjusted_sw_level1 = ['SW'+str(i)+'.SHA' for i in sw_level1]
price_df = D.history_data(instruments=adjusted_sw_level1, start_date='2017-01-01', end_date='2017-08-07',
fields=['close'])
price_df = pd.pivot_table(price_df, values='close', index=['date'], columns=['instrument'])
T.plot(price_df.apply(lambda df:df/df[0]))
黑色的曲线是240000为有色金属,可以看出有色金属在4月以来不断走高!
看到这里,其实我们只是对国防军工有一个整体的把握,从数据来看,并不能判断国防军工板块是否处于低估阶段。历史不会简单地重复,投资不能盲目预测。