def cal_factor_exposure(self, beg_date, end_date):
        """ 计算因子暴露 """

        # param
        long_term = 20
        effective_term = int(long_term / 2)
        extreme_value = 80

        # read data
        pct = Stock().read_factor_h5("Pct_chg").T
        trade_amount = Stock().read_factor_h5("TradeAmount").T / 100000000

        # data precessing
        [pct,
         trade_amount] = Stock().make_same_index_columns([pct, trade_amount])
        trade_amount = trade_amount.fillna(0.0)

        # calculate data daily
        date_series = Date().get_trade_date_series(beg_date, end_date)
        date_series = list(set(date_series) & set(pct.index))
        date_series.sort()
        res = pd.DataFrame()

        for i in range(0, len(date_series)):

            current_date = date_series[i]
            data_beg_date = Date().get_trade_date_offset(
                current_date, -(long_term - 1))
            trade_amount_before = trade_amount.loc[
                data_beg_date:current_date, :]

            if len(trade_amount_before) > effective_term:
                print('Calculating factor %s at date %s' %
                      (self.raw_factor_name, current_date))
                zero_number = trade_amount_before.applymap(
                    lambda x: 1.0 if x == 0.0 else 0.0).sum()
                code_filter_list = (
                    zero_number[zero_number < effective_term]).index
                amount_before = trade_amount.loc[data_beg_date:current_date,
                                                 code_filter_list]
                pct_before = pct.loc[data_beg_date:current_date,
                                     code_filter_list]
                iq = pct_before.abs().div(amount_before)
                iq[iq > extreme_value] = np.nan
                bias = iq.mean()
                bias = pd.DataFrame(bias)
                bias.columns = [current_date]
            else:
                print('Calculating factor %s at date %s is null' %
                      (self.raw_factor_name, current_date))
                bias = pd.DataFrame([],
                                    columns=[current_date],
                                    index=trade_amount_before.columns)

            res = pd.concat([res, bias], axis=1)

        res = res.T.dropna(how='all').T
        self.save_alpha_factor_exposure(res, self.raw_factor_name)
示例#2
0
    def get_balance_data(self, beg_date, end_date):

        """ 一段时间内的数据 """

        beg_date = Date().get_trade_date_offset(beg_date, 0)
        end_date = Date().get_trade_date_offset(end_date, 0)
        print(beg_date, end_date)
        data = Stock().read_factor_h5("Margin_TradeBalance")
        data = data.fillna(0.0)
        data /= 100000000.0

        concat_data = pd.concat([data[beg_date], data[end_date]], axis=1)
        concat_data.columns = ['BegMv', 'EndMv']

        concat_data['DiffMv'] = concat_data['EndMv'] - concat_data['BegMv']
        concat_data = concat_data.dropna()

        return concat_data
示例#3
0
    def cal_factor_exposure(self, beg_date, end_date):
        """ 计算因子暴露 """

        # read data
        research = Stock().read_factor_h5("ResearchDevelopmentExpense")
        research = research.fillna(0.0)
        income = Stock().read_factor_h5("OperatingIncome")

        research_ttm = Stock().change_single_quarter_to_ttm_quarter(research)
        income_ttm = Stock().change_single_quarter_to_ttm_quarter(income) / 4.0

        [research_ttm, income_ttm
         ] = Stock().make_same_index_columns([research_ttm, income_ttm])
        roa = research_ttm.div(income_ttm)

        report_data = Stock().read_factor_h5("ReportDateDaily")
        roa = Stock().change_quarter_to_daily_with_disclosure_date(
            roa, report_data, beg_date, end_date)

        res = roa.T.dropna(how='all').T
        self.save_alpha_factor_exposure(res, self.raw_factor_name)
示例#4
0
def VolumeIR(beg_date, end_date):
    """
    因子说明:-1* 过去40天成交额标准差 / 过去40天成交额均值
    """
    # param
    #################################################################################
    LongTerm = 40
    HalfTerm = int(LongTerm / 2)
    factor_name = "VolumeIR"
    ipo_num = 90

    # read data
    #################################################################################
    trade_amount = Stock().get_factor_h5("TradeAmount", None, "primary_mfc").T

    # code set & date set
    #################################################################################
    trade_amount = trade_amount.fillna(0.0)

    # calculate data daily
    #################################################################################
    date_series = Date().get_trade_date_series(beg_date, end_date)
    date_series = list(set(trade_amount.index) & set(date_series))
    date_series.sort()

    for i in range(0, len(date_series)):

        current_date = date_series[i]
        data_beg_date = Date().get_trade_date_offset(current_date,
                                                     -(LongTerm - 1))
        amount_before = trade_amount.ix[data_beg_date:current_date, :]

        if len(amount_before) >= int(0.8 * LongTerm):

            print('Calculating factor %s at date %s' %
                  (factor_name, current_date))
            zero_number = amount_before.applymap(lambda x: 1.0
                                                 if x == 0.0 else 0.0).sum()
            code_filter_list = (zero_number[zero_number < HalfTerm]).index

            trade_amount_pre = trade_amount.ix[data_beg_date:current_date,
                                               code_filter_list]
            trade_amount_pre_std = trade_amount_pre.std()
            trade_amount_pre_mean = trade_amount_pre.mean()
            trade_amount_pre_cv = -trade_amount_pre_std / trade_amount_pre_mean
            trade_amount_pre_cv = pd.DataFrame(trade_amount_pre_cv.values,
                                               columns=[current_date],
                                               index=trade_amount_pre.columns)
        else:
            print('Calculating factor %s at date %s is null' %
                  (factor_name, current_date))
            trade_amount_pre_cv = pd.DataFrame([],
                                               columns=[current_date],
                                               index=trade_amount.columns)

        if i == 0:
            res = trade_amount_pre_cv
        else:
            res_add = trade_amount_pre_cv
            res = pd.concat([res, res_add], axis=1)

    res = res.T.dropna(how='all').T
    # save data
    #############################################################################
    Stock().write_factor_h5(res, factor_name, "alpha_dfc")
    return res
示例#5
0
def VolumeLnMean120d(beg_date, end_date):
    """
    因子说明:过去120天的-1*log(交易额)的加权平均 权为随时间线性递减
    """

    # param
    #################################################################################
    LongTerm = 120
    HalfTerm = int(LongTerm / 2)
    factor_name = 'VolumeLnMean120d'
    ipo_num = 90

    # read data
    #################################################################################
    trade_amount = Stock().get_factor_h5("TradeAmount", None, "primary_mfc").T

    # code set & date set
    #################################################################################
    trade_amount = trade_amount.fillna(0.0)

    # calculate data daily
    #################################################################################
    date_series = Date().get_trade_date_series(beg_date, end_date)
    date_series = list(set(trade_amount.index) & set(date_series))
    date_series.sort()

    for i in range(0, len(date_series)):

        current_date = date_series[i]
        data_beg_date = Date().get_trade_date_offset(current_date,
                                                     -(LongTerm - 1))
        amount_before = trade_amount.ix[data_beg_date:current_date, :]

        if len(amount_before) == LongTerm:

            print('Calculating factor %s at date %s' %
                  (factor_name, current_date))
            zero_number = amount_before.applymap(lambda x: 1.0
                                                 if x == 0.0 else 0.0).sum()
            code_filter_list = (zero_number[zero_number < HalfTerm]).index

            amount_before = trade_amount.ix[data_beg_date:current_date,
                                            code_filter_list]
            amount_before_log = amount_before.applymap(
                lambda x: np.nan if x == 0 else -np.log(x))

            weight = np.array(list(range(1, LongTerm + 1)))
            weight_amount_log_val = np.dot(amount_before_log.T.values, weight)
            weight_amount_log = pd.DataFrame(weight_amount_log_val,
                                             index=amount_before_log.columns,
                                             columns=[current_date])

        else:
            print('Calculating factor %s at date %s is null' %
                  (factor_name, current_date))
            weight_amount_log = pd.DataFrame([],
                                             columns=[current_date],
                                             index=trade_amount.columns)

        if i == 0:
            res = weight_amount_log
        else:
            res_add = weight_amount_log
            res = pd.concat([res, res_add], axis=1)

    res = res.T.dropna(how='all').T
    # save data
    #############################################################################
    Stock().write_factor_h5(res, factor_name, "alpha_dfc")
    return res
示例#6
0
def IlliquidityBias(beg_date, end_date):
    """
    因子说明: 涨跌幅的绝对值 / 交易额
    最近10天均值 / 最近40天均值
    """

    # param
    #################################################################################
    LongTerm = 40
    ShortTerm = 10
    HalfTerm = LongTerm / 2
    factor_name = "IlliquidityBias"
    ipo_num = 90

    # read data
    #################################################################################
    pct = Stock().get_factor_h5("Pct_chg", None, "primary_mfc").T
    trade_amount = Stock().get_factor_h5("TradeAmount", None, "primary_mfc").T

    # data precessing
    #################################################################################
    [pct, trade_amount] = Stock().make_same_index_columns([pct, trade_amount])
    trade_amount = trade_amount.fillna(0.0)

    # calculate data daily
    #################################################################################
    date_series = Date().get_trade_date_series(beg_date, end_date)
    date_series = list(set(date_series) & set(pct.index))
    date_series.sort()

    for i in range(0, len(date_series)):

        current_date = date_series[i]
        data_beg_date = Date().get_trade_date_offset(current_date,
                                                     -(LongTerm - 1))
        trade_amount_before = trade_amount.ix[data_beg_date:current_date, :]

        if len(trade_amount_before) > HalfTerm:
            print('Calculating factor %s at date %s' %
                  (factor_name, current_date))
            zero_number = trade_amount_before.applymap(
                lambda x: 1.0 if x == 0.0 else 0.0).sum()
            code_filter_list = (zero_number[zero_number < ShortTerm]).index
            amount_before = trade_amount.ix[data_beg_date:current_date,
                                            code_filter_list]
            pct_before = pct.ix[data_beg_date:current_date, code_filter_list]
            illiq = pct_before.abs().div(amount_before,
                                         axis='index') * 100000000
            illiq[illiq > 100.0] = np.nan
            illiq_bias = illiq.ix[-1 - ShortTerm:, :].mean() / illiq.mean()
            price_mean = pd.DataFrame(illiq_bias.values,
                                      columns=[current_date],
                                      index=illiq_bias.index)
        else:
            print('Calculating factor %s at date %s is null' %
                  (factor_name, current_date))
            price_mean = pd.DataFrame([],
                                      columns=[current_date],
                                      index=trade_amount_before.columns)

        if i == 0:
            res = price_mean
        else:
            res = pd.concat([res, price_mean], axis=1)

    res = res.T.dropna(how='all').T

    # save data
    #############################################################################
    Stock().write_factor_h5(res, factor_name, "alpha_dfc")
    return res
示例#7
0
def VolumeUpRatio(beg_date, end_date):

    """
    因子说明:1*以当日收盘价为下限 当日收盘价*1.1为上限,
    过去120天的在上下限之间的天的成交额的总和占过去120天总成交额的比例
    最后乘以 -1
    注意:补齐nan为0,,去掉过去120天超过60天交易额为0的股票
    """

    # param
    #################################################################################
    LongTerm = 120
    HalfTerm = int(LongTerm / 2)
    PctRange = 0.1
    factor_name = "VolumeUpRatio"
    ipo_num = 90

    # read data
    #################################################################################
    close = Stock().get_factor_h5("PriceCloseAdjust", None, "alpha_dfc").T
    trade_amount = Stock().get_factor_h5("TradeAmount", None, "primary_mfc").T

    # data precessing
    #################################################################################
    [close, trade_amount] = Stock().make_same_index_columns([close, trade_amount])
    trade_amount = trade_amount.fillna(0.0)

    # calculate data daily
    #################################################################################
    date_series = Date().get_trade_date_series(beg_date, end_date)
    date_series = list(set(trade_amount.index) & set(date_series))
    date_series.sort()

    for i in range(0, len(date_series)):

        current_date = date_series[i]
        data_beg_date = Date().get_trade_date_offset(current_date, -(LongTerm-1))
        amount_before = trade_amount.ix[data_beg_date:current_date, :]

        if len(amount_before) >= int(0.8*LongTerm):

            print('Calculating factor %s at date %s' % (factor_name, current_date))
            zero_number = amount_before.applymap(lambda x: 1.0 if x == 0.0 else 0.0).sum()
            code_filter_list = (zero_number[zero_number < HalfTerm]).index

            close_low_limit = close.ix[current_date, code_filter_list]
            close_up_limit = close.ix[current_date, code_filter_list] * (1 + PctRange)

            close_before = close.ix[data_beg_date:current_date, code_filter_list]
            price_center = (close_before > close_low_limit) & (close_before < close_up_limit)
            trade_amount_filter_sum = amount_before[price_center].sum()
            trade_amount_sum = amount_before.sum()

            trade_amount_sum = pd.concat([trade_amount_filter_sum, trade_amount_sum], axis=1)
            trade_amount_sum.columns = ['filter_sum', 'sum']
            trade_amount_sum = trade_amount_sum[trade_amount_sum['sum'] != 0.0]
            trade_amount_sum['ratio'] = - trade_amount_sum['filter_sum'] / trade_amount_sum['sum']
        else:
            print('Calculating factor %s at date %s is null' % (factor_name, current_date))
            trade_amount_sum = pd.DataFrame([], columns=['ratio'], index=amount_before.columns)

        if i == 0:
            res = pd.DataFrame(trade_amount_sum['ratio'].values, columns=[current_date], index=trade_amount_sum.index)
        else:
            res_add = pd.DataFrame(trade_amount_sum['ratio'].values, columns=[current_date], index=trade_amount_sum.index)
            res = pd.concat([res, res_add], axis=1)

    res = res.T.dropna(how='all').T

    # save data
    #############################################################################
    Stock().write_factor_h5(res, factor_name, "alpha_dfc")
    return res