用Python分析A股市场的时间效应

刚过去的2018年,中国A股市场可谓不堪回首,在全球主要股票市场中收益率垫底。2019年刚过完元旦假期,中国有讲求开门大吉的传统,股市如果开门红,心理上认为是个好的开始,讨了个好兆头。但是2019年的第一个交易日,A股上证综指、深证成指、创业板指均跌逾1%,感觉有些心塞:smile:。如果股市开门红,当年就大概率会上涨吗?如果开门绿,当年大概率会下跌吗?不论基本面和消息面上的原因,我们用数据来回答。

1
2
3
import tushare as ts
import pandas as pd
pro = ts.pro_api('你的接口token')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 获取月度数据函数
def stock_by_month(indx='000001.SH',start_year=1991,end_year=2018,ftable=False,
m_list = ['01','02','03','04','05','06','07','08','09','10','11','12']):

# 设定参数
tabletitle = []
for i in m_list:
tabletitle.append(str(int(i))+'月')

# 初始化

df = pd.DataFrame(columns=tabletitle,
index=range(start_year,end_year+1))

# 获取指数历史行情数据
df_hq = pro.index_daily(ts_code=indx, start_date=str(start_year)+'0101',
end_date=str(end_year)+'1231')

# df_hq = pd.read_csv("./data/stock_d_h.csv", encoding='utf8')
# df_hq.to_csv("./data/stock_d_h.csv",encoding="utf8")

# 遍历计算每年情况
for y in range(start_year,end_year+1):
year = str(y)
df_y_hq = df_hq[df_hq['trade_date'].str.contains(year)].reset_index(drop = True)

keydate_list = []
for month in m_list:

bool1 = df_y_hq['trade_date'].str.contains(year + month)
df_temp = df_y_hq[bool1]
if len(df_temp)<2:
continue
df_temp = df_temp.sort_values('trade_date',ascending = False).reset_index(drop = True)

rows = len(df_temp) - 1
keydate_list.append(df_temp.loc[rows,'trade_date'])
keydate_list.append(df_temp.loc[0,'trade_date'])

# 获取关键交易日行情数据
df_temp = df_y_hq[df_y_hq.trade_date.isin(keydate_list)] \
.reset_index(drop = True)
df_temp = df_temp.sort_values('trade_date',ascending = True).reset_index(drop = True)

if len(df_temp)<len(m_list)*2:
continue

# 计算涨跌幅

for i in range(len(tabletitle)):

df.loc[y,tabletitle[i]] = (df_temp.loc[i*2+1,'close'] - df_temp.loc[i*2,'close']) \
/ df_temp.loc[i*2,'close']

df.loc[y,'首日'] = df_temp.loc[0,'pct_chg']/100
df.loc[y,'全年'] = (df_temp.loc[len(df_temp)-1,'close'] - df_temp.loc[0,'close']) \
/ df_temp.loc[0,'close']

df = df.dropna()

# 格式化
if ftable==True:
for cls in df.columns:
df[cls] = df[cls].apply(lambda x: '%.0f%%' % (x*100))

return df

开门红重要吗

我们根据1991-2018年的28年数据统计发现,从上证综指、深成指,创业板指三大指数分别来看,首日上涨、全年上涨的概率并不高,最高是深成指的57%,首日下跌,全年下跌的概率最高也是深成指(62%),但上证综指和创业板的表现不显著。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
d_index = {'上证综指':'000001.SH','深成指':'399001.SZ','沪深300':'000300.SH',
'创业板指':'399006.SZ','中证500':'000905.SH'}

index_list = ['上证综指','深成指','创业板指']

df_tj = pd.DataFrame(index=index_list)

for indxname in index_list:

df = stock_by_month(indx=d_index[indxname],ftable=False)

# 股票指数名称
print('{:-^24}'.format(indxname))

# 首日上涨,全年上涨的概率
prob1 = len(df[(df.首日 > 0) & (df.全年 > 0)]) / len(df[df.首日 > 0])
print('首日上涨,全年上涨的概率:','{:.0%}'.format(prob1))

# 首日下跌,全年下跌的概率
prob2 = len(df[(df.首日 < 0) & (df.全年 < 0)]) / len(df[df.首日 < 0])
print('首日下跌,全年下跌的概率:','{:.0%}'.format(prob2))
----------上证综指----------
首日上涨,全年上涨的概率: 56%
首日下跌,全年下跌的概率: 50%
----------深成指-----------
首日上涨,全年上涨的概率: 57%
首日下跌,全年下跌的概率: 62%
----------创业板指----------
首日上涨,全年上涨的概率: 25%
首日下跌,全年下跌的概率: 25%

月份效应真的存在吗

我们再看看月历效应是否存在,我们依然选取三大指数作为分析基准。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
d_index = {'上证综指':'000001.SH','深成指':'399001.SZ','沪深300':'000300.SH',
'创业板指':'399006.SZ','中证500':'000905.SH'}

index_list = ['上证综指','深成指','创业板指']

df_tj = pd.DataFrame(index=index_list)

for indxname in index_list:

df = stock_by_month(indx=d_index[indxname],ftable=False)

for cls in df.columns.values.tolist():

df_tj.loc[indxname,cls] = len(df[df[cls]>0])/len(df[cls])

for cls in df_tj.columns:
df_tj[cls] = df_tj[cls].apply(lambda x: '%.0f%%' % (x*100))

df_tj
1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月 首日 全年
上证综指 57% 79% 54% 46% 54% 46% 54% 50% 46% 46% 64% 39% 64% 54%
深成指 48% 67% 59% 52% 48% 44% 48% 48% 44% 48% 59% 41% 52% 48%
创业板指 38% 75% 38% 25% 50% 38% 25% 50% 50% 38% 62% 38% 50% 50%

统计发现,三大指数2月上涨概率最高,均在65%以上,尤其是上证综指达到78%。其次是11月,总体约在59%以上。最后给出上证综指的月份涨跌幅详细数据及代码,供大家参考。

1
2
3
4
5
6
7
8
d_index = {'上证综指':'000001.SH','深成指':'399001.SZ','沪深300':'000300.SH',
'创业板指':'399006.SZ','中证500':'000905.SH'}

index_name = '上证综指'

df = stock_by_month(indx=d_index[index_name],ftable=True)

df
1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月 首日 全年
1991 1% 3% -9% -6% 1% 19% 5% 23% 0% 20% 18% 12% 1% 127%
1992 7% 16% 4% 17% 172% 1% -12% -20% -15% -25% 52% 9% 0% 166%
1993 47% 5% -23% 43% -31% 8% -12% 6% -0% -9% 21% -14% 4% 2%
1994 -8% 2% -7% -15% -8% -14% -27% 76% 0% -7% 1% -5% 0% -22%
1995 -12% 3% 16% -12% 21% -11% 13% 1% -1% -0% -11% -12% -1% -13%
1996 -0% 3% -8% 20% -2% 25% 8% -3% 9% 13% 11% -19% -3% 70%
1997 5% 6% 16% 12% -11% -6% -1% 2% -11% 7% -3% 5% 0% 30%
1998 0% -3% 5% 7% 4% -5% 0% -12% 9% -1% 2% -7% 2% -6%
1999 1% -3% 5% -4% 15% 29% 3% 1% -2% -2% -4% -6% -2% 21%
2000 9% 2% 6% 2% 3% 1% 7% -0% -4% 2% 5% -0% 3% 47%
2001 -2% -2% 8% -0% 4% -0% -13% -8% -3% -2% 2% -7% 1% -22%
2002 -7% 3% 7% 4% -8% 17% -4% 1% -5% -4% -5% -3% -2% -16%
2003 14% 2% -1% -0% 3% -6% -1% -4% -6% -2% 2% 4% -3% 13%
2004 5% 3% 3% -9% -0% -11% -4% -2% 6% -7% 3% -5% 1% -17%
2005 -4% 10% -9% -5% -6% 4% 3% 7% -2% -4% 1% 6% -2% -7%
2006 7% 1% -1% 9% 10% -1% -5% 4% 7% 3% 13% 27% 2% 127%
2007 3% 3% 14% 18% 4% -5% 17% 21% 4% 5% -18% 8% 2% 94%
2008 -17% 1% -22% 11% -9% -21% 5% -14% -1% -20% 9% -4% 0% -65%
2009 6% 4% 13% 3% 3% 9% 13% -23% 4% 3% 4% 1% 3% 74%
2010 -8% 4% 1% -9% -9% -7% 11% -1% 1% 9% -8% -1% -1% -13%
2011 -2% 4% 0% -2% -6% 1% -2% -5% -8% 5% -6% -8% 2% -23%
2012 6% 7% -7% 4% -3% -6% -6% -4% 1% -0% -6% 16% -1% 5%
2013 5% -2% -5% -3% 6% -14% -0% 3% 4% -3% 3% -4% 0% -7%
2014 -4% 1% -2% -1% 1% 0% 7% 1% 6% 2% 10% 21% -0% 53%
2015 -4% 6% 12% 17% 3% -11% -10% -12% -4% 8% 4% 2% 4% 6%
2016 -17% -0% 10% -2% -3% 1% 2% 4% -2% 2% 4% -5% -7% -6%
2017 1% 3% -1% -4% -1% 3% 2% 2% -1% 1% -2% -0% 1% 5%
2018 4% -5% -3% -3% 0% -7% 4% -4% 4% -4% -1% -6% 1% -26%
如果文章对您有用请随意打赏,谢谢支持!