def cal_summary_table_enhanced_fund(fund_name, fund_code, fund_type, date_array, benchmark_code, benchmark_name, benchmark_ratio=1.0): """ 指数型基金表现总结 分区间计算 基金和基准表现(累计收益 年化收益 超额收益 跟踪误差 信息比率 超额收益最大回撤等) """ # 分类读取基金数据 fund_data = MfcData().get_mfc_nav(fund_code, fund_name, fund_type) benchmark_data = Index().get_index_factor(benchmark_code, attr=["CLOSE"]) enhanced_table = pd.DataFrame([], columns=date_array[:, 0]) fs = FinancialSeries(pd.DataFrame(fund_data), pd.DataFrame(benchmark_data), benchmark_ratio) for i_date in range(date_array.shape[0]): label = date_array[i_date, 0] bd = Date().change_to_str(date_array[i_date, 1]) ed = Date().change_to_str(date_array[i_date, 2]) enhanced_table.loc[fund_name + "累计收益", label] = fs.get_interval_return(bd, ed) enhanced_table.loc[benchmark_name + "累计收益", label] = fs.get_interval_return_benchmark( bd, ed) bench_return = fs.get_interval_return_benchmark_ratio(bd, ed) enhanced_table.loc[benchmark_name + "*%s累计收益" % benchmark_ratio, label] = bench_return enhanced_table.loc[fund_name + "超额收益", label] = fs.get_interval_excess_return(bd, ed) enhanced_table.loc[fund_name + "超额年化收益", label] = fs.get_interval_excess_return_annual( bd, ed) enhanced_table.loc[fund_name + "跟踪误差", label] = fs.get_interval_tracking_error(bd, ed) enhanced_table.loc[fund_name + "信息比率", label] = fs.get_interval_mean_ir(bd, ed) enhanced_table.loc[ fund_name + "超额收益最大回撤", label] = fs.get_interval_excess_return_max_drawdown(bd, ed) enhanced_table.loc[fund_name + "标准差", label] = fs.get_interval_std_annual(bd, ed) enhanced_table.loc[fund_name + "夏普比率", label] = fs.get_interval_shape_ratio( bd, ed, 0.03) enhanced_table.loc[fund_name + "最大回撤", label] = fs.get_interval_max_drawdown(bd, ed) enhanced_table.loc[fund_name + "年化收益", label] = fs.get_interval_return_annual(bd, ed) enhanced_table.loc[ benchmark_name + "年化收益", label] = fs.get_interval_return_annual_benchmark(bd, ed) return enhanced_table
def mfcteda_fund_excess_return(self, end_date, fund_type, fund_code, fund_name, benchmark_code, benchmark_ratio): """ 计算1个基金今年所有月份超额收益 """ date_pd = self.get_date_pd(end_date) if fund_type == "公募": fund_data = MfcData().get_mfc_public_fund_nav(fund_code) fund_data = fund_data['NAV_ADJ'] else: fund_data = MfcData().get_mfc_private_fund_nav(fund_name) fund_data = fund_data['累计复权净值'] benchmark_data = Index().get_index_factor(benchmark_code, attr=["CLOSE"]) return_pd = pd.DataFrame([], columns=date_pd.columns, index=[fund_name]) for i in date_pd.columns: beg_date = date_pd.ix["beg_date", i] end_date = date_pd.ix["end_date", i] fs = FinancialSeries( pd.DataFrame(fund_data), pd.DataFrame(benchmark_data) * benchmark_ratio) try: return_pd.ix[fund_name, i] = fs.get_interval_excess_return( beg_date, end_date) except Exception as e: pass return return_pd
def cal_fund_index(self, fund_pool_name, my_index_code, my_fund_code, beg_date, end_date): """ 计算某只基金所在基金池的各项指标(包括基金收益、基金基准收益、超额收益、跟踪误差及信息比率) 剔除新基金 """ fund_pool = Fund().get_fund_pool_all(date="20181231", name=fund_pool_name) fund_pool = fund_pool[fund_pool['setupdate'] < beg_date] fund_pool = list(fund_pool['wind_code'].values) fund_pool.append(my_fund_code) result = pd.DataFrame([], index=fund_pool) data = Fund().get_fund_factor("Repair_Nav") for i in range(0, len(fund_pool)): fund_code = fund_pool[i] if fund_code == my_fund_code: index_code = my_index_code else: index_code = "881001.WI" print(fund_code, index_code, beg_date, end_date) try: fund = pd.DataFrame(data[fund_code]) index = Index().get_index_factor(index_code, attr=["CLOSE"]) fs = FinancialSeries(pd.DataFrame(fund), pd.DataFrame(index)) fund_return = fs.get_interval_return_annual(beg_date, end_date) bench_return = fs.get_interval_return_benchmark(beg_date, end_date) excess_return = fs.get_interval_excess_return(beg_date, end_date) tracking_error = fs.get_interval_tracking_error(beg_date, end_date) ir = excess_return / tracking_error result.loc[fund_code, "基准收益"] = bench_return result.loc[fund_code, "基金收益"] = fund_return result.loc[fund_code, "超额收益"] = - bench_return + fund_return result.loc[fund_code, "跟踪误差"] = tracking_error result.loc[fund_code, "信息比率"] = ir except Exception as e: print(e) result = result.dropna() result = result[~result.index.duplicated()] result = result.sort_values(by=['基金收益'], ascending=False) result['收益名次'] = range(1, len(result) + 1) result['收益排名'] = result['收益名次'].map(lambda x: str(x) + '/' + str(len(result))) file = "%s_%s_%s_%s.csv" % (fund_pool_name, my_fund_code, beg_date, end_date) file = os.path.join(self.data_path, 'data', file) result.to_csv(file)
def cal_summary_period(self, beg_date=None, end_date=None): """ 计算组合在区间内 收益率、波动率等表现情况 并画图并存储图片 """ self.get_port_return() if beg_date is None: beg_date = self.port_return.index[0] if end_date is None: end_date = self.port_return.index[-1] fs = FinancialSeries( pd.DataFrame(self.port_return["CumPortReturn"] + 1.0), pd.DataFrame(self.port_return["CumIndexReturn"] + 1.0)) port_return = self.port_return.loc[beg_date:end_date, :] result = pd.DataFrame([]) beg_date = port_return.index[0] end_date = port_return.index[-1] label = str(beg_date)[0:4] + '年' result.loc[label, '开始时间'] = beg_date result.loc[label, '结束时间'] = end_date result.loc[label, "收益率"] = fs.get_interval_return(beg_date, end_date) result.loc[label, "年化收益率"] = fs.get_interval_return_annual( beg_date, end_date) result.loc[label, "年化波动率"] = fs.get_interval_std_annual(beg_date, end_date) result.loc[label, "年化跟踪误差"] = fs.get_interval_tracking_error( beg_date, end_date) result.loc[label, "最大回撤率"] = fs.get_interval_max_drawdown(beg_date, end_date) result.loc[label, "超额收益率"] = fs.get_interval_excess_return( beg_date, end_date) result.loc[label, "年化超额收益率"] = fs.get_interval_excess_return_annual( beg_date, end_date) result.loc[label, "超额收益率最大回撤"] = fs.get_interval_excess_return_max_drawdown( beg_date, end_date) result.loc[label, "基准收益率"] = fs.get_interval_return_benchmark( beg_date, end_date) result.loc[label, "基准年化收益率"] = fs.get_interval_return_annual_benchmark( beg_date, end_date) result.loc[label, "基准年化波动率"] = fs.get_interval_std_annual_benchmark( beg_date, end_date) result.loc[label, "基准最大回撤率"] = fs.get_interval_max_drawdown_benchmark( beg_date, end_date) self.plot_port_return_period(beg_date, end_date) return result.T