示例#1
0
def rank_fund_manager(end_date, fund_code, rank_pool, fund_name, mage_date):

    """ 某个基金经理管理以来排名 """
    today = datetime.strptime(end_date, "%Y%m%d")
    before_1y = datetime(year=today.year - 1, month=today.month, day=today.day).strftime("%Y%m%d")
    before_2y = datetime(year=today.year - 2, month=today.month, day=today.day).strftime("%Y%m%d")
    before_3y = datetime(year=today.year - 3, month=today.month, day=today.day).strftime("%Y%m%d")
    before_5y = datetime(year=today.year - 5, month=today.month, day=today.day).strftime("%Y%m%d")

    date_array = np.array([
        ["2019年", "20190101", end_date, '20180930'],
        ["2018年", '20180101', "20181231", '20170930'],
        ["2017年", "20170101", '20171231', "20160930"],
        ["2016年", "20160101", '20161231', "20150930"],
        ["2015年", "20150101", '20151231', "20140930"],
        ["管理以来", mage_date, end_date, mage_date],
        ["过去1年", before_1y, end_date, before_1y],
        ["过去2年", before_2y, end_date, before_2y],
        ["过去3年", before_3y, end_date, before_3y],
        ["过去5年", before_5y, end_date, before_3y]])

    rank_percent = pd.DataFrame([], index=[fund_name])
    rank_str = pd.DataFrame([], index=[fund_name])
    for i_date in range(len(date_array)):
        label = date_array[i_date, 0]
        beg_date = date_array[i_date, 1]
        end_date = date_array[i_date, 2]
        new_fund_date = date_array[i_date, 3]
        if beg_date >= str(int(mage_date)):

            # if fund_code in ["162201.OF"]:
            #     str_rank, pct = MfcManagerMoney().cal_fund_index(
            #         rank_pool, "FTSE成长", fund_code, beg_date, end_date, "超额收益")
            # elif fund_code in ["162202.OF"]:
            #     str_rank, pct = MfcManagerMoney().cal_fund_index(
            #         rank_pool, "FTSE周期", fund_code, beg_date, end_date, "超额收益")
            # elif fund_code in ["162203.OF"]:
            #     str_rank, pct = MfcManagerMoney().cal_fund_index(
            #         rank_pool, "FTSE稳定", fund_code, beg_date, end_date, "超额收益")
            # else:
            #     str_rank, pct = FundRank().rank_fund(fund_code, rank_pool, beg_date, end_date, new_fund_date,
            #                                          excess=False)
            str_rank, pct = FundRank().rank_fund(fund_code, rank_pool, beg_date, end_date, new_fund_date, excess=False)
            rank_percent.loc[fund_name, label] = pct
            rank_str.loc[fund_name, label] = str_rank
        else:
            rank_percent.loc[fund_name, label] = "NAN"
            rank_str.loc[fund_name, label] = "NAN"
    print(rank_percent)
    print(rank_str)
    return rank_percent, rank_str
示例#2
0
def rank_fund_self(end_date, fund_code, rank_pool, fund_name, mage_date):

    """ 某个基金排名 """
    today = datetime.strptime(end_date, "%Y%m%d")
    before_1y = datetime(year=today.year - 1, month=today.month, day=today.day).strftime("%Y%m%d")
    before_2y = datetime(year=today.year - 2, month=today.month, day=today.day).strftime("%Y%m%d")
    before_3y = datetime(year=today.year - 3, month=today.month, day=today.day).strftime("%Y%m%d")
    before_5y = datetime(year=today.year - 5, month=today.month, day=today.day).strftime("%Y%m%d")
    before_10y = datetime(year=today.year - 10, month=today.month, day=today.day).strftime("%Y%m%d")

    date_array = np.array([
        ["2019年", "20190101", end_date, '20180930'],
        ["2018年", '20180101', "20181231", '20170930'],
        ["2017年", "20170101", '20171231', "20160930"],
        ["2016年", "20160101", '20161231', "20150930"],
        ["2015年", "20150101", '20151231', "20140930"],
        ["2014年", "20140101", '20141231', "20130930"],
        ["2013年", "20130101", '20131231', "20120930"],
        ["2012年", "20120101", '20121231', "20110930"],
        ["2011年", "20110101", '20111231', "20100930"],
        ["2010年", "20100101", '20101231', "20090930"],
        ["2009年", "20090101", '20091231', "20080930"],
        ["2008年", "20080101", '20081231', "20070930"],
        ["2007年", "20070101", '20071231', "20060930"],
        ["成立以来", mage_date, end_date, mage_date],
        ["过去1年", before_1y, end_date, before_1y],
        ["过去2年", before_2y, end_date, before_2y],
        ["过去3年", before_3y, end_date, before_3y],
        ["过去5年", before_5y, end_date, before_5y],
        ["过去10年", before_10y, end_date, before_10y],
    ])

    rank_percent = pd.DataFrame([], index=[fund_name])
    rank_str = pd.DataFrame([], index=[fund_name])
    for i_date in range(len(date_array)):
        label = date_array[i_date, 0]
        beg_date = date_array[i_date, 1]
        end_date = date_array[i_date, 2]
        new_fund_date = date_array[i_date, 3]
        if beg_date >= str(int(mage_date)):
            str_rank, pct = FundRank().rank_fund(fund_code, rank_pool, beg_date, end_date, new_fund_date, excess=False)
            rank_percent.loc[fund_name, label] = pct
            rank_str.loc[fund_name, label] = str_rank
        else:
            rank_percent.loc[fund_name, label] = "NAN"
            rank_str.loc[fund_name, label] = "NAN"
    print(rank_percent)
    print(rank_str)
    return rank_percent, rank_str
def write_public_fxwy(end_date, save_path):

    # 参数
    ###########################################################################################
    fund_name = '泰达宏利复兴伟业'
    fund_code = '001170.OF'
    fund_type = "公募"

    benchmark_code = '885001.WI'
    benchmark_name = '偏股混合基金总指数'
    benchmark_code_2 = "000300.SH"
    benchmark_name_2 = "沪深300"
    benchmark_ratio = 0.95

    setup_date = '20150421'  # 吴华开始管理 也是成立日
    today = datetime.strptime(end_date, "%Y%m%d")
    before_1y = datetime(year=today.year-1, month=today.month, day=today.day).strftime("%Y%m%d")
    before_2y = datetime(year=today.year-2, month=today.month, day=today.day).strftime("%Y%m%d")
    before_3y = datetime(year=today.year-3, month=today.month, day=today.day).strftime("%Y%m%d")
    before_5y = datetime(year=today.year-5, month=today.month, day=today.day).strftime("%Y%m%d")

    date_array = np.array([["2019年", '20190101', end_date, '20180930'],
                           ["2018年", "20180101", '20181231', "20170930"],
                           ["2017年", "20170101", '20171231', "20160930"],
                           ["2016年", '20160101', '20161231', "20150930"],
                           ["成立以来(吴华管理)", setup_date, end_date, setup_date],
                           ["过去1年", before_1y, end_date, before_1y],
                           ["过去2年", before_2y, end_date, before_2y],
                           ["过去3年", before_3y, end_date, before_3y],
                           ])

    benchmark_array = np.array([["沪深300", "000300.SH"],
                                ["中证500", "000905.SH"],
                                ["股票型基金总指数", '885012.WI'],
                                ["WIND全A", '881001.WI']])

    from quant.fund.fund import Fund
    fund_pct = Fund().get_fund_factor("Repair_Nav_Pct")
    bench_pct = Fund().get_fund_factor("Fund_Bench_Pct") * 100

    # 准备文件
    ###########################################################################################
    file_name = os.path.join(save_path, "OutFile", fund_name + '.xlsx')
    sheet_name = fund_name
    excel = WriteExcel(file_name)
    worksheet = excel.add_worksheet(sheet_name)

    # 写入基金表现 和基金排名
    ###########################################################################################
    performance_table = MfcTable().cal_summary_table_sample(fund_name, fund_code, fund_type, date_array, benchmark_array)
    rank1 = FundRank().rank_fund_array2(fund_pct, bench_pct, fund_code, date_array, "偏股混合型基金", excess=False)
    rank2 = FundRank().rank_fund_array2(fund_pct, bench_pct, fund_code, date_array, "灵活配置型基金_60", excess=False)
    rank3 = FundRank().rank_fund_array2(fund_pct, bench_pct, fund_code, date_array, "股票+灵活配置60型基金", excess=False)
    performance_table = pd.concat([performance_table, rank1, rank2, rank3], axis=0)

    col_number = 1
    num_format_pd = pd.DataFrame([], columns=performance_table.columns, index=['format'])
    num_format_pd.ix['format', :] = '0.00%'
    excel.write_pandas(performance_table, worksheet, begin_row_number=0, begin_col_number=col_number,
                       num_format_pd=num_format_pd, color="red", fillna=True)
    col_number = col_number + performance_table.shape[1] + 2

    # 读取基金和基准时间序列
    ###########################################################################################
    fund_data = MfcData().get_mfc_nav(fund_code, fund_name, fund_type)

    benchmark_data = Index().get_index_factor(benchmark_code, attr=["CLOSE"])
    fs = FinancialSeries(pd.DataFrame(fund_data), pd.DataFrame(benchmark_data))
    cum_return = fs.get_fund_and_bencnmark_cum_return_series(setup_date, end_date)

    benchmark_data = Index().get_index_factor(benchmark_code_2, attr=["CLOSE"])
    fs = FinancialSeries(pd.DataFrame(fund_data), pd.DataFrame(benchmark_data))
    cum_return2 = fs.get_bencnmark_cum_return_series(setup_date, end_date)

    # 写入基金和基准时间序列
    ###########################################################################################
    cum_return = pd.concat([cum_return, cum_return2], axis=1)
    cum_return.columns = [fund_name, benchmark_name, benchmark_name_2]
    cum_return = cum_return.dropna()

    num_format_pd = pd.DataFrame([], columns=cum_return.columns, index=['format'])
    num_format_pd.ix['format', :] = '0.00%'
    excel.write_pandas(cum_return, worksheet, begin_row_number=0, begin_col_number=col_number,
                       num_format_pd=num_format_pd, color="blue", fillna=True)

    # 基金和基准时间序列图
    ###########################################################################################
    chart_name = fund_name + "累计收益(管理以来)"
    series_name = [fund_name, benchmark_name, benchmark_name_2]
    insert_pos = 'B12'
    excel.line_chart_time_series_plot(worksheet, 0, col_number, cum_return,
                                      series_name, chart_name, insert_pos, sheet_name)
    excel.close()
    ###########################################################################################
    return True
示例#4
0
def write_public_qf(end_date, save_path):

    # 参数
    ###########################################################################################
    fund_name = '泰达宏利启富'
    fund_code = '003912.OF'
    fund_type = "公募"

    benchmark_code = '885003.WI'
    benchmark_name = '偏债混合型基金总指数'
    benchmark_code_2 = "881001.WI"
    benchmark_name_2 = "WIND全A"
    benchmark_ratio = 0.95

    setup_date = '20170315'
    date_array = np.array([["2019年", '20190101', end_date, '20180930'],
                           ["2018年", "20180101", '20181231', "20170930"],
                           ["2017年", setup_date, '20171231', setup_date],
                           ["成立以来", setup_date, end_date, setup_date]])

    benchmark_array = np.array([["沪深300",
                                 "000300.SH"], ["WIND全A", "881001.WI"],
                                ["中证全债", "H11001.CSI"],
                                ["偏债混合基金指数", '885003.WI']])

    from quant.fund.fund import Fund
    fund_pct = Fund().get_fund_factor("Repair_Nav_Pct")
    bench_pct = Fund().get_fund_factor("Fund_Bench_Pct") * 100

    # 准备文件
    ###########################################################################################
    file_name = os.path.join(save_path, "OutFile", fund_name + '.xlsx')
    sheet_name = fund_name
    excel = WriteExcel(file_name)
    worksheet = excel.add_worksheet(sheet_name)

    # 写入基金表现 和基金排名
    ###########################################################################################
    performance_table = MfcTable().cal_summary_table(fund_name, fund_code,
                                                     fund_type, date_array,
                                                     benchmark_array)
    rank0 = FundRank().rank_fund_array2(fund_pct,
                                        bench_pct,
                                        fund_code,
                                        date_array,
                                        "灵活配置型基金_30",
                                        excess=False)
    rank1 = FundRank().rank_fund_array2(fund_pct,
                                        bench_pct,
                                        fund_code,
                                        date_array,
                                        "wind",
                                        excess=False)
    performance_table = pd.concat([performance_table, rank0, rank1], axis=0)

    col_number = 1
    num_format_pd = pd.DataFrame([],
                                 columns=performance_table.columns,
                                 index=['format'])
    num_format_pd.ix['format', :] = '0.00%'
    excel.write_pandas(performance_table,
                       worksheet,
                       begin_row_number=0,
                       begin_col_number=col_number,
                       num_format_pd=num_format_pd,
                       color="red",
                       fillna=True)
    col_number = col_number + performance_table.shape[1] + 2

    # 读取基金和基准时间序列
    ###########################################################################################
    fund_data = MfcData().get_mfc_nav(fund_code, fund_name, fund_type)

    benchmark_data = Index().get_index_factor(benchmark_code, attr=["CLOSE"])
    fs = FinancialSeries(pd.DataFrame(fund_data), pd.DataFrame(benchmark_data))
    cum_return = fs.get_fund_and_bencnmark_cum_return_series(
        setup_date, end_date)

    benchmark_data = Index().get_index_factor(benchmark_code_2, attr=["CLOSE"])
    fs = FinancialSeries(pd.DataFrame(fund_data), pd.DataFrame(benchmark_data))
    cum_return2 = fs.get_bencnmark_cum_return_series(setup_date, end_date)

    # 写入基金和基准时间序列
    ###########################################################################################
    cum_return = pd.concat([cum_return, cum_return2], axis=1)
    cum_return.columns = [fund_name, benchmark_name, benchmark_name_2]
    cum_return = cum_return.dropna()

    num_format_pd = pd.DataFrame([],
                                 columns=cum_return.columns,
                                 index=['format'])
    num_format_pd.ix['format', :] = '0.00%'
    excel.write_pandas(cum_return,
                       worksheet,
                       begin_row_number=0,
                       begin_col_number=col_number,
                       num_format_pd=num_format_pd,
                       color="blue",
                       fillna=True)

    # 基金和基准时间序列图
    ###########################################################################################
    chart_name = fund_name + "累计收益(成立以来)"
    series_name = [fund_name, benchmark_name, benchmark_name_2]
    insert_pos = 'B12'
    excel.line_chart_time_series_plot(worksheet, 0, col_number, cum_return,
                                      series_name, chart_name, insert_pos,
                                      sheet_name)
    excel.close()
    ###########################################################################################
    return True
def write_public_zz500_adjust(end_date, save_path):

    # 参数
    ###########################################################################################
    fund_name_adjust = '泰达宏利中证500_adjust'
    fund_code_adjust = '162216.OF_adjust'
    fund_name = '泰达宏利中证500'
    fund_code = '162216.OF'
    fund_type = "公募"

    benchmark_code = '000905.SH'
    benchmark_name = '中证500'
    benchmark_ratio = 0.95

    setup_date = '20141003'
    today = datetime.strptime(end_date, "%Y%m%d")
    before_1y = datetime(year=today.year - 1, month=today.month,
                         day=today.day).strftime("%Y%m%d")
    before_2y = datetime(year=today.year - 2, month=today.month,
                         day=today.day).strftime("%Y%m%d")
    before_3y = datetime(year=today.year - 3, month=today.month,
                         day=today.day).strftime("%Y%m%d")

    date_array = np.array([
        ["2019年", '20190101', end_date, '20180930'],
        ["2018年", "20180101", '20181231', "20170930"],
        ["2017年", "20170101", '20171231', "20160930"],
        ["2016年", "20160101", "20161231", "20150930"],
        ["2015年", "20150101", "20151231", "20150101"],
        ["管理(20141003)以来", setup_date, end_date, setup_date],
        ["2015年以来", "20150101", end_date, setup_date],
        ["过去1年", before_1y, end_date, before_1y],
        ["过去2年", before_2y, end_date, before_2y],
        ["过去3年", before_3y, end_date, before_3y],
    ])

    benchmark_array = np.array([["沪深300", "000300.SH"], ["中证500", "000905.SH"],
                                ["WIND全A", '881001.WI']])

    from quant.fund.fund import Fund
    fund_pct = Fund().get_fund_factor("Repair_Nav_Pct")
    bench_pct = Fund().get_fund_factor("Fund_Bench_Pct") * 100

    # 准备文件
    ###########################################################################################
    file_name = os.path.join(save_path, "OutFile", fund_name_adjust + '.xlsx')
    sheet_name = fund_name_adjust
    excel = WriteExcel(file_name)
    worksheet = excel.add_worksheet(sheet_name)

    # 写入基金表现 和基金排名
    ###########################################################################################
    performance_table = MfcTable().cal_summary_table_sample(
        fund_name, fund_code, fund_type, date_array, benchmark_array)
    # rank0 = FundRank().rank_fund_array2(fund_pct, bench_pct, fund_code, date_array, "wind", excess=False)
    # rank1 = FundRank().rank_fund_array2(fund_pct, bench_pct, fund_code, date_array, "被动指数型基金", excess=True)
    rank2 = FundRank().rank_fund_array2(fund_pct,
                                        bench_pct,
                                        fund_code,
                                        date_array,
                                        "中证500基金",
                                        excess=False)
    performance_table = pd.concat([performance_table, rank2], axis=0)

    col_number = 1
    num_format_pd = pd.DataFrame([],
                                 columns=performance_table.columns,
                                 index=['format'])
    num_format_pd.ix['format', :] = '0.00%'
    excel.write_pandas(performance_table,
                       worksheet,
                       begin_row_number=0,
                       begin_col_number=col_number,
                       num_format_pd=num_format_pd,
                       color="red",
                       fillna=True)
    col_number = col_number + performance_table.shape[1] + 2

    # 写入增强基金表现
    ###########################################################################################
    performance_table = MfcTable().cal_summary_table_enhanced_fund(
        fund_name, fund_code, fund_type, date_array, benchmark_code,
        benchmark_name, benchmark_ratio)

    num_format_pd = pd.DataFrame([],
                                 columns=performance_table.columns,
                                 index=['format'])
    num_format_pd.ix['format', :] = '0.00%'
    excel.write_pandas(performance_table,
                       worksheet,
                       begin_row_number=0,
                       begin_col_number=col_number,
                       num_format_pd=num_format_pd,
                       color="red",
                       fillna=True)
    col_number = col_number + performance_table.shape[1] + 2

    # 读取基金和基准时间序列
    ###########################################################################################
    fund_data = MfcData().get_mfc_nav(fund_code, fund_name, fund_type)

    benchmark_data = Index().get_index_factor(benchmark_code, attr=["CLOSE"])
    fs = FinancialSeries(pd.DataFrame(fund_data), pd.DataFrame(benchmark_data),
                         benchmark_ratio)

    # 写入超额收益时间序列
    ###########################################################################################
    excess_cum_return = fs.get_cum_excess_return_series("20150101", end_date)

    num_format_pd = pd.DataFrame([],
                                 columns=excess_cum_return.columns,
                                 index=['format'])
    num_format_pd.ix['format', :] = '0.00%'
    excel.write_pandas(excess_cum_return,
                       worksheet,
                       begin_row_number=0,
                       begin_col_number=col_number,
                       num_format_pd=num_format_pd,
                       color="blue",
                       fillna=True)

    # 超额收益图
    ###########################################################################################
    chart_name = fund_name + "累计超额收益(2015年以来)"
    insert_pos = 'B12'
    excel.line_chart_one_series_with_linear_plot(worksheet, 0, col_number,
                                                 excess_cum_return, chart_name,
                                                 insert_pos, sheet_name)

    col_number = col_number + excess_cum_return.shape[1] + 2

    # 写入基金收益时间序列
    ###########################################################################################
    benchmark_data = Index().get_index_factor(benchmark_code, attr=["CLOSE"])
    fs = FinancialSeries(pd.DataFrame(fund_data), pd.DataFrame(benchmark_data))
    cum_return = fs.get_fund_and_bencnmark_cum_return_series(
        "20150101", end_date)

    num_format_pd = pd.DataFrame([],
                                 columns=cum_return.columns,
                                 index=['format'])
    num_format_pd.ix['format', :] = '0.00%'
    excel.write_pandas(cum_return,
                       worksheet,
                       begin_row_number=0,
                       begin_col_number=col_number,
                       num_format_pd=num_format_pd,
                       color="blue",
                       fillna=True)

    # 写入基金收益时间序列图
    ############################################################################################
    series_name = [fund_name, benchmark_name]
    chart_name = fund_name + "累计收益(2015年以来)"
    insert_pos = 'B26'
    excel.line_chart_time_series_plot(worksheet, 0, col_number, cum_return,
                                      series_name, chart_name, insert_pos,
                                      sheet_name)
    excel.close()
    ###########################################################################################
    return True
示例#6
0
    def fund_score(self, fund_code, fund_name, end_date, rank_pool, mg_date, fund_type, my_index_code):

        """ 计算基金得分 """

        # index_code = "881001.WI"
        # fund_code = "162208.OF"
        # end_date = "20181231"
        # rank_pool = "普通股票型基金"
        # mg_date = "20141121"
        # fund_type = "行业基金"
        # my_index_code = "FTSE成长"

        end_date = Date().change_to_datetime(end_date)
        before_1y = datetime(year=end_date.year, month=1, day=1).strftime("%Y%m%d")
        before_3y = datetime(year=end_date.year-2, month=1, day=1).strftime("%Y%m%d")
        before_3y = max(before_3y, "20160101")
        before_5y = datetime(year=end_date.year-4, month=1, day=1).strftime("%Y%m%d")
        before_5y = max(before_5y, "20160101")
        mg_date = max(mg_date, "20160101")
        end_date = Date().change_to_str(end_date)

        result = pd.DataFrame([], columns=["名称", "1年收益", "1年排名", "1年排名百分比", "1年得分",
                                           "3年收益", "3年排名", "3年排名百分比", "3年得分",
                                           "5年收益", "5年排名", "5年排名百分比", "5年得分"
                                           ])

        result.loc[fund_code, "名称"] = fund_name
        beg_date = before_1y
        fund_nav = MfcData().get_mfc_public_fund_nav(fund_code)
        fs = FinancialSeries(pd.DataFrame(fund_nav['NAV_ADJ']))
        result.loc[fund_code, "1年收益"] = fs.get_interval_return(beg_date, end_date)
        str_rank, pct = FundRank().rank_fund(fund_code, rank_pool, beg_date, end_date, beg_date, excess=False)
        result.loc[fund_code, "1年排名百分比"] = pct
        result.loc[fund_code, "1年排名"] = str_rank
        result.loc[fund_code, "1年得分"] = self.score(pct)

        beg_date = before_3y
        fund_nav = MfcData().get_mfc_public_fund_nav(fund_code)
        fs = FinancialSeries(pd.DataFrame(fund_nav['NAV_ADJ']))
        result.loc[fund_code, "3年收益"] = fs.get_interval_return(beg_date, end_date)
        str_rank, pct = FundRank().rank_fund(fund_code, rank_pool, beg_date, end_date, beg_date, excess=False)
        result.loc[fund_code, "3年排名百分比"] = pct
        result.loc[fund_code, "3年排名"] = str_rank
        result.loc[fund_code, "3年得分"] = self.score(pct)

        beg_date = before_5y
        fund_nav = MfcData().get_mfc_public_fund_nav(fund_code)
        fs = FinancialSeries(pd.DataFrame(fund_nav['NAV_ADJ']))
        result.loc[fund_code, "5年收益"] = fs.get_interval_return(beg_date, end_date)
        str_rank, pct = FundRank().rank_fund(fund_code, rank_pool, beg_date, end_date, beg_date, excess=False)
        result.loc[fund_code, "5年排名百分比"] = pct
        result.loc[fund_code, "5年排名"] = str_rank
        result.loc[fund_code, "5年得分"] = self.score(pct)

        beg_date = mg_date
        fund_nav = MfcData().get_mfc_public_fund_nav(fund_code)
        fs = FinancialSeries(pd.DataFrame(fund_nav['NAV_ADJ']))
        result.loc[fund_code, "管理以来收益"] = fs.get_interval_return(beg_date, end_date)
        str_rank, pct = FundRank().rank_fund(fund_code, rank_pool, beg_date, end_date, beg_date, excess=False)
        result.loc[fund_code, "管理以来排名"] = str_rank
        result.loc[fund_code, "管理以来排名百分比"] = pct
        result.loc[fund_code, "管理以来得分"] = self.score(pct)
        print(result)
        return result
示例#7
0
    def fund_excess_score(self, fund_code, fund_name, end_date, rank_pool, mg_date, fund_type, my_index_code):

        """ 行业基金超额收益得分 """
        end_date = Date().change_to_datetime(end_date)
        before_1y = datetime(year=end_date.year, month=1, day=1).strftime("%Y%m%d")
        before_3y = datetime(year=end_date.year-2, month=1, day=1).strftime("%Y%m%d")
        before_3y = max(before_3y, "20160101")
        before_5y = datetime(year=end_date.year-4, month=1, day=1).strftime("%Y%m%d")
        before_5y = max(before_5y, "20160101")
        mg_date = max(mg_date, "20160101")
        end_date = Date().change_to_str(end_date)

        result = pd.DataFrame([], columns=["名称", "1年超额收益", "1年超额排名", "1年超额排名百分比", "1年超额得分",
                                           "3年超额收益", "3年超额排名", "3年超额排名百分比", "3年超额得分",
                                           "5年超额收益", "5年超额排名", "5年超额排名百分比", "5年超额得分",
                                           "管理以来超额收益", "管理以来超额排名", "管理以来超额排名百分比", "管理以来超额得分"
                                           ])

        result.loc[fund_code, "名称"] = fund_name

        if fund_type == "行业基金":

            beg_date = before_1y
            excess_return, pct, rank_str = FundRank().rank_excess_fund(fund_pool_name=rank_pool, ge_index_code="881001.WI",
                                                                       my_index_code=my_index_code, my_fund_code=fund_code,
                                                                       beg_date=beg_date, end_date=end_date)
            result.loc[fund_code, "1年超额收益"] = excess_return
            result.loc[fund_code, "1年超额排名"] = rank_str
            result.loc[fund_code, "1年超额排名百分比"] = pct
            result.loc[fund_code, "1年超额得分"] = self.score(pct)

            beg_date = before_3y
            excess_return, pct, rank_str = FundRank().rank_excess_fund(fund_pool_name=rank_pool, ge_index_code="881001.WI",
                                                                       my_index_code=my_index_code, my_fund_code=fund_code,
                                                                       beg_date=beg_date, end_date=end_date)
            result.loc[fund_code, "3年超额收益"] = excess_return
            result.loc[fund_code, "3年超额排名"] = rank_str
            result.loc[fund_code, "3年超额排名百分比"] = pct
            result.loc[fund_code, "3年超额得分"] = self.score(pct)

            beg_date = before_5y
            excess_return, pct, rank_str = FundRank().rank_excess_fund(fund_pool_name=rank_pool, ge_index_code="881001.WI",
                                                                       my_index_code=my_index_code, my_fund_code=fund_code,
                                                                       beg_date=beg_date, end_date=end_date)
            result.loc[fund_code, "5年超额收益"] = excess_return
            result.loc[fund_code, "5年超额排名百分比"] = pct
            result.loc[fund_code, "5年超额排名"] = rank_str
            result.loc[fund_code, "5年超额得分"] = self.score(pct)

            beg_date = mg_date
            excess_return, pct, rank_str = FundRank().rank_excess_fund(fund_pool_name=rank_pool,
                                                                       ge_index_code="881001.WI",
                                                                       my_index_code=my_index_code,
                                                                       my_fund_code=fund_code,
                                                                       beg_date=beg_date, end_date=end_date)
            result.loc[fund_code, "管理以来超额收益"] = excess_return
            result.loc[fund_code, "管理以来超额排名百分比"] = pct
            result.loc[fund_code, "管理以来超额排名"] = rank_str
            result.loc[fund_code, "管理以来超额得分"] = self.score(pct)

        return result
def write_public_lh(end_date, save_path):

    # 参数
    ###########################################################################################
    fund_name = '泰达宏利量化增强'
    fund_code = '001733.OF'
    fund_type = "公募"

    benchmark_code = '000905.SH'
    benchmark_name = '中证500'
    benchmark_ratio = 0.95

    setup_date = '20160830'
    date_array = np.array(
        [["2019年", '20190101', end_date, '20180930'],
         ["2018年", "20180101", '20181231', "20170930"],
         ["2017年", "20170101", '20171231', "20160930"],
         ["2016年", setup_date, "20161231", setup_date],
         ["成立(20160830)以来", setup_date, end_date, setup_date]])

    benchmark_array = np.array([["沪深300", "000300.SH"], ["中证500", "000905.SH"],
                                ["股票型基金", '885012.WI'], ["创业板指", '399006.SZ'],
                                ["WIND全A", '881001.WI']])

    from quant.fund.fund import Fund
    fund_pct = Fund().get_fund_factor("Repair_Nav_Pct")
    bench_pct = Fund().get_fund_factor("Fund_Bench_Pct") * 100

    # 准备文件
    ###########################################################################################
    file_name = os.path.join(save_path, "OutFile", fund_name + '.xlsx')
    sheet_name = fund_name
    excel = WriteExcel(file_name)
    worksheet = excel.add_worksheet(sheet_name)

    # 写入基金表现 和基金排名
    ###########################################################################################
    performance_table = MfcTable().cal_summary_table_sample(
        fund_name, fund_code, fund_type, date_array, benchmark_array)
    rank0 = FundRank().rank_fund_array2(fund_pct,
                                        bench_pct,
                                        fund_code,
                                        date_array,
                                        "中证500基金",
                                        excess=False)
    rank1 = FundRank().rank_fund_array2(fund_pct,
                                        bench_pct,
                                        fund_code,
                                        date_array,
                                        "普通股票型基金",
                                        excess=False)
    # rank2 = FundRank().rank_fund_array2(fund_pct, bench_pct, fund_code, date_array, "中证500基金", excess=True)
    # rank3 = FundRank().rank_fund_array2(fund_pct, bench_pct, fund_code, date_array, "指数增强型基金", excess=True)

    performance_table = pd.concat([performance_table, rank0, rank1], axis=0)

    col_number = 1
    num_format_pd = pd.DataFrame([],
                                 columns=performance_table.columns,
                                 index=['format'])
    num_format_pd.ix['format', :] = '0.00%'
    excel.write_pandas(performance_table,
                       worksheet,
                       begin_row_number=0,
                       begin_col_number=col_number,
                       num_format_pd=num_format_pd,
                       color="red",
                       fillna=True)
    col_number = col_number + performance_table.shape[1] + 2

    # 写入增强基金表现
    ###########################################################################################
    performance_table = MfcTable().cal_summary_table_enhanced_fund(
        fund_name, fund_code, fund_type, date_array, benchmark_code,
        benchmark_name, benchmark_ratio)

    num_format_pd = pd.DataFrame([],
                                 columns=performance_table.columns,
                                 index=['format'])
    num_format_pd.ix['format', :] = '0.00%'
    excel.write_pandas(performance_table,
                       worksheet,
                       begin_row_number=0,
                       begin_col_number=col_number,
                       num_format_pd=num_format_pd,
                       color="red",
                       fillna=True)
    col_number = col_number + performance_table.shape[1] + 2

    # 读取基金和基准时间序列
    ###########################################################################################
    fund_data = MfcData().get_mfc_nav(fund_code, fund_name, fund_type)
    benchmark_data = Index().get_index_factor(benchmark_code, attr=["CLOSE"])
    fs = FinancialSeries(pd.DataFrame(fund_data), pd.DataFrame(benchmark_data),
                         benchmark_ratio)

    # 写入超额收益时间序列
    ###########################################################################################
    excess_cum_return = fs.get_cum_excess_return_series(setup_date, end_date)

    num_format_pd = pd.DataFrame([],
                                 columns=excess_cum_return.columns,
                                 index=['format'])
    num_format_pd.ix['format', :] = '0.00%'
    excel.write_pandas(excess_cum_return,
                       worksheet,
                       begin_row_number=0,
                       begin_col_number=col_number,
                       num_format_pd=num_format_pd,
                       color="blue",
                       fillna=True)

    # 超额收益图
    ###########################################################################################
    chart_name = fund_name + "累计超额收益(成立以来)"
    insert_pos = 'B16'
    excel.line_chart_one_series_with_linear_plot(worksheet, 0, col_number,
                                                 excess_cum_return, chart_name,
                                                 insert_pos, sheet_name)

    col_number = col_number + excess_cum_return.shape[1] + 2

    # 写入基金收益时间序列
    ###########################################################################################
    benchmark_data = Index().get_index_factor(benchmark_code, attr=["CLOSE"])
    fs = FinancialSeries(pd.DataFrame(fund_data), pd.DataFrame(benchmark_data))
    cum_return = fs.get_fund_and_bencnmark_cum_return_series(
        setup_date, end_date)

    num_format_pd = pd.DataFrame([],
                                 columns=cum_return.columns,
                                 index=['format'])
    num_format_pd.ix['format', :] = '0.00%'
    excel.write_pandas(cum_return,
                       worksheet,
                       begin_row_number=0,
                       begin_col_number=col_number,
                       num_format_pd=num_format_pd,
                       color="blue",
                       fillna=True)

    # 写入基金收益时间序列图
    ############################################################################################
    series_name = [fund_name, benchmark_name]
    chart_name = fund_name + "累计收益(成立以来)"
    insert_pos = 'B32'
    excel.line_chart_time_series_plot(worksheet, 0, col_number, cum_return,
                                      series_name, chart_name, insert_pos,
                                      sheet_name)
    excel.close()
    ###########################################################################################
    return True