碧沼

V1

2023/01/09阅读:32主题:橙心

atr因子

如何构造异常周转率(atr)变量

前言

本文记录复现JFE上Machine learning in the Chinese stock market(中国股市中的机器学习)这篇文章,异常周转率(abnormal turnover ratio)因子的构造。

如果您对我的内容感兴趣,欢迎后台私信进行交流。

1.ATR因子

图1 ATR因子的定义
图1 ATR因子的定义

2.因子计算

def Get_atr(trading_df,ann_df,sh_df,sz_df):
    '''
    异常周转率——月度指标
    需要设定一个dummy  
    公司重大事项公告,前后三天的交易日设定为1,其余为0
    重大事项包括:季报、半年报、年报发布、股票回购、大股东交易、重大资本投资、债券发行、股份制改革、重大管理人员变动、分红股利  (原文应该是用wind获取的公告数据)
    构造方式:
    被解释变量: 个股的换手率日频
    解释变量: 市场的总体换手率+个股重大事项的dummy(一个事件构造一个)+截距项
    对上述回归方程计算每日的残差,在月度层面进行残差求和得到该月的异常周转率。
    回归时间: t-7月-t-1月 6个月日频数据
    '''

    try:
        code = int(trading_df['股票代码'].iloc[0][-6:])
        temp = trading_df.copy()
        temp['交易日期'] = pd.to_datetime(temp['交易日期'])
        temp['turnover'] = temp['成交额'] / temp['流通市值']
        date = temp['交易日期'].iloc[0]
        temp.index = pd.to_datetime(temp['交易日期']).values
        result = temp.resample('1M').aggregate({'股票代码':'last',
                                            '股票名称':'last',
                                            '交易日期':'last'})
        result = result.loc['2006-05':]
        result['t-6月'] = result['交易日期'].shift(6)
        result.dropna(inplace=True)

        temp_an = ann_df.query('(Stkcd==@code)&(DeclareDate>=@date)')
        temp_an.index = temp_an['DeclareDate']


        for i in result.index:
            temp_ols = temp.loc[result.loc[i,'t-6月']:i]
            if temp['股票代码'].iloc[0][:2]=='sh':
                temp_ols = pd.concat([temp_ols,sh_df['换手率']],axis=1,join='inner')
            elif temp['股票代码'].iloc[0][:2]=='sz':
                temp_ols = pd.concat([temp_ols,sz_df['换手率']],axis=1,join='inner')
            events = ""
            if len(temp_an.loc[result.loc[i,'t-6月']:i])!=0:
                event_list = pd.unique(temp_an.loc[result.loc[i,'t-6月']:i,'DeclareDate'])
                n = 1
                for j in event_list:
                    temp_ols['event_'+str(n)] = np.where((temp_ols['交易日期']<=j+np.timedelta64(3'D'))&(temp_ols['交易日期'] >=j-np.timedelta64(3'D')),1,0)
                    events = events + "+" + 'event_'+str(n)
                    n += 1 
            formula_test = 'turnover~换手率'+events
            r1 = sm.ols(formula=formula_test, data = temp_ols).fit()
            result.loc[i,'atr'] = r1.resid.sum()
                
        atr = result[['股票代码','股票名称','交易日期','atr']]
        return atr 
    except:
        result['atr'] = np.nan
        return  result[['股票代码','股票名称','交易日期','atr']]

欢迎在后台进行交流。

分类:

人工智能

标签:

人工智能

作者介绍

碧沼
V1