探索沪深300指数(HS300)——基于Python(中)
【承接上文】
写在前面:本文只做分析,提供观点,不构成投资建议
如需转载请微信联系:eosO_oke
佛系更新,关注不迷路
沪深300指数是A股市场中比较具有代表性的指数之一,于2005年4月8日正式推出。2005年之前沪深两个市场各自均有独立的综合指数和成分指数,但市场缺乏反映沪深市场整体走势的跨市场指数,沪深300指数应运而生。
沪深300指数是反映沪深两个市场整体走势的“晴雨表”,指数样本覆盖了两市大部分流通股。沪深300成分股均为市场中代表性好、流动性高、交易活跃的主流投资股票,多为蓝筹股或白马股,能够反映市场主流投资的收益情况。
思路:
· 沪深300近15年走势图(2005-20年)
· 沪深300历次重大事件中的表现情况
· 收益是否遵从正态分布
· 每年收益率展示
· 月历效应
· 日历效应
前文我们分析了沪深300历史走势图、分析了众多历史事件中指数的表现、也查看了收益的分布情况。
本节继续探索沪深300指数收益情况,将分别使用柱状图和饼图来进行分析,重点分析【月历效应】
01 年度收益与年平均收益
(1)首先我们剥离日期索引中的年月日
- #先把原数据的日期中的年、月、日剥离出来(后续工作可能用到)
- hs300["year"] = hs300.index.year
- hs300['month'] = hs300.index.month
- hs300['week'] = hs300.index.strftime('%w') #注week要用strftime('%w')的方法
- hs300['day'] = hs300.index.day
- hs300[['year','month','week','day']].tail(20) #仅查看剥离出来的四列的最后20个值
输出结果为(其中的week表示当前交易日为周几,因此没有6和7):
- year month week day
- date
- 2020-03-19 2020 3 4 19
- 2020-03-20 2020 3 5 20
- 2020-03-23 2020 3 1 23
- 2020-03-24 2020 3 2 24
- 2020-03-25 2020 3 3 25
- 2020-03-26 2020 3 4 26
- 2020-03-27 2020 3 5 27
- 2020-03-30 2020 3 1 30
- 2020-03-31 2020 3 2 31
- 2020-04-01 2020 4 3 1
- #15年来每一年各自的收益(乘以100是为了去掉%,下同)
- return_by_year = pd.DataFrame(round(hs300['return'].groupby(hs300.year).sum()*100,2))
- #15年来每年的平均收益
- average_return = np.mean(return_by_year['return'])
- #年度收益正负分开
- return_by_year['positive'] = return_by_year > 0
(3)年化收益率大于0 和小于0各自占比(绘制饼状图)
- data = return_by_year['positive'].value_counts().tolist()
- pos_neg = pd.DataFrame(data=data,columns=['counts'],index=['positive','negative'])
- plt.figure(figsize=(15,15))
- pos_neg.counts.plot(kind='pie',colors=['green','red'],autopct='%1.1f%%',startangle=140)
- plt.title('年化收益率正负各自占比',size=(20))
- plt.legend(loc='upper right',fontsize=10,ncol=2,fancybox=True,framealpha=0.5,shadow=True) #两列展示图例
此图作用不大,背后的意义也不大,但是食之无味弃之可惜,展示出来权当是占个位置来学做饼图吧:
输出结果为:
(4)各月份平均收益图(A股月历效应)
- return_by_month = pd.DataFrame(round(hs300['return'].groupby(hs300.month).mean()*100,2))
- return_by_month['positive'] = return_by_month['return']>0
- plt.figure(figsize=(20,15))
- return_by_month['return'].plot(kind='bar',color=return_by_month.positive.map({True:'red',False:'forestgreen'}))
- plt.title("月度平均收益图(%)",size=20)
- plt.xlabel('月份',size=20)
- plt.ylabel('每月平均收益',size=20)
- plt.yticks(np.arange(-0.25,0.25,step=0.025),size=15)
- plt.xticks(rotation=45,size=15)
- plt.grid(alpha=0.3)
输出结果为:
从结果可以看出,除了一月平均收益为0,6月和8月平均收益为负之外,其他九个月的平均收益均为正。收益率在2月和12月份达到最高,平均0.2%,月历效应背后的原因大家可以上网去查,各有各的说法,我是想说,含有我们最讨厌的数字2,4的月份,表现的反而最好,我们最喜欢的6,8数字的月份,表现最差,是的,它很有个性,这很A股
。
(5)绘制每个月收益图,具体查看月历效应
- #合并年和月,查看15年*12个月 每个月的收益相对于月份平均收益的走势图
- hs300['year_month'] = hs300['year'].map(str)+'年'+hs300['month'].map(str)+'月'
- hs300['year_month'].tail(20)
- month_return = pd.DataFrame(round(hs300['return'].groupby(hs300['year_month']).sum()*100,2))
- month_return['positive'] = month_return['return']>0
- #计算平均月收益
- average_month_return = np.mean(month_return['return'])
- plt.figure(figsize=(20,10))
- month_return['return'].plot(kind='bar',color=month_return['positive'].map({True:'red',False:'green'}),label='Monthly Return')
- plt.axhline(y=average_month_return, color='blue',linestyle='-',label='Average Return')
- plt.title('每月收益率与月平均收益率对比',size=20)
- plt.xlabel('月份',size=20)
- plt.ylabel('收益',size=20)
- month_return['n'] = range(len(month_return)) #解释同133行代码
- plt.xticks(month_return['n'][::10],month_return.index[::10],rotation=45) #解释同上
- plt.yticks(np.arange(-50,50,step=10))
- plt.legend(ncol=2)
大家可以通过让x轴更加密集的方式,来查看是否大部分年份里每年的2,4,12月份收益最高,6,8月收益最低。
下期来看看每周哪一天交易日里收益最好。
昨天收到“管友”催更的信息了,本人也是激动的一P,说明帖子有人看,但是事情较多,只能佛系更新,下期不定时展示;
***关注一下 ,不做尘世中迷途的小书童
未完待续


雷达卡





京公网安备 11010802022788号







