其他分享
首页 > 其他分享> > 利用tushare 研究A股的动量效应

利用tushare 研究A股的动量效应

作者:互联网

利用tushare 研究A股的动量效应

一、数据获取预处理?

1、首选选择沪深300前1个月(这个时间戳可以更改)累积收益排名前十的股票,同时收益排名最差的10只股票;持有时间1个月之后检查他们的收益情况。

2、注意事项在用tushare获取股票数据的时候记得要进行前复权,这里可以参考官网

前复权链接: https://tushare.pro/document/2?doc_id=109

二、使用步骤

1.引入库

代码如下(示例):

import pandas as pd
import tushare as ts
pro = ts.set_token('############')

## 2.读入数据
df_sum = pd.DataFrame()
df = pd.read_csv('沪深300.csv', sep=',')#这里.csv文件可以在东财上一键下载。

for code in df.loc[:, 'ts_code']:
    df1 =  ts.pro_bar(ts_code=code,start_date='20210101', end_date='20210201', freq='M', adj='qfq') #  pro.monthly(ts_code=code,start_date='20210101', end_date='20210201',fields='ts_code,trade_date,open,close,pct_chg')
    df1['sum']=df1['pct_chg'].sum()
    df_sum=pd.concat((df_sum,df1 ),axis=0, ignore_index=True, join="outer")
    
#按照累计月份收益进行排序
df_sum=df_sum.sort_values(by=['sum'],ascending=False)
#将处理好的数据保存在csv中,然后计算动量持有期间的累计收益。
df_sum.to_csv(str(1)+"个月动量.csv")

#接下来就是对上部分排序后的股票进行持有,并计算持有期的累计收益

import pandas as pd
import tushare as ts

pro = ts.set_token('#############')
df1 = pd.read_csv('1个月动量.csv',sep=',')

df_sum = pd.DataFrame()
df_sum1 = pd.DataFrame()
df_sum2 = pd.DataFrame()
df_sum3 = pd.DataFrame()
df_sum4 = pd.DataFrame()
df_sum5 = pd.DataFrame()
df_sum6 = pd.DataFrame()
df_sum7 = pd.DataFrame()
df_sum8 = pd.DataFrame()
df_sum9 = pd.DataFrame()
df_sum10 = pd.DataFrame()

#按照月度的收益情况进行排序。
print('持有期为1个月')
df = df1.head(10)
for code in df.loc[:,'ts_code']:
    df = ts.pro_bar(ts_code=code, start_date='20190201', end_date='20190301', freq='M', adj='qfq')
    df_sum7 = pd.concat((df_sum7, df), axis=0, ignore_index=True, join="outer")
df_sum7.loc['sum'] = df_sum7['pct_chg'].sum()
print(df_sum7)
#选择排名靠后的后10个个股
df = df1.tail(10)
for code in df.loc[:,'ts_code']:
    df = ts.pro_bar(ts_code=code, start_date='20190201', end_date='20190301', freq='M', adj='qfq')
    df_sum8 = pd.concat((df_sum8, df), axis=0, ignore_index=True, join="outer")
df_sum8.loc['sum'] = df_sum8['pct_chg'].sum()
print(df_sum8)
print('持有期为3个月')
df = df1.head(10)
for code in df.loc[:,'ts_code']:
    df = ts.pro_bar(ts_code=code, start_date='20190201', end_date='20190501', freq='M', adj='qfq')
    df_sum9 = pd.concat((df_sum9, df), axis=0, ignore_index=True, join="outer")
df_sum9.loc['sum'] = df_sum9['pct_chg'].sum()
print(df_sum9)
#选择排名靠后的后10个个股
df = df1.tail(10)
for code in df.loc[:,'ts_code']:
    df = ts.pro_bar(ts_code=code, start_date='20190201', end_date='20190501',freq='M', adj='qfq')
    df_sum10 = pd.concat((df_sum10, df), axis=0, ignore_index=True, join="outer")
df_sum10.loc['sum'] = df_sum10['pct_chg'].sum()
print(df_sum10)
print('持有期为6个月')
#选择排名靠前的前10个股,计算6个月之后的累计盈利(等权重)
df = df1.head(10)
for code in df.loc[:,'ts_code']:
    df = ts.pro_bar(ts_code=code, start_date='20190201', end_date='20190801', freq='M', adj='qfq')
    df_sum1 = pd.concat((df_sum1, df), axis=0, ignore_index=True, join="outer")
df_sum1.loc['sum'] = df_sum1['pct_chg'].sum()
print(df_sum1)
#选择排名靠后的后10个个股
df = df1.tail(10)
for code in df.loc[:,'ts_code']:
    df = ts.pro_bar(ts_code=code, start_date='20190201', end_date='20190801', freq='M', adj='qfq')
    df_sum2 = pd.concat((df_sum2, df), axis=0, ignore_index=True, join="outer")
df_sum2.loc['sum'] = df_sum2['pct_chg'].sum()
print(df_sum2)

print('持有期为9个月')
#9个月
df = df1.head(10)
for code in df.loc[:,'ts_code']:
    df = ts.pro_bar(ts_code=code, start_date='20190201', end_date='20191101', freq='M', adj='qfq')
    df_sum3 = pd.concat((df_sum3, df), axis=0, ignore_index=True, join="outer")
df_sum3.loc['sum'] = df_sum3['pct_chg'].sum()
print(df_sum3)
#选择排名靠后的后10个个股
df = df1.tail(10)
for code in df.loc[:,'ts_code']:
    df =ts.pro_bar(ts_code=code, start_date='20190201', end_date='20191101', freq='M', adj='qfq')
    df_sum4 = pd.concat((df_sum4, df), axis=0, ignore_index=True, join="outer")
df_sum4.loc['sum'] = df_sum4['pct_chg'].sum()
print(df_sum4)

#12个月
print('持有期为12个月')
df = df1.head(10)
for code in df.loc[:,'ts_code']:
    df = ts.pro_bar(ts_code=code, start_date='20190201', end_date='20200201', freq='M', adj='qfq')
    df_sum5 = pd.concat((df_sum5, df), axis=0, ignore_index=True, join="outer")
df_sum5.loc['sum'] = df_sum5['pct_chg'].sum()
print(df_sum5)
#选择排名靠后的后10个个股
df = df1.tail(10)
for code in df.loc[:,'ts_code']:
    df = ts.pro_bar(ts_code=code, start_date='20190201', end_date='20200201', freq='M', adj='qfq')
    df_sum6 = pd.concat((df_sum6, df), axis=0, ignore_index=True, join="outer")
df_sum6.loc['sum'] = df_sum6['pct_chg'].sum()
print(df_sum6)

结论:A股目前在位一个不成熟的市场,其动量效应并不明显,(下图的结果我没有进行前复权,但道理都是一样。)
在这里插入图片描述

标签:A股,tushare,code,df,sum,ts,动量,pd,date
来源: https://blog.csdn.net/weixin_56454587/article/details/123225183