def cal_factor_mrar_all(self, T, r, beg_date, end_date):

        date_series = Date().get_normal_date_series(beg_date, end_date, "Q")
        result = pd.DataFrame([], index=date_series)

        def fun_date(x):
            year = int(x[0:4])
            month = int(x[4:6])
            day = calendar.monthrange(year, month)[1]
            date = datetime(year, month, day).strftime("%Y%m%d")
            return date

        macro_code = "S0059744"
        macro_name = "中债国债到期收益率-1年"
        macro_data = Macro().get_macro_data(macro_code, None, None)
        macro_data.columns = [macro_name]
        macro_data['YearMonth'] = macro_data.index.map(lambda x: x[0:6])
        macro_data = macro_data.groupby(by=['YearMonth']).mean()[macro_name]
        macro_data.index = macro_data.index.map(fun_date)
        macro_data = pd.DataFrame(macro_data)
        macro_data.columns = [macro_name]
        macro_data /= 12.0

        fund_data = Fund().get_fund_factor("Repair_Nav_Pct", None, None)

        for i in range(len(date_series)):

            # 日期
            ######################################################################################################
            report_date = date_series[i]

            # 基金池信息
            ######################################################################################################
            fund_code_list = FundPool().get_fund_pool_code(date=report_date, name="基金持仓基准基金池")
            fund_code_list3 = FundPool().get_fund_pool_code(date=report_date, name="量化基金")
            fund_code_list2 = FundPool().get_fund_pool_code(date="20180630", name="东方红基金")
            fund_code_list.extend(fund_code_list2)
            fund_code_list.extend(fund_code_list3)
            fund_code_list = list(set(fund_code_list))
            fund_code_list.sort()

            for i_fund in range(len(fund_code_list)):
                fund = fund_code_list[i_fund]
                print(report_date, fund)
                try:
                    res = self.cal_factor_mrar(fund, T, r, end_date, fund_data, macro_data)
                    result.loc[report_date, fund] = res
                except Exception as e:
                    result.loc[report_date, fund] = np.nan

        result = result.T
        file = os.path.join(self.path, "MorningStar_MRAR_" + str(r) + "_" + str(T) + '.csv')
        result.to_csv(file)
Пример #2
0
    def cal_fund_holder_return_quarter_backtest_all(self,
                                                    T,
                                                    beg_date,
                                                    end_date,
                                                    col="AlphaReturn",
                                                    type="Mean"):

        date_series = Date().get_normal_date_series(beg_date, end_date, "Q")
        result = pd.DataFrame([], index=date_series)

        for i in range(len(date_series)):

            # 日期
            ######################################################################################################
            report_date = date_series[i]

            # 基金池信息
            ######################################################################################################
            fund_code_list = FundPool().get_fund_pool_code(date=report_date,
                                                           name="基金持仓基准基金池")
            fund_code_list3 = FundPool().get_fund_pool_code(date=report_date,
                                                            name="量化基金")
            fund_code_list2 = FundPool().get_fund_pool_code(date="20180630",
                                                            name="东方红基金")
            fund_code_list.extend(fund_code_list2)
            fund_code_list.extend(fund_code_list3)
            fund_code_list = list(set(fund_code_list))
            fund_code_list.sort()

            for i_fund in range(len(fund_code_list)):
                fund = fund_code_list[i_fund]
                print(report_date, fund)
                try:
                    res = self.cal_fund_holder_return_quarter_backtest(
                        fund, T, report_date, col, type)
                    result.loc[report_date, fund] = res
                except Exception as e:
                    result.loc[report_date, fund] = np.nan

        result = result.T
        file = os.path.join(
            self.path,
            "FundHolderQuarter_" + col + type + "_" + str(T) + '.csv')
        result.to_csv(file)
Пример #3
0
    def cal_fund_regression_return_index_backtest_all(self,
                                                      T,
                                                      beg_date,
                                                      end_date,
                                                      col="AlphaReturn",
                                                      type="Mean"):

        date_series = Date().get_trade_date_series(beg_date, end_date, "M")
        result = pd.DataFrame([], index=date_series)

        # 基金池信息
        ######################################################################################################
        fund_code_list = FundPool().get_fund_pool_code(date="20180630",
                                                       name="基金持仓基准基金池")
        # fund_code_list3 = FundPool().get_fund_pool_code(date="20180630", name="量化基金")
        fund_code_list2 = FundPool().get_fund_pool_code(date="20180630",
                                                        name="东方红基金")
        # fund_code_list4 = FundPool().get_fund_pool_code(date="20180630", name="指数型基金")
        fund_code_list.extend(fund_code_list2)
        # fund_code_list.extend(fund_code_list3)
        # fund_code_list.extend(fund_code_list4)
        fund_code_list = list(set(fund_code_list))
        fund_code_list.sort()

        for i_fund in range(len(fund_code_list)):
            fund = fund_code_list[i_fund]
            print(fund)
            if i_fund == 0:
                result = self.cal_fund_regression_return_index_backtest(
                    fund, T, date_series, col, type)
            else:
                result_add = self.cal_fund_regression_return_index_backtest(
                    fund, T, date_series, col, type)
                result = pd.concat([result, result_add], axis=1)

        result = result.T
        file = os.path.join(
            self.path,
            "FundRegressionIndex_" + col + type + "_" + str(T) + '.csv')
        result.to_csv(file)