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

        # read data
        pe_ttm = Stock().read_factor_h5("PE_ttm")

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

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

            current_date = date_series[i]
            print('Calculating factor %s at date %s' %
                  (self.raw_factor_name, current_date))
            data_cur = pe_ttm[current_date]
            data_cur = data_cur[data_cur != 0.0]
            ep_ttm = 1.0 / data_cur
            ep_ttm = pd.DataFrame(ep_ttm.values,
                                  columns=[current_date],
                                  index=ep_ttm.index)
            res = pd.concat([res, ep_ttm], axis=1)

        res = res.T.dropna(how='all').T
        self.save_alpha_factor_exposure(res, self.raw_factor_name)
    def cal_factor_barra_leverage_debt_to_asset(self):
        """ 资产负债比 = 总负债/总资产 """

        total_debt = Stock().read_factor_h5("TotalLiabilityDaily")
        total_asset = Stock().read_factor_h5('TotalAssetDaily')

        debt_to_asset = total_debt.div(total_asset)
        debt_to_asset = debt_to_asset.T.dropna(how='all').T

        self.save_risk_factor_exposure(debt_to_asset,
                                       self.raw_factor_name_debt_to_asset)
        debt_to_asset = FactorPreProcess().remove_extreme_value_mad(
            debt_to_asset)
        debt_to_asset = FactorPreProcess().standardization(debt_to_asset)
        self.save_risk_factor_exposure(debt_to_asset,
                                       self.factor_name_debt_to_asset)
示例#3
0
    def fund_holder_quarter_industry(self, quarter_date, quarter_last_date):
        """ 基金季度重仓持股 最近一个季度行业净流入金额 """

        # quarter_date = "20180930"
        # quarter_last_date = "20180630"
        result = self.get_fund_holder_data(quarter_date, quarter_last_date)

        # groupby industry
        data_gb_industry = pd.DataFrame(
            result.groupby(by=['Industry']).sum()[[
                'MvQuarter', 'MvQuarterLast', 'Inflow'
            ]])
        data_gb_industry.index = data_gb_industry.index.map(
            Stock().get_industry_citic1_name_ch)

        data_gb_industry['MvQuarterLast'] = data_gb_industry[
            'MvQuarterLast'].map(lambda x: np.round(x, 2))
        data_gb_industry['MvQuarter'] = data_gb_industry['MvQuarter'].map(
            lambda x: np.round(x, 2))
        data_gb_industry['Inflow'] = data_gb_industry['Inflow'].map(
            lambda x: np.round(x, 2))
        data_gb_industry.columns = [[
            '%s持股总市值' % quarter_date,
            '%s持股总市值' % quarter_last_date, '净流入金额'
        ]]
        data_gb_industry['净流入占比'] = data_gb_industry[
            '净流入金额'] / data_gb_industry['%s持股总市值' % quarter_date]
        data_gb_industry = data_gb_industry.sort_values(by=['净流入金额'],
                                                        ascending=False)

        return data_gb_industry
示例#4
0
    def get_stock_pool(self,
                       stock_pool_name="AllChinaStockFilter",
                       date="20181231"):
        """ 风险因子股票池 """

        stock_pool = Stock().get_invest_stock_pool(stock_pool_name, date)
        return stock_pool
示例#5
0
    def generate_weight_file(self, weight, date, next_date):
        """ 基金权重文件 """

        weight = weight[weight['Weight'] > self.min_tor]
        weight = weight.sort_values(by=['Weight'], ascending=False)
        weight /= weight.sum()
        print("Number of multi_factor", len(weight), date)

        weight.index.name = "Code"
        weight['Name'] = weight.index.map(
            lambda x: Stock().get_stock_name_date(x, next_date))
        weight["CreditTrading"] = "No"
        weight["Date"] = next_date
        weight["Price"] = 0.0
        weight["Direction"] = "Long"
        weight['Weight'] *= self.weight_sum
        weight.loc['Cash', 'Weight'] = 1 - self.weight_sum

        sub_path = os.path.join(self.wind_port_path, self.port_name)
        if not os.path.exists(sub_path):
            os.makedirs(sub_path)

        file = os.path.join(sub_path,
                            '%s_%s.csv' % (self.port_name, next_date))
        weight.to_csv(file)
示例#6
0
    def get_alpha_risk_exposure(self, factor_name, stock_pool_name):
        """ 得到Alpha在风险因子上的暴露 """

        hdf_risk_path = os.path.join(self.data_path, stock_pool_name,
                                     "exposure_risk\hdf")
        data = Stock().read_factor_h5(factor_name, hdf_risk_path)
        return data
示例#7
0
    def hk_inflow_period_stock(self, beg_date, end_date):
        """ 陆股通 一段时间内平均持股总市值最大、最小的5只股票 """

        concat_data = self.get_hk_inflow_data(beg_date, end_date)

        concat_data = concat_data[['EndMv', 'Inflow']]
        concat_data = concat_data[concat_data['Inflow'] != 0.0]
        concat_data = concat_data.dropna()

        concat_data['Inflow'] = concat_data['Inflow'].map(
            lambda x: np.round(x, 2))
        concat_data['EndMv'] = concat_data['EndMv'].map(
            lambda x: np.round(x, 2))
        concat_data[
            'InflowRatio'] = concat_data['Inflow'] / concat_data['EndMv']
        concat_data = concat_data.dropna()
        concat_data.index = concat_data.index.map(
            lambda x: Stock().get_stock_name_date(stock_code=x, date=end_date))

        concat_data = concat_data.sort_values(by=['Inflow'], ascending=False)
        positive = concat_data.iloc[0:10, :]

        concat_data = concat_data.sort_values(by=['Inflow'], ascending=True)
        negative = concat_data.iloc[0:10, :]

        data = pd.concat([positive, negative], axis=0)
        data.columns = ['期末持股市值(亿元)', '期间净流入(亿元)', '净流入占比']

        return data
示例#8
0
    def get_alpha_res_exposure(self, factor_name, stock_pool_name):
        """ 得到残差Alpha的暴露 """

        hdf_res_path = os.path.join(self.data_path, stock_pool_name,
                                    "res_alpha\hdf")
        data = Stock().read_factor_h5(factor_name, hdf_res_path)
        return data
示例#9
0
    def __init__(self):
        """ 初始化 """

        Data.__init__(self)
        self.sub_data_path = r'mfcteda_data\attribution'
        self.data_path = os.path.join(self.primary_data_path,
                                      self.sub_data_path)

        self.double_fee_ratio = 0.001 + 0.0008 * 2  # 印花税 + 交易佣金
        self.mg_fee_ratio = (1.2 / 100 + 0.2 / 100) / 250.0  # 管理费
        self.index_code_ratio = 0.95
        self.index_code = ""

        self.fund_code = ""
        self.fund_name = ""
        self.type = ""

        self.beg_date = ""
        self.end_date = ""
        self.beg_date_pre = ""
        self.period_name = ""

        self.close_unadjust = None
        self.adjust_factor = None
        self.data = None
        self.ipo_days = 40
        self.ipo_data = Stock().get_ipo_date()
        self.ipo_data.columns = ['IpoDate', 'DeListDate']
        self.ipo_data = pd.DataFrame(self.ipo_data['IpoDate'])
示例#10
0
    def update_data(self):
        """ 更新所需要的数据 """

        Date().load_trade_date_series("D")
        end_date = datetime.today().strftime("%Y%m%d")
        beg_date = Date().get_trade_date_offset(end_date, -12)
        Stock().load_major_holder_deal(beg_date, end_date)
示例#11
0
    def hk_inflow_period_stock(self, quarter_date, quarter_last_date):
        """ 基金季度重仓持股 最近一个季度股票净流入金额 """

        # quarter_date = "20180930"
        # quarter_last_date = "20180630"
        result = self.get_fund_holder_data(quarter_date, quarter_last_date)

        result = result.sort_values(by=['Inflow'], ascending=False)
        result['InflowRatio'] = result['Inflow'] / result['MvQuarter']
        result = result[[
            'MvQuarterLast', 'MvQuarter', 'Inflow', 'InflowRatio'
        ]]
        result = result.dropna()

        positive = result.iloc[0:10, :]
        negative = result.iloc[-10:, :]
        data = pd.concat([positive, negative], axis=0)

        data['MvQuarterLast'] = data['MvQuarterLast'].map(
            lambda x: np.round(x, 2))
        data['MvQuarter'] = data['MvQuarter'].map(lambda x: np.round(x, 2))
        data['Inflow'] = data['Inflow'].map(lambda x: np.round(x, 2))
        data.index = data.index.map(lambda x: Stock().get_stock_name_date(
            stock_code=x, date=quarter_date))

        data.columns = [
            '%s持股总市值' % quarter_last_date,
            '%s持股总市值' % quarter_date, "净流入金额", '净流入占比'
        ]

        return data
示例#12
0
    def get_data(self,
                 beg_date,
                 end_date,
                 period='M',
                 fund_pool_name="优质基金池",
                 pool_date='20180630'):
        """ 得到回测时间 回测基金池 """

        self.date_series = Date().get_trade_date_series(beg_date,
                                                        end_date,
                                                        period=period)
        self.fund_pool = Fund().get_fund_pool_code(pool_date, fund_pool_name)
        """ 得到 基金复权净值增长率 股票涨跌幅 基金市值 """

        self.stock_mv = Stock().read_factor_h5("Mkt_freeshares")
        self.fund_return = Fund().get_fund_factor("Repair_Nav_Pct")
        self.stock_return = Stock().read_factor_h5("Pct_chg").T
示例#13
0
    def cal_return_all(self, beg_date, end_date, risk_model_name, period='D'):
        """ 计算每天的因子收益率、因子暴露、股票的残差收益率 """

        self.set_model_name(risk_model_name)
        self.get_data_all(risk_model_name)

        date_series = Date().get_trade_date_series(beg_date, end_date, period)
        date_series = list(
            set(date_series) & set(self.pct_chg.columns)
            & set(self.free_mv_data.columns) & set(self.trade_status.columns)
            & set(self.industry.columns))
        date_series.sort()

        factor_return = pd.DataFrame()
        res_return = pd.DataFrame()

        # 因子暴露文件
        for i_date in range(len(date_series)):

            date = date_series[i_date]
            before_date = Date().get_trade_date_offset(date, -1)
            factor_return_date, exposure_before_date, res_return_date = self.cal_return_date(
                date)
            exposure_file = os.path.join(self.exposure_path,
                                         "exposure_%s.csv" % before_date)
            exposure_before_date.to_csv(exposure_file)
            factor_return = pd.concat([factor_return, factor_return_date],
                                      axis=1)
            res_return = pd.concat([res_return, res_return_date], axis=1)

        # 因子收益率文件
        factor_return = factor_return.T
        factor_return_file = os.path.join(self.factor_return_path,
                                          "factor_return.csv")
        if os.path.exists(factor_return_file):
            old_data = self.get_factor_return()
            factor_return = Stock().pandas_add_row(old_data, factor_return)
        factor_return.to_csv(factor_return_file)
        factor_return_cum = factor_return.cumsum()
        factor_return_file = os.path.join(self.factor_return_path,
                                          "factor_return_cum.csv")
        factor_return_cum.to_csv(factor_return_file)

        # 股票残差率文件
        res_return = res_return.T
        res_return_file = os.path.join(self.res_return_path,
                                       "stock_residual_return.csv")
        if os.path.exists(res_return_file):
            old_data = self.get_stock_residual_return()
            res_return = Stock().pandas_add_row(old_data, res_return)
        res_return.to_csv(res_return_file)
    def cal_factor_exposure(self, beg_date, end_date):

        """ 计算因子暴露 """

        # params
        long_term = 120
        short_term = int(long_term * 0.5)

        # read data
        trade_amount = Stock().read_factor_h5("TradeAmount").T / 100000000
        trade_amount = trade_amount.dropna(how='all')

        # 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()
        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))
            amount_before = trade_amount.loc[data_beg_date:current_date, :]
            amount_before = amount_before.fillna(0.0)

            if len(amount_before) == long_term:

                print('Calculating factor %s at date %s' % (self.raw_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 < short_term]).index

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

                weight = np.array(list(range(1, long_term + 1)))
                weight_amount = np.dot(amount_log.T.values, weight)
                weight_amount = pd.DataFrame(weight_amount, index=amount_log.columns, columns=[current_date])

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

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

        res = res.T.dropna(how='all').T
        self.save_alpha_factor_exposure(res, self.raw_factor_name)
示例#15
0
def ARAPIncomeTTM(beg_date, end_date):
    """
    因子说明:(预收账款 + 应付账款) / 营业总收入 TTM
    最近一期财报 实时更新
    若有一个为负值 结果为负值
    """

    # param
    #################################################################################
    factor_name = 'ARAPIncomeTTM'
    ipo_num = 90

    # read data
    #################################################################################
    income = Stock().get_factor_h5("OperatingIncome", None, "primary_mfc")
    advance = Stock().get_factor_h5("AdvanceReceipts", None, "primary_mfc")
    payable = Stock().get_factor_h5("AccountsPayable", None, "primary_mfc")

    # data precessing
    #################################################################################
    [advance, payable,
     income] = Stock().make_same_index_columns([advance, payable, income])

    add = advance.add(payable)
    ratio = add.div(income)
    ratio = StockFactorOperate().change_quarter_to_daily_with_report_date(
        ratio, beg_date, end_date)

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

    # save data
    #############################################################################
    Stock().write_factor_h5(res, factor_name, "alpha_dfc")
    return res
示例#16
0
def RetainMarketValue(beg_date, end_date):
    """
    因子说明: 留存收益 / 总市值
    披露日期 为 最近财报
    """

    # param
    #################################################################################
    factor_name = 'RetainMarketValue'
    ipo_num = 90

    # read data
    #################################################################################
    retain = Stock().get_factor_h5("RetainedEarnings", None, "primary_mfc")
    mv = Stock().get_factor_h5("TotalMarketValue", None, "alpha_dfc")
    retain = StockFactorOperate().change_quarter_to_daily_with_report_date(
        retain, beg_date, end_date)
    [retain, mv] = Stock().make_same_index_columns([retain, mv])

    retain_ratio = retain.div(mv)

    # data precessing
    #################################################################################
    pass

    # calculate data daily
    #################################################################################

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

    # save data
    #############################################################################
    Stock().write_factor_h5(res, factor_name, "alpha_dfc")
    return res
示例#17
0
def CashMarketValue(beg_date, end_date):
    """
    因子说明: 货币资金/总市值, 根据最新财报更新数据
    披露日期 为 最近财报
    """

    # param
    #################################################################################
    factor_name = 'CashMarketValue'
    ipo_num = 90

    # read data
    #################################################################################
    cash = Stock().get_factor_h5("CashEquivalents", None, "primary_mfc")
    total_mv = Stock().get_factor_h5("TotalMarketValue", None, "alpha_dfc")

    cash = StockFactorOperate().change_quarter_to_daily_with_report_date(
        cash, beg_date, end_date)

    [cash, total_mv] = Stock().make_same_index_columns([cash, total_mv])
    cash_price = cash.div(total_mv)

    # data precessing
    #################################################################################
    pass

    # calculate data daily
    #################################################################################

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

    # save data
    #############################################################################
    Stock().write_factor_h5(res, factor_name, "alpha_dfc")
    return res
    def cal_predicted_earnings_to_price_ratio(self):
        """ 预期盈利 / 总市值 """

        e1_predicted = Stock().read_factor_h5("FE_1")
        price_unadjust = Stock().read_factor_h5("Price_Unadjust")

        e1_predicted, price_unadjust = FactorPreProcess(
        ).make_same_index_columns([e1_predicted, price_unadjust])
        ep1_predicted = e1_predicted.div(price_unadjust)
        ep1_predicted = ep1_predicted.T.dropna(how='all').T

        self.save_risk_factor_exposure(ep1_predicted,
                                       self.raw_factor_name_predicted)
        ep1_predicted = FactorPreProcess().remove_extreme_value_mad(
            ep1_predicted)
        ep1_predicted = FactorPreProcess().standardization(ep1_predicted)
        self.save_risk_factor_exposure(ep1_predicted,
                                       self.factor_name_predicted)
示例#19
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
    def get_data(self, beg_date, end_date,
                 period='M'):

        """ 得到回测时间 回测基金池 """

        self.date_series = Date().get_trade_date_series(beg_date, end_date, period=period)

        """ 得到 基金复权净值增长率 股票涨跌幅 基金市值 """

        self.stock_mv = Stock().read_factor_h5("Mkt_freeshares")
        self.stock_return = Stock().read_factor_h5("Pct_chg").T

        index1 = Index().get_index_factor("885000.WI", attr=['PCT'])
        index2 = Index().get_index_factor("885001.WI", attr=['PCT'])
        index = pd.concat([index1, index2], axis=1)
        index['PctMean'] = index.mean(axis=1)
        self.index_return = pd.DataFrame(index['PctMean']) * 100
        self.index_return.columns = ['IndexReturn']
示例#21
0
def CFNOTTMIncomeTTMYOY(beg_date, end_date):
    """
    因子说明: 经营活动现金净流量(TTM)/营业收入(TTM)  同比增速
    披露日期 为 最近财报
    """

    # param
    #################################################################################
    factor_name = 'CFNOTTMIncomeTTMYOY'
    ipo_num = 90

    # read data
    #################################################################################
    operate_cash = Stock().get_factor_h5("NetOperateCashFlow", None,
                                         "primary_mfc")
    income = Stock().get_factor_h5("OperatingIncome", None, "primary_mfc")

    operate_cash_ttm = StockFactorOperate(
    ).change_single_quarter_to_ttm_quarter(operate_cash)
    income_ttm = StockFactorOperate().change_single_quarter_to_ttm_quarter(
        income)

    [operate_cash_ttm, income_ttm
     ] = Stock().make_same_index_columns([operate_cash_ttm, income_ttm])
    ratio = operate_cash_ttm.div(income_ttm).T
    ratio_4 = ratio.shift(4)
    ratio_yoy = ratio / ratio_4 - 1.0
    ratio_yoy = ratio_yoy.T
    ratio_yoy = StockFactorOperate().change_quarter_to_daily_with_report_date(
        ratio_yoy, beg_date, end_date)

    # data precessing
    #################################################################################
    pass

    # calculate data daily
    #################################################################################

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

    # save data
    #############################################################################
    Stock().write_factor_h5(res, factor_name, "alpha_dfc")
    return res
示例#22
0
def CFNOTTMIncomeTTMDaily(beg_date, end_date):
    """
    因子说明: 经营活动现金净流量(TTM)/营业收入(TTM)
    披露日期 为 最近财报
    """

    # param
    #################################################################################
    factor_name = 'CFNOTTMIncomeTTMDaily'
    ipo_num = 90

    # read data
    #################################################################################
    operate_cash = Stock().get_factor_h5("NetOperateCashFlow", None,
                                         "primary_mfc")
    income = Stock().get_factor_h5("OperatingIncome", None, "primary_mfc")

    operate_cash_ttm = StockFactorOperate(
    ).change_single_quarter_to_ttm_quarter(operate_cash)
    income_ttm = StockFactorOperate().change_single_quarter_to_ttm_quarter(
        income)

    [operate_cash_ttm, income_ttm
     ] = Stock().make_same_index_columns([operate_cash_ttm, income_ttm])
    ratio = operate_cash_ttm.div(income_ttm)

    report_data = Stock().get_factor_h5("OperatingIncome" + "Daily",
                                        "ReportDate", 'primary_mfc')
    ratio = StockFactorOperate().change_quarter_to_daily_with_disclosure_date(
        ratio, report_data, beg_date, end_date)

    # data precessing
    #################################################################################
    pass

    # calculate data daily
    #################################################################################

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

    # save data
    #############################################################################
    Stock().write_factor_h5(res, factor_name, "alpha_dfc")
    return res
    def cal_factor_barra_std(self, beg_date, end_date):
        """ 利用过去120天的收益率计算波动率(未来可以考虑不够120天的情况) """

        # param
        term = 252
        half_life = 42
        min_periods = 120

        # read data
        pct_chg = Stock().read_factor_h5("Pct_chg")
        pct_chg = Stock().replace_suspension_with_nan(pct_chg)
        pct_chg = Stock().fillna_with_mad_market(pct_chg)
        pct_chg = pct_chg.T

        date_series = Date().get_trade_date_series(beg_date, end_date)
        date_series = list(set(pct_chg.index) & set(date_series))
        date_series.sort()

        res = pd.DataFrame([])

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

            current_date = date_series[i]
            print('Calculating Barra Risk factor %s at date %s' %
                  (self.factor_name_std, current_date))
            data_beg_date = Date().get_trade_date_offset(
                current_date, -(term - 1))
            data_pre = pct_chg.ix[data_beg_date:current_date, :]
            data_pre = data_pre.dropna(how='all')

            if len(data_pre) > 0:
                data_std = data_pre.ewm(
                    halflife=half_life,
                    min_periods=min_periods).std().loc[current_date, :]
                data_date = pd.DataFrame(data_std) * np.sqrt(250)
                data_date.columns = [current_date]
                res = pd.concat([res, data_date], axis=1)

        res = res.T.dropna(how='all').T
        self.save_risk_factor_exposure(res, self.raw_factor_name_std)
        res = Stock().remove_extreme_value_mad(res)
        res = Stock().standardization(res)
        self.save_risk_factor_exposure(res, self.factor_name_std)
示例#24
0
def cal_lasso_stock_pool_all():

    lasso_period = 60
    lasso_period_min = 40
    beg_date = "20041231"
    end_date = datetime.today()
    out_path = "E:\\3_数据\\4_fund_data\\4_fund_holding_predict\\lasso_stock_pool\\"

    quarter_date = Date().get_last_fund_quarter_date(end_date)
    quarter_date = '20180630'
    date_series = Date().get_trade_date_series(beg_date, end_date, period="M")
    fund_pool = Fund().get_fund_pool_code(quarter_date, "优质基金池")

    s_marketvalue = Stock().get_h5_primary_factor("Mkt_freeshares",
                                                  date_list=None)
    f_pct = Fund().get_fund_factor("Repair_Nav_Pct",
                                   date_list=None,
                                   fund_pool=fund_pool)
    s_pct = Stock().get_h5_primary_factor("Pct_chg",
                                          date_list=None,
                                          stock_pool=None).T

    for i_fund in range(0, len(fund_pool)):

        fund_code = fund_pool[i_fund]

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

            period_end_date = date_series[i_date]
            period_beg_date = Date().get_trade_date_offset(
                period_end_date, -lasso_period)
            res_add = cal_lasso_stock_pool(s_marketvalue, f_pct, s_pct,
                                           period_beg_date, period_end_date,
                                           fund_code, out_path,
                                           lasso_period_min)

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

        res.to_csv(out_path + 'LASSO回归股票池_' + fund_code + "_AllDate.csv")
    def cal_factor_exposure(self, beg_date, end_date):
        """ 计算因子暴露 """

        dividend_12m = Stock().read_factor_h5("dividendyield2") / 100
        beg_date = Date().change_to_str(beg_date)
        end_date = Date().change_to_str(end_date)
        dividend_12m = dividend_12m.loc[:, beg_date:end_date]

        res = dividend_12m.T.dropna(how='all').T
        self.save_alpha_factor_exposure(res, self.raw_factor_name)
示例#26
0
    def cal_factor_barra_growth_short_term_predicted_earnings_growth(self):

        """ 未来1年的预期盈利增长 """

        predicted_earnings_growth = Stock().read_factor_h5("FEGR_1")

        self.save_risk_factor_exposure(predicted_earnings_growth, self.raw_factor_name_short_term)
        predicted_earnings_growth = FactorPreProcess().remove_extreme_value_mad(predicted_earnings_growth)
        predicted_earnings_growth = FactorPreProcess().standardization(predicted_earnings_growth)
        self.save_risk_factor_exposure(predicted_earnings_growth, self.factor_name_short_term)
示例#27
0
def GrossProfitRateBiasDaily(beg_date, end_date):
    """
    因子说明:当季 毛利率TTM - 上季度 毛利率TTM
    毛利率TTM = (营业收入TTM - 营业成本TTM)/ 营业收入TTM
    披露日期 为 最近财报
    """

    # param
    #################################################################################
    factor_name = 'GrossProfitRateBiasDaily'
    ipo_num = 90

    # read data
    #################################################################################
    income = Stock().get_factor_h5("OperatingIncome", None, "primary_mfc")
    cost = Stock().get_factor_h5("OperatingCost", None, "primary_mfc")
    income_ttm = StockFactorOperate().change_single_quarter_to_ttm_quarter(
        income).T
    cost_ttm = StockFactorOperate().change_single_quarter_to_ttm_quarter(
        cost).T

    gross_margin = (income_ttm - cost_ttm) / cost_ttm

    gross_margin_qoq = gross_margin.diff().T

    report_data = Stock().get_factor_h5("OperatingCost" + "Daily",
                                        "ReportDate", 'primary_mfc')
    gross_margin_qoq = StockFactorOperate().\
        change_quarter_to_daily_with_disclosure_date(gross_margin_qoq, report_data, beg_date, end_date)

    # data precessing
    #################################################################################
    pass

    # calculate data daily
    #################################################################################

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

    # save data
    #############################################################################
    Stock().write_factor_h5(res, factor_name, "alpha_dfc")
    return res
示例#28
0
def ROATTMDaily(beg_date, end_date):
    """
    因子说明: ROA
    披露日期 为 最近财报
    """

    # param
    #################################################################################
    factor_name = 'ROATTMDaily'
    ipo_num = 90

    # read data
    #################################################################################
    net_profit = Stock().get_factor_h5("NetProfit", None, "primary_mfc")
    asset = Stock().get_factor_h5("TotalAsset", None, "primary_mfc")
    net_profit_ttm = StockFactorOperate().change_single_quarter_to_ttm_quarter(
        net_profit)
    asset = StockFactorOperate().change_single_quarter_to_ttm_quarter(
        asset) / 4.0

    [net_profit_ttm,
     asset] = Stock().make_same_index_columns([net_profit_ttm, asset])
    roa = net_profit_ttm.div(asset)

    report_data = Stock().get_factor_h5("NetProfit" + "Daily", "ReportDate",
                                        'primary_mfc')
    roa = StockFactorOperate().change_quarter_to_daily_with_disclosure_date(
        roa, report_data, beg_date, end_date)

    # data precessing
    #################################################################################
    pass

    # calculate data daily
    #################################################################################

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

    # save data
    #############################################################################
    Stock().write_factor_h5(res, factor_name, "alpha_dfc")
    return res
示例#29
0
    def get_info(self,
                 port_name,
                 weight_name,
                 benchmark_code,
                 stock_pool_name,
                 beg_date,
                 end_date,
                 period,
                 industry_deviate,
                 style_deviate,
                 double_turnover,
                 alpha_name,
                 alpha_type="my_alpha",
                 weight_sum=0.95,
                 weight_type='fixed',
                 min_tor=0.001):
        """ 回测基础信息 得到全部数据 """

        self.port_name = port_name
        self.weight_name = weight_name
        self.benchmark_code = benchmark_code
        self.stock_pool_name = stock_pool_name
        self.industry_deviate = industry_deviate
        self.style_deviate = style_deviate
        self.double_turnover = double_turnover
        self.weight_sum = weight_sum
        self.weight_type = weight_type
        self.min_tor = min_tor

        self.style_columns = ['Size']
        self.trading_status = Stock().read_factor_h5("TradingStatus")
        self.free_mv = Stock().read_factor_h5("Mkt_freeshares")
        self.get_alpha_factor(alpha_name, alpha_type)

        date_series = Date().get_trade_date_series(beg_date, end_date, period)
        date_series = list(
            map(lambda x: Date().get_trade_date_offset(x, 24), date_series))
        date_series = list(
            set(self.trading_status.columns) & set(date_series)
            & set(self.free_mv.columns))
        date_series.sort()
        self.date_series = date_series
    def cal_trailing_earnings_to_price_ratio(self):
        """ 归母净利润TTM / 总市值 """

        pe_ttm = Stock().read_factor_h5("PE_ttm")
        ep_ttm = 1.0 / pe_ttm

        ep_ttm = ep_ttm.T.dropna(how='all').T
        self.save_risk_factor_exposure(ep_ttm, self.raw_factor_name_trailing)
        ep_ttm = FactorPreProcess().remove_extreme_value_mad(ep_ttm)
        ep_ttm = FactorPreProcess().standardization(ep_ttm)
        self.save_risk_factor_exposure(ep_ttm, self.factor_name_trailing)