diff --git a/1.gif b/1.gif new file mode 100644 index 0000000..90007ef Binary files /dev/null and b/1.gif differ diff --git "a/10\344\272\214\346\234\210\344\273\275\344\270\212\346\266\250\347\273\237\350\256\241\346\246\202\347\216\207.png" "b/10\344\272\214\346\234\210\344\273\275\344\270\212\346\266\250\347\273\237\350\256\241\346\246\202\347\216\207.png" new file mode 100644 index 0000000..972f5b8 Binary files /dev/null and "b/10\344\272\214\346\234\210\344\273\275\344\270\212\346\266\250\347\273\237\350\256\241\346\246\202\347\216\207.png" differ diff --git "a/1\344\270\212\350\257\201\346\214\207\346\225\260K\347\272\277\345\233\276 .png" "b/1\344\270\212\350\257\201\346\214\207\346\225\260K\347\272\277\345\233\276 .png" new file mode 100644 index 0000000..0edd06c Binary files /dev/null and "b/1\344\270\212\350\257\201\346\214\207\346\225\260K\347\272\277\345\233\276 .png" differ diff --git a/2.gif b/2.gif new file mode 100644 index 0000000..6b1926f Binary files /dev/null and b/2.gif differ diff --git "a/2\344\270\212\350\257\201\346\214\207\346\225\260\345\216\206\345\271\264\346\224\266\347\233\212\347\216\207(%).png" "b/2\344\270\212\350\257\201\346\214\207\346\225\260\345\216\206\345\271\264\346\224\266\347\233\212\347\216\207(%).png" new file mode 100644 index 0000000..8956970 Binary files /dev/null and "b/2\344\270\212\350\257\201\346\214\207\346\225\260\345\216\206\345\271\264\346\224\266\347\233\212\347\216\207(%).png" differ diff --git a/3.gif b/3.gif new file mode 100644 index 0000000..b68d1c6 Binary files /dev/null and b/3.gif differ diff --git "a/3\344\270\212\350\257\201\346\214\207\346\225\260\345\220\204\346\234\210\344\270\212\346\266\250\346\203\205\345\206\265.png" "b/3\344\270\212\350\257\201\346\214\207\346\225\260\345\220\204\346\234\210\344\270\212\346\266\250\346\203\205\345\206\265.png" new file mode 100644 index 0000000..ae10255 Binary files /dev/null and "b/3\344\270\212\350\257\201\346\214\207\346\225\260\345\220\204\346\234\210\344\270\212\346\266\250\346\203\205\345\206\265.png" differ diff --git "a/4\344\270\212\350\257\201\346\214\207\346\225\260\344\272\214\346\234\210\344\273\275\344\270\212\346\266\250\346\203\205\345\206\265.png" "b/4\344\270\212\350\257\201\346\214\207\346\225\260\344\272\214\346\234\210\344\273\275\344\270\212\346\266\250\346\203\205\345\206\265.png" new file mode 100644 index 0000000..33efb33 Binary files /dev/null and "b/4\344\270\212\350\257\201\346\214\207\346\225\260\344\272\214\346\234\210\344\273\275\344\270\212\346\266\250\346\203\205\345\206\265.png" differ diff --git "a/5\347\233\270\345\205\263\347\263\273\346\225\260\347\203\255\345\212\233\345\233\276.png" "b/5\347\233\270\345\205\263\347\263\273\346\225\260\347\203\255\345\212\233\345\233\276.png" new file mode 100644 index 0000000..c290172 Binary files /dev/null and "b/5\347\233\270\345\205\263\347\263\273\346\225\260\347\203\255\345\212\233\345\233\276.png" differ diff --git "a/6\346\214\207\346\225\260\350\265\260\345\212\277\346\203\205\345\206\265.png" "b/6\346\214\207\346\225\260\350\265\260\345\212\277\346\203\205\345\206\265.png" new file mode 100644 index 0000000..69f382b Binary files /dev/null and "b/6\346\214\207\346\225\260\350\265\260\345\212\277\346\203\205\345\206\265.png" differ diff --git "a/7\347\233\270\345\205\263\347\263\273\346\225\260\347\203\255\345\212\233\345\233\276.png" "b/7\347\233\270\345\205\263\347\263\273\346\225\260\347\203\255\345\212\233\345\233\276.png" new file mode 100644 index 0000000..8efc309 Binary files /dev/null and "b/7\347\233\270\345\205\263\347\263\273\346\225\260\347\203\255\345\212\233\345\233\276.png" differ diff --git "a/8\346\214\207\346\225\260\346\266\250\350\267\214\346\203\205\345\206\265.png" "b/8\346\214\207\346\225\260\346\266\250\350\267\214\346\203\205\345\206\265.png" new file mode 100644 index 0000000..2ef5a97 Binary files /dev/null and "b/8\346\214\207\346\225\260\346\266\250\350\267\214\346\203\205\345\206\265.png" differ diff --git "a/9\345\220\204\346\214\207\346\225\260\345\244\217\346\231\256\346\257\224\347\216\207(%) (1).png" "b/9\345\220\204\346\214\207\346\225\260\345\244\217\346\231\256\346\257\224\347\216\207(%) (1).png" new file mode 100644 index 0000000..79fd3d8 Binary files /dev/null and "b/9\345\220\204\346\214\207\346\225\260\345\244\217\346\231\256\346\257\224\347\216\207(%) (1).png" differ diff --git "a/\350\202\241\345\270\202\346\234\210\344\273\275\346\225\210\345\272\224\346\243\200\351\252\214.md" "b/\350\202\241\345\270\202\346\234\210\344\273\275\346\225\210\345\272\224\346\243\200\351\252\214.md" new file mode 100644 index 0000000..c542858 --- /dev/null +++ "b/\350\202\241\345\270\202\346\234\210\344\273\275\346\225\210\345\272\224\346\243\200\351\252\214.md" @@ -0,0 +1,96405 @@ + +### 月份效应 + +月份效应(Month-of-the-Year Effect) 实证研究发现,在大多数的证券市场中存在某个或某些特定月份的平均收益率年复一年显著地异于其他各月平均收益率的现象,这种市场异象被称作“月份效应”。 + +在美国的股票市场表现为“1月效应”,即1月份的平均收益率显著高于其他月份的平均收益。该现象最早由美国学者瓦切尔(Wachte1)于1942年发现,但直到1976年罗兹弗(Rozef)和金乃尔(Kinney)系统地将这一异象揭示出来,“1月效应”才逐渐进入现代金融学者的视野,并逐渐形成一套科学严谨的“月份效应”研究体系,包括“1月效应”主要体现在小规模公司的股票上,并相应地提出了“税减假说”等理论解释。 + +张兵给出了基于资金面季节性流动规律的简要解释,总结起来有两方面:第一,资金面的季节变化,我国股市年末往往面临着各种形式的资金抽回,而来年初这些资金又会回流,这一资金运动规律决定了股市的“春涨”、“冬藏”;第二,重大利好政策往在2、3月份发布,从而引发“春涨”现象 。 + + +```python +#先引入后面可能用到的包(package) +import pandas as pd +import numpy as np +import tushare as ts +from datetime import datetime +import matplotlib.pyplot as plt +%matplotlib inline + +#正常显示画图时出现的中文和负号 +from pylab import mpl +mpl.rcParams['font.sans-serif']=['SimHei'] +mpl.rcParams['axes.unicode_minus']=False +``` + + +```python +#获取数据函数 +def get_data(code,start_date): + df=ts.get_k_data(code,start_date) + df.index=pd.to_datetime(df.date) + return df +#计算日对数收益率 +def log_ret(df): + logret=np.log(df/df.shift(1))[1:] + return logret +#将日收益率转换为月收益率 +def month_rate(logret): + date=[] + d=list(logret.index) + for i in range(0,np.size(logret)): + t=''.join([d[i].strftime("%Y"),d[i].strftime("%m"),"01"]) + date.append(datetime.strptime(t,"%Y%m%d")) + y=pd.DataFrame(logret.values,date,columns=['月收益率']) + ret_M=y.groupby(y.index).sum() + return ret_M + +#将日收益率转换为年收益率 +def annual_rate(logret): + year=[] + d=list(logret.index) + for i in range(0,np.size(logret)): + year.append(d[i].strftime("%Y")) + y=pd.DataFrame(logret.values,year,columns=['年收益率']) + ret_Y=np.exp(y.groupby(y.index).sum())-1 + return ret_Y +``` + + +```python +#获取数据 +df=get_data('sh','1993-01-01') +#对数收益率 +logret=log_ret(df.close) +#月收益率 +ret_M=month_rate(logret) +#年收益率 +ret_Y=annual_rate(logret) +``` + +### 查看K线图 +使用pyecharts画狂拽酷炫的K线图(cmd上先安装:pip install pyecharts) + + +```python +from pyecharts import Kline +v1=list(df.loc[:,['open','close','low','high']].values) +v0=list(df.date) +kline = Kline("上证指数K线图",title_pos='center',title_text_size=15) +kline.add("", v0, v1,is_datazoom_show=True,mark_line=["average"], + mark_point=["max", "min"],mark_point_symbolsize=80, + mark_line_valuedim=['highest', 'lowest'] ) +#kline.render("上证指数图.html") +kline +``` + + + + + +
+ + + + + + + +### 年收益率情况 + + +```python +from pyecharts import Bar +attr = list(ret_Y.index) +v = list((ret_Y['年收益率']*100).round(2)) +bar = Bar("上证指数历年收益率(%)",title_text_size=15,title_pos='center') +bar.add("", attr, v,is_label_show=True, is_datazoom_show=True) +bar +``` + + + + + +
+ + + + + + + + +```python +ret_Y.describe().T +``` + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
countmeanstdmin25%50%75%max
年收益率26.00.1205130.436376-0.653941-0.151281-0.0077110.2745521.304334
+
+ + + + +```python +ret_Y.sort_values('年收益率')[:5].T +``` + + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
20081994201820112001
年收益率-0.653941-0.222991-0.218492-0.216753-0.20618
+
+ + + + +```python +ret_Y.sort_values('年收益率',ascending=False)[:5].T +``` + + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
20062007200919962014
年收益率1.3043340.9665930.7998250.6514250.528691
+
+ + + + +```python +ratio=int(ret_Y[ret_Y['年收益率']>0].count().values)/len(ret_Y) +print(f'上涨年份占比{ratio*100}%') +``` + + 上涨年份占比50.0% + + +从1993年至2018年共26年,上证指数有13年是下跌的,另外13年是上涨的,很fair哦,看来上证指数是属天秤座的。其中,跌幅最大的五年分别为:2008年(-65.39%),1994年(-22.30%),2018年(-21.85%),2011年(-21.68%),2001年(-20.62%);涨幅最大的五年分别为:2006年(130.43%),2007年(96.66%),2009年(79.98%),1996年(65.14%),2014(52.87%)。平均收益率35.12%,标准差为135.24%,可见,尽管A股市场波动性非常大,特别是前十年,但是整体上来看,并没有大家口中说的那么糟糕。但是为啥赚钱的永远是少数人呢?本质上是因为“知情者”总是是少数的,而财富遵循“幂律分布”(二八法则),此处不详细展开。 + +### 月份效应检验 +所谓“月份效应”,主要是指股票市场中存在某个或某些特定月份的平均收益率年复一年显著地异于其他各月平均收益率的现象。有实证研究表明,美国股票市场表现为“1月效应”,即1月份的平均收益率为正,且显著高于其他月份的平均收益。该现象最早由Wachte1(1942)发现,但直到1976年Rozef和Kinney系统地将这一异象揭示出来,“1月效应”才引起金融界的注意。美国等发达国家对“月份效应”的讨论和研究已走向成熟,但是由于A股运行时间较短,并且由于文化差异(节假日安排)导致的交易时间不一致,其“月份效应”特征可能异于我国,因此仍有必要进一步挖掘和探讨。 +检验思路:(1)将数据分成两组:m月份和其他月份;(2)检验两组数据的平均值是否相等 + + +```python +#每年不同月份收益率 +df_m=pd.DataFrame() +for i in range (1,13): + ret=ret_M[ret_M.index.month==i] + df_m[str(i)+'月份']=ret['月收益率'].values +df_m.describe().round(3) +``` + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
1月份2月份3月份4月份5月份6月份7月份8月份9月份10月份11月份12月份
count26.00026.00026.00026.00026.00026.00026.00026.00026.00026.00026.00026.000
mean0.0040.0220.0070.033-0.008-0.013-0.0140.016-0.001-0.0090.012-0.004
std0.1190.0450.1170.1140.1060.1130.1020.1890.0500.0890.0780.094
min-0.257-0.066-0.370-0.173-0.373-0.227-0.340-0.246-0.107-0.283-0.201-0.167
25%-0.056-0.008-0.024-0.025-0.062-0.077-0.049-0.044-0.039-0.047-0.042-0.056
50%0.0070.0240.0130.005-0.002-0.013-0.0020.004-0.003-0.0080.021-0.016
75%0.0510.0440.0720.0740.0480.0190.0240.0280.0390.0470.0520.042
max0.3870.1120.1710.3840.1890.2780.1570.8550.0780.1150.1900.243
+
+ + + + +```python +#除了某一年剩余年份收益率 +df_exm=pd.DataFrame() +for i in range (1,13): + ret=ret_M[ret_M.index.month!=i] + df_exm['ex'+str(i)]=ret['月收益率'].values +``` + + +```python +t1,p1=stats.ttest_1samp(df_m,0.0) +t2,p2=stats.ttest_ind(df_m,df_exm,equal_var=False) +``` + + +```python +l=np.array([t1,p1,t2,p2]).T +df=pd.DataFrame(l,index=range(1,13),columns=['单样本t检验_t值', + '单样本t检验_p值','双样本t检验_t值','双样本t检验_p值']) +df +``` + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
单样本t检验_t值单样本t检验_p值双样本t检验_t值双样本t检验_p值
10.1590870.8748780.0001860.999853
22.4438400.0219291.7864280.079309
30.3001720.7665280.1458480.885056
41.4943290.1476061.3963060.173210
5-0.3806790.706657-0.5840050.563606
6-0.6045270.550941-0.8096860.424691
7-0.6929030.494757-0.9134990.368209
80.4262530.6735710.3518070.727798
9-0.1196810.905692-0.4509110.653973
10-0.4851080.631823-0.7138510.480491
110.7777630.4440070.5402440.592507
12-0.2301750.819829-0.4438740.660195
+
+ + + + +```python +pro=[] +al=len(df_m) +for i in df_m.columns: + up=df_m[df_m[i]>0][i].count() + r=round((up/al)*100,2) + pro.append(r) +``` + + +```python +from pyecharts import Overlap, Bar, Line, Grid +grid = Grid() +attr = df_m.columns +v1 = pro +v2 = df_m.describe().loc['mean'].values +v2=(v2*100).round(2) +bar = Bar(title="上证指数各月上涨情况",title_text_size=15) +bar.add("各月上涨次数占比%", attr, v1,yaxis_max=80,is_label_show=True) + +line = Line() +line.add("各月收益率均值%", attr, v2,is_label_show=True) +overlap = Overlap() +overlap.add(bar) +overlap.add(line, is_add_yaxis=True, yaxis_index=1) + +grid.add(overlap, grid_right="17%") +grid +``` + + + + + +
+ + + + + + + + +```python +from pyecharts import Gauge +gauge = Gauge("上证指数二月份上涨情况",title_text_size=15,title_pos='center') +gauge.add("", "1993-2018年\n二月份上涨占比", 69.23) +gauge +``` + + + + + +
+ + + + + + + + +```python +import seaborn as sns +import matplotlib.pyplot as plt +def plot_corr(df): + dfData = df.corr() + plt.subplots(figsize=(9, 9)) + sns.heatmap(dfData, annot=True, vmax=1, square=True,cmap='Reds') + plt.savefig('相关系数热力图.png') + plt.show() +``` + + +```python +plot_corr(df_m) +``` + + +![png](output_22_0.png) + + +### 其他指数情况 + + +```python +codes=['sh','sz','hs300','sz50','zxb','cyb'] +``` + + +```python +zs=pd.DataFrame() +for code in codes: + zs[code]=get_data(code,'2011-01-01')['close'] +``` + + +```python +zs.head() +``` + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
shszhs300sz50zxbcyb
date
2011-01-042852.6512714.513189.682015.116896.761155.11
2011-01-052838.5912647.633175.661999.226865.591155.35
2011-01-062824.2012599.663159.641984.286804.591134.79
2011-01-072838.8012573.943166.622006.376770.151126.52
2011-01-102791.8112331.163108.191975.746584.041093.77
+
+ + + + +```python +def date_trans(date): + d=[] + for i in range(0,len(date)): + d.append(''.join([date[i].strftime("%Y"), + date[i].strftime("%m") + ,date[i].strftime("%d")])) + return d + +from pyecharts import Line +date=date_trans(zs.index) +v1=list(zs['sh'].values) +v2=list(zs['sz'].values) +v3=list(zs['hs300'].values) +v4=list(zs['sz50'].values) +v5=list(zs['zxb'].values) +v6=list(zs['cyb'].values) + +line = Line(title="") +line.add("上证指数",date,v1,line_width=2) +line.add("深证指数",date,v2,line_width=2) +line.add("沪深300",date,v3,line_width=2) +line.add("上证50",date,v4,line_width=2) +line.add("中小板",date,v5,line_width=2) +line.add("创业板",date,v6,line_width=2) + +line +``` + + + + + +
+ + + + + + + + +```python +#对数收益率 +zs_log=log_ret(zs) +d={'sh':'上证综指','sz':'深证综指','hs300':'沪深300','sz50':'上证50','zxb':'中小板','cyb':'创业板'} +zs_log.rename(columns=d,inplace=True) +``` + + +```python +zs_log.head() +``` + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
上证综指深证综指沪深300上证50中小板创业板
date
2011-01-05-0.004941-0.005274-0.004405-0.007917-0.0045300.000208
2011-01-06-0.005082-0.003800-0.005057-0.007501-0.008925-0.017956
2011-01-070.005156-0.0020430.0022070.011071-0.005074-0.007314
2011-01-10-0.016691-0.019497-0.018624-0.015384-0.027875-0.029503
2011-01-110.0043750.0043700.0053680.012084-0.010256-0.014189
+
+ + + + +```python +import seaborn as sns +import matplotlib.pyplot as plt +def plot_corr(df): + dfData = df.corr() + plt.subplots(figsize=(9, 9)) + sns.heatmap(dfData, annot=True, vmax=1, square=True,cmap='Blues') + plt.savefig('7相关系数热力图.png') + plt.show() +plot_corr(zs_log) +``` + + +![png](output_30_0.png) + + + +```python +#将日收益率转换为年收益率 +def annual_rate(logret): + year=[] + d=list(logret.index) + for i in range(0,len(logret)): + year.append(d[i].strftime("%Y")) + y=pd.DataFrame(logret.values,year) + ret_Y=np.exp(y.groupby(y.index).sum())-1 + return ret_Y +``` + + +```python +zs_Y=annual_rate(zs_log) +d={0:'上证综指',1:'深证综指',2:'沪深300',3:'上证50',4:'中小板',5:'创业板'} +zs_Y.rename(columns=d,inplace=True) +``` + + +```python +zs_Y.describe().T +``` + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
countmeanstdmin25%50%75%max
sh8.00.0102580.242815-0.228991-0.146920-0.0178990.0727170.528691
sz8.0-0.0375550.233311-0.309371-0.221955-0.0434730.1010780.356181
hs3008.00.0240030.255064-0.264585-0.139569-0.0103180.1110970.516594
sz508.00.0516790.284424-0.197260-0.153705-0.0587940.1740060.639321
zxb8.00.0031310.307310-0.377119-0.2545650.0414310.1693140.536997
cyb8.00.0978770.481090-0.368458-0.251478-0.0640850.3030280.844085
+
+ + + + +```python +bar = Bar(" ") + +attr = list(zs_Y.index) +v1 = list(((zs_Y['上证综指'].values)*100).round(2)) +v2 = list(((zs_Y['深证综指'].values)*100).round(2)) +v3 = list(((zs_Y['沪深300'].values)*100).round(2)) +v4 = list(((zs_Y['上证50'].values)*100).round(2)) +v5 = list(((zs_Y['中小板'].values)*100).round(2)) +v6 = list(((zs_Y['创业板'].values)*100).round(2)) + +bar.add("上证综指(%)", attr, v1,) +bar.add("深证综指(%)", attr, v2, ) +bar.add("沪深300(%)", attr, v3,) +bar.add("上证50(%)", attr, v4, ) +bar.add("中小板(%)", attr, v5,) +bar.add("创业板(%)", attr, v6, ) +bar +``` + + + + + +
+ + + + + + + + +```python +dec=zs_Y.describe().T +dec +``` + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
countmeanstdmin25%50%75%max
上证综指8.00.0102580.242815-0.228991-0.146920-0.0178990.0727170.528691
深证综指8.0-0.0375550.233311-0.309371-0.221955-0.0434730.1010780.356181
沪深3008.00.0240030.255064-0.264585-0.139569-0.0103180.1110970.516594
上证508.00.0516790.284424-0.197260-0.153705-0.0587940.1740060.639321
中小板8.00.0031310.307310-0.377119-0.2545650.0414310.1693140.536997
创业板8.00.0978770.481090-0.368458-0.251478-0.0640850.3030280.844085
+
+ + + + +```python +from pyecharts import Line,EffectScatter,Overlap +attr = list(dec.index) +v2=dec['std'].values*100 +#假设无风险收益率为2% +v1 = dec['mean'].values*100-2 +v=((v1/v2)*100).round(2) +line = Line('各指数夏普比率(%)',title_text_size=15,title_pos='center') +line.add('',attr,v) +es = EffectScatter() +es.add('',attr,v,effect_scale=6) + +overlop = Overlap() +overlop.add(line) +overlop.add(es) +overlop +``` + + + + + +
+ + + + + + + + +```python +zs_2=zs_M[zs_M.index.month==2] +zs_ex2=zs_M[zs_M.index.month==2] +``` + + +```python +cyb_df=get_data('cyb','2011-01-01') +#对数收益率 +cyb_logret=log_ret(cyb_df.close) +#月收益率 +cyb_ret_M=month_rate(cyb_logret) +#年收益率 +cyb_ret_Y=annual_rate(cyb_logret) +``` + + +```python +cyb_2=cyb_ret_M[cyb_ret_M.index.month==2] +cyb_ex2=cyb_ret_M[cyb_ret_M.index.month!=2] +``` + + +```python +up=cyb_2[cyb_2['月收益率']>0].count() +al=len(cyb_2) +up/al +``` + + + + + 月收益率 0.75 + dtype: float64 + + + + +```python +def up_ratio(code,m): + df=get_data(code,'2011-01-01') + logret=log_ret(df.close) + ret_M=month_rate(logret) + ret_2=ret_M[ret_M.index.month==m] + up=ret_2[ret_2['月收益率']>0].count() + al=len(ret_2) + return up/al +``` + + +```python +ratio=[] +for code in codes: + r=up_ratio(code,2) + ratio.append(r) +ratio +``` + + + + + [月收益率 0.625 + dtype: float64, 月收益率 0.5 + dtype: float64, 月收益率 0.5 + dtype: float64, 月收益率 0.5 + dtype: float64, 月收益率 0.75 + dtype: float64, 月收益率 0.75 + dtype: float64] + + + + +```python +from pyecharts import Pie + +pie = Pie('二月份上涨统计概率',"基于2011-2018年", title_pos='center',title_text_size=15) +pie.add("上证", ["上证综指\n上涨", "下跌"], [62.5, 37.5], center=[30, 30], radius=[18, 24], + label_pos='center', is_label_show=True, label_text_color=None, ) +pie.add("深证", ["深证综指\n上涨", "下跌"], [50, 50], center=[50, 30], radius=[18, 24], + label_pos='center', is_label_show=True, label_text_color=None) +pie.add("沪深300", ["沪深300\n上涨", "下跌"], [50, 50], center=[70, 30], radius=[18, 24], + label_pos='center', is_label_show=True, label_text_color=None) +pie.add("上证50", ["上证50\n上涨", "下跌"], [50, 50], center=[30, 70], radius=[18, 24], + label_pos='center', is_label_show=True, label_text_color=None) +pie.add("中小板", ["中小板\n上涨", "下跌"], [75, 25], center=[50, 70], radius=[18, 24], + label_pos='center', is_label_show=True, label_text_color=None) +pie.add("创业板", ["创业板\n上涨", "下跌"], [75, 25], center=[70, 70], radius=[18, 24], + label_pos='center', is_label_show=True, label_text_color=None,is_legend_show=False) + +pie +``` + + + + + +
+ + + + + + diff --git "a/\350\202\241\345\270\202\346\234\210\344\273\275\346\225\210\345\272\224\346\243\200\351\252\214.py" "b/\350\202\241\345\270\202\346\234\210\344\273\275\346\225\210\345\272\224\346\243\200\351\252\214.py" new file mode 100644 index 0000000..6325f3b --- /dev/null +++ "b/\350\202\241\345\270\202\346\234\210\344\273\275\346\225\210\345\272\224\346\243\200\351\252\214.py" @@ -0,0 +1,471 @@ + +# coding: utf-8 + +# ### 月份效应 +# +# 月份效应(Month-of-the-Year Effect) 实证研究发现,在大多数的证券市场中存在某个或某些特定月份的平均收益率年复一年显著地异于其他各月平均收益率的现象,这种市场异象被称作“月份效应”。 +# +# 在美国的股票市场表现为“1月效应”,即1月份的平均收益率显著高于其他月份的平均收益。该现象最早由美国学者瓦切尔(Wachte1)于1942年发现,但直到1976年罗兹弗(Rozef)和金乃尔(Kinney)系统地将这一异象揭示出来,“1月效应”才逐渐进入现代金融学者的视野,并逐渐形成一套科学严谨的“月份效应”研究体系,包括“1月效应”主要体现在小规模公司的股票上,并相应地提出了“税减假说”等理论解释。 +# +# 张兵给出了基于资金面季节性流动规律的简要解释,总结起来有两方面:第一,资金面的季节变化,我国股市年末往往面临着各种形式的资金抽回,而来年初这些资金又会回流,这一资金运动规律决定了股市的“春涨”、“冬藏”;第二,重大利好政策往在2、3月份发布,从而引发“春涨”现象 。 + +# In[1]: + + +#先引入后面可能用到的包(package) +import pandas as pd +import numpy as np +import tushare as ts +from datetime import datetime +import matplotlib.pyplot as plt +get_ipython().run_line_magic('matplotlib', 'inline') + +#正常显示画图时出现的中文和负号 +from pylab import mpl +mpl.rcParams['font.sans-serif']=['SimHei'] +mpl.rcParams['axes.unicode_minus']=False + + +# In[282]: + + +#获取数据函数 +def get_data(code,start_date): + df=ts.get_k_data(code,start_date) + df.index=pd.to_datetime(df.date) + return df +#计算日对数收益率 +def log_ret(df): + logret=np.log(df/df.shift(1))[1:] + return logret +#将日收益率转换为月收益率 +def month_rate(logret): + date=[] + d=list(logret.index) + for i in range(0,np.size(logret)): + t=''.join([d[i].strftime("%Y"),d[i].strftime("%m"),"01"]) + date.append(datetime.strptime(t,"%Y%m%d")) + y=pd.DataFrame(logret.values,date,columns=['月收益率']) + ret_M=y.groupby(y.index).sum() + return ret_M + +#将日收益率转换为年收益率 +def annual_rate(logret): + year=[] + d=list(logret.index) + for i in range(0,np.size(logret)): + year.append(d[i].strftime("%Y")) + y=pd.DataFrame(logret.values,year,columns=['年收益率']) + ret_Y=np.exp(y.groupby(y.index).sum())-1 + return ret_Y + + +# In[283]: + + +#获取数据 +df=get_data('sh','1993-01-01') +#对数收益率 +logret=log_ret(df.close) +#月收益率 +ret_M=month_rate(logret) +#年收益率 +ret_Y=annual_rate(logret) + + +# ### 查看K线图 +# 使用pyecharts画狂拽酷炫的K线图(cmd上先安装:pip install pyecharts) + +# In[478]: + + +from pyecharts import Kline +v1=list(df.loc[:,['open','close','low','high']].values) +v0=list(df.date) +kline = Kline("上证指数K线图",title_pos='center',title_text_size=15) +kline.add("", v0, v1,is_datazoom_show=True,mark_line=["average"], + mark_point=["max", "min"],mark_point_symbolsize=80, + mark_line_valuedim=['highest', 'lowest'] ) +#kline.render("上证指数图.html") +kline + + +# ### 年收益率情况 + +# In[480]: + + +from pyecharts import Bar +attr = list(ret_Y.index) +v = list((ret_Y['年收益率']*100).round(2)) +bar = Bar("上证指数历年收益率(%)",title_text_size=15,title_pos='center') +bar.add("", attr, v,is_label_show=True, is_datazoom_show=True) +bar + + +# In[120]: + + +ret_Y.describe().T + + +# In[150]: + + +ret_Y.sort_values('年收益率')[:5].T + + +# In[151]: + + +ret_Y.sort_values('年收益率',ascending=False)[:5].T + + +# In[122]: + + +ratio=int(ret_Y[ret_Y['年收益率']>0].count().values)/len(ret_Y) +print(f'上涨年份占比{ratio*100}%') + + +# 从1993年至2018年共26年,上证指数有13年是下跌的,另外13年是上涨的,很fair哦,看来上证指数是属天秤座的。其中,跌幅最大的五年分别为:2008年(-65.39%),1994年(-22.30%),2018年(-21.85%),2011年(-21.68%),2001年(-20.62%);涨幅最大的五年分别为:2006年(130.43%),2007年(96.66%),2009年(79.98%),1996年(65.14%),2014(52.87%)。平均收益率35.12%,标准差为135.24%,可见,尽管A股市场波动性非常大,特别是前十年,但是整体上来看,并没有大家口中说的那么糟糕。但是为啥赚钱的永远是少数人呢?本质上是因为“知情者”总是是少数的,而财富遵循“幂律分布”(二八法则),此处不详细展开。 + +# ### 月份效应检验 +# 所谓“月份效应”,主要是指股票市场中存在某个或某些特定月份的平均收益率年复一年显著地异于其他各月平均收益率的现象。有实证研究表明,美国股票市场表现为“1月效应”,即1月份的平均收益率为正,且显著高于其他月份的平均收益。该现象最早由Wachte1(1942)发现,但直到1976年Rozef和Kinney系统地将这一异象揭示出来,“1月效应”才引起金融界的注意。美国等发达国家对“月份效应”的讨论和研究已走向成熟,但是由于A股运行时间较短,并且由于文化差异(节假日安排)导致的交易时间不一致,其“月份效应”特征可能异于我国,因此仍有必要进一步挖掘和探讨。 +# 检验思路:(1)将数据分成两组:m月份和其他月份;(2)检验两组数据的平均值是否相等 + +# In[172]: + + +#每年不同月份收益率 +df_m=pd.DataFrame() +for i in range (1,13): + ret=ret_M[ret_M.index.month==i] + df_m[str(i)+'月份']=ret['月收益率'].values +df_m.describe().round(3) + + +# In[155]: + + +#除了某一年剩余年份收益率 +df_exm=pd.DataFrame() +for i in range (1,13): + ret=ret_M[ret_M.index.month!=i] + df_exm['ex'+str(i)]=ret['月收益率'].values + + +# In[152]: + + +t1,p1=stats.ttest_1samp(df_m,0.0) +t2,p2=stats.ttest_ind(df_m,df_exm,equal_var=False) + + +# In[154]: + + +l=np.array([t1,p1,t2,p2]).T +df=pd.DataFrame(l,index=range(1,13),columns=['单样本t检验_t值', + '单样本t检验_p值','双样本t检验_t值','双样本t检验_p值']) +df + + +# In[475]: + + +pro=[] +al=len(df_m) +for i in df_m.columns: + up=df_m[df_m[i]>0][i].count() + r=round((up/al)*100,2) + pro.append(r) + + +# In[482]: + + +from pyecharts import Overlap, Bar, Line, Grid +grid = Grid() +attr = df_m.columns +v1 = pro +v2 = df_m.describe().loc['mean'].values +v2=(v2*100).round(2) +bar = Bar(title="上证指数各月上涨情况",title_text_size=15) +bar.add("各月上涨次数占比%", attr, v1,yaxis_max=80,is_label_show=True) + +line = Line() +line.add("各月收益率均值%", attr, v2,is_label_show=True) +overlap = Overlap() +overlap.add(bar) +overlap.add(line, is_add_yaxis=True, yaxis_index=1) + +grid.add(overlap, grid_right="17%") +grid + + +# In[443]: + + +from pyecharts import Gauge +gauge = Gauge("上证指数二月份上涨情况",title_text_size=15,title_pos='center') +gauge.add("", "1993-2018年\n二月份上涨占比", 69.23) +gauge + + +# In[484]: + + +import seaborn as sns +import matplotlib.pyplot as plt +def plot_corr(df): + dfData = df.corr() + plt.subplots(figsize=(9, 9)) + sns.heatmap(dfData, annot=True, vmax=1, square=True,cmap='Reds') + plt.savefig('相关系数热力图.png') + plt.show() + + +# In[485]: + + +plot_corr(df_m) + + +# ### 其他指数情况 + +# In[254]: + + +codes=['sh','sz','hs300','sz50','zxb','cyb'] + + +# In[256]: + + +zs=pd.DataFrame() +for code in codes: + zs[code]=get_data(code,'2011-01-01')['close'] + + +# In[259]: + + +zs.head() + + +# In[277]: + + +def date_trans(date): + d=[] + for i in range(0,len(date)): + d.append(''.join([date[i].strftime("%Y"), + date[i].strftime("%m") + ,date[i].strftime("%d")])) + return d + +from pyecharts import Line +date=date_trans(zs.index) +v1=list(zs['sh'].values) +v2=list(zs['sz'].values) +v3=list(zs['hs300'].values) +v4=list(zs['sz50'].values) +v5=list(zs['zxb'].values) +v6=list(zs['cyb'].values) + +line = Line(title="") +line.add("上证指数",date,v1,line_width=2) +line.add("深证指数",date,v2,line_width=2) +line.add("沪深300",date,v3,line_width=2) +line.add("上证50",date,v4,line_width=2) +line.add("中小板",date,v5,line_width=2) +line.add("创业板",date,v6,line_width=2) + +line + + +# In[342]: + + +#对数收益率 +zs_log=log_ret(zs) +d={'sh':'上证综指','sz':'深证综指','hs300':'沪深300','sz50':'上证50','zxb':'中小板','cyb':'创业板'} +zs_log.rename(columns=d,inplace=True) + + +# In[343]: + + +zs_log.head() + + +# In[487]: + + +import seaborn as sns +import matplotlib.pyplot as plt +def plot_corr(df): + dfData = df.corr() + plt.subplots(figsize=(9, 9)) + sns.heatmap(dfData, annot=True, vmax=1, square=True,cmap='Blues') + plt.savefig('7相关系数热力图.png') + plt.show() +plot_corr(zs_log) + + +# In[319]: + + +#将日收益率转换为年收益率 +def annual_rate(logret): + year=[] + d=list(logret.index) + for i in range(0,len(logret)): + year.append(d[i].strftime("%Y")) + y=pd.DataFrame(logret.values,year) + ret_Y=np.exp(y.groupby(y.index).sum())-1 + return ret_Y + + +# In[345]: + + +zs_Y=annual_rate(zs_log) +d={0:'上证综指',1:'深证综指',2:'沪深300',3:'上证50',4:'中小板',5:'创业板'} +zs_Y.rename(columns=d,inplace=True) + + +# In[337]: + + +zs_Y.describe().T + + +# In[351]: + + +bar = Bar(" ") + +attr = list(zs_Y.index) +v1 = list(((zs_Y['上证综指'].values)*100).round(2)) +v2 = list(((zs_Y['深证综指'].values)*100).round(2)) +v3 = list(((zs_Y['沪深300'].values)*100).round(2)) +v4 = list(((zs_Y['上证50'].values)*100).round(2)) +v5 = list(((zs_Y['中小板'].values)*100).round(2)) +v6 = list(((zs_Y['创业板'].values)*100).round(2)) + +bar.add("上证综指(%)", attr, v1,) +bar.add("深证综指(%)", attr, v2, ) +bar.add("沪深300(%)", attr, v3,) +bar.add("上证50(%)", attr, v4, ) +bar.add("中小板(%)", attr, v5,) +bar.add("创业板(%)", attr, v6, ) +bar + + +# In[416]: + + +dec=zs_Y.describe().T +dec + + +# In[442]: + + +from pyecharts import Line,EffectScatter,Overlap +attr = list(dec.index) +v2=dec['std'].values*100 +#假设无风险收益率为2% +v1 = dec['mean'].values*100-2 +v=((v1/v2)*100).round(2) +line = Line('各指数夏普比率(%)',title_text_size=15,title_pos='center') +line.add('',attr,v) +es = EffectScatter() +es.add('',attr,v,effect_scale=6) + +overlop = Overlap() +overlop.add(line) +overlop.add(es) +overlop + + +# In[352]: + + +zs_2=zs_M[zs_M.index.month==2] +zs_ex2=zs_M[zs_M.index.month==2] + + +# In[357]: + + +cyb_df=get_data('cyb','2011-01-01') +#对数收益率 +cyb_logret=log_ret(cyb_df.close) +#月收益率 +cyb_ret_M=month_rate(cyb_logret) +#年收益率 +cyb_ret_Y=annual_rate(cyb_logret) + + +# In[382]: + + +cyb_2=cyb_ret_M[cyb_ret_M.index.month==2] +cyb_ex2=cyb_ret_M[cyb_ret_M.index.month!=2] + + +# In[383]: + + +up=cyb_2[cyb_2['月收益率']>0].count() +al=len(cyb_2) +up/al + + +# In[411]: + + +def up_ratio(code,m): + df=get_data(code,'2011-01-01') + logret=log_ret(df.close) + ret_M=month_rate(logret) + ret_2=ret_M[ret_M.index.month==m] + up=ret_2[ret_2['月收益率']>0].count() + al=len(ret_2) + return up/al + + +# In[412]: + + +ratio=[] +for code in codes: + r=up_ratio(code,2) + ratio.append(r) +ratio + + +# In[410]: + + +from pyecharts import Pie + +pie = Pie('二月份上涨统计概率',"基于2011-2018年", title_pos='center',title_text_size=15) +pie.add("上证", ["上证综指\n上涨", "下跌"], [62.5, 37.5], center=[30, 30], radius=[18, 24], + label_pos='center', is_label_show=True, label_text_color=None, ) +pie.add("深证", ["深证综指\n上涨", "下跌"], [50, 50], center=[50, 30], radius=[18, 24], + label_pos='center', is_label_show=True, label_text_color=None) +pie.add("沪深300", ["沪深300\n上涨", "下跌"], [50, 50], center=[70, 30], radius=[18, 24], + label_pos='center', is_label_show=True, label_text_color=None) +pie.add("上证50", ["上证50\n上涨", "下跌"], [50, 50], center=[30, 70], radius=[18, 24], + label_pos='center', is_label_show=True, label_text_color=None) +pie.add("中小板", ["中小板\n上涨", "下跌"], [75, 25], center=[50, 70], radius=[18, 24], + label_pos='center', is_label_show=True, label_text_color=None) +pie.add("创业板", ["创业板\n上涨", "下跌"], [75, 25], center=[70, 70], radius=[18, 24], + label_pos='center', is_label_show=True, label_text_color=None,is_legend_show=False) + +pie + diff --git "a/\350\241\2501.jpg" "b/\350\241\2501.jpg" new file mode 100644 index 0000000..1743c50 Binary files /dev/null and "b/\350\241\2501.jpg" differ diff --git "a/\350\241\2502.jpg" "b/\350\241\2502.jpg" new file mode 100644 index 0000000..ea92849 Binary files /dev/null and "b/\350\241\2502.jpg" differ diff --git "a/\350\241\2503.jpg" "b/\350\241\2503.jpg" new file mode 100644 index 0000000..2dbe9db Binary files /dev/null and "b/\350\241\2503.jpg" differ