Beispiel #1
0
    def save_regression_analysis_result(self, factor_name):
        with MongoConnect(DatabaseName.MULTI_FACTOR_DATA.value):
            factor_return = self.factor_return.copy()
            factor_t_value = self.factor_t_value.copy()
            net_analysis_result = self.net_analysis_result
            factor_return.index = factor_return.index.format()
            factor_t_value.index = factor_t_value.index.format()
            net_analysis_result['cumsum'][
                'net_value_df'].index = net_analysis_result['cumsum'][
                    'net_value_df'].index.format()
            net_analysis_result['cumprod'][
                'net_value_df'].index = net_analysis_result['cumprod'][
                    'net_value_df'].index.format()
            net_analysis_result['cumsum'][
                'benchmark_df'].index = net_analysis_result['cumsum'][
                    'benchmark_df'].index.format()
            net_analysis_result['cumprod'][
                'benchmark_df'].index = net_analysis_result['cumprod'][
                    'benchmark_df'].index.format()

            doc = FactorRegressionAnalysisResult(
                factor_name=factor_name,
                # 因子数据开始时间
                begin_date=self.factor.index[0],
                # 因子数据结束时间
                end_date=self.factor.index[-1],
                # 因子收益率的自相关系数acf和偏自相关系数pacf,默认1-10阶,结果list len=11,取1-10个数
                acf_result=self.acf_result,
                # 因子收益率,单利,复利, 日收益率
                factor_return=factor_return,
                # 单因子检测的T值, Series, index为时间
                factor_t_value=factor_t_value,
                # 单因子检测的T值的统计值,'t_value_mean': 绝对值均值, 't_value_greater_two':绝对值序列大于2的占比
                factor_t_value_statistics=self.factor_t_value_statistics,
                # 净值分析结果
                net_analysis_result=self.net_analysis_result)
            doc.save()
    path = LocalDataPath.path + LocalDataFolderName.FACTOR.value + '/'
    factor_data = {}
    for factor_name in factor_list:
        factor_single_data = get_local_data(path, factor_name + '.h5')
        # 指数数据不全,需要删一部分因子数据
        factor_data[factor_name] = factor_single_data[
            factor_single_data.index < datetime(2020, 1, 1)]

    with MongoConnect(DatabaseName.MULTI_FACTOR_DATA.value):
        factor_ic = {}
        factor_return = {}
        for factor_name in factor_list:
            factor_regression_analysis_result = FactorRegressionAnalysisResult.objects(factor_name=factor_name) \
                .only('factor_name') \
                .only('begin_date') \
                .only('end_date') \
                .only('factor_return') \
                .as_pymongo()
            factor_return[factor_name] = pd.DataFrame(
                factor_regression_analysis_result[0]['factor_return'])
            factor_return[factor_name].index = pd.DatetimeIndex(
                factor_return[factor_name].index)

            factor_ic_result = FactorIcAnalysisResult.objects(factor_name=factor_name) \
                .only('factor_name') \
                .only('begin_date') \
                .only('end_date') \
                .only('ic') \
                .as_pymongo()
            # print(factor_ic_result)
            factor_ic[factor_name] = pd.DataFrame(factor_ic_result[0]['ic'])