Ejemplo n.º 1
0
 def run_alpha(self):
     fund_code = self.get_fund_code_by_filter(ALPHA_FILTER)
     run_cnt = 0
     fd_num = len(fund_code)
     logging.info('funds number: {}'.format(fd_num))
     for code in fund_code:
         run_cnt += 1
         logging.info('code: {} pct: {} / {}'.format(code, run_cnt, fd_num))
         for date_list in self.run_date_list:
             report_date = date_list[0]
             # 只跑近一年
             for year_lag in range(1, 4):
                 tmp_date = date_list[4 - year_lag]
                 start_date = pd.date_range(
                     start=tmp_date, periods=2,
                     freq='D').strftime('%Y%m%d').tolist()[-1]
                 try:
                     rst_value = compute_alpha_categroy(code,
                                                        start_date,
                                                        report_date,
                                                        run_type=0)
                     logging.info('start_date: {} end_date: {}'.format(
                         start_date, report_date))
                     logging.info('code {} year_lag {} alpha{}'.format(
                         code, year_lag, rst_value))
                 except Exception as err:
                     logging.warning(err)
                     rst_value = None
                 rec = (code,
                        'AlphaCategroyBenchmark{}Year'.format(year_lag),
                        str(rst_value), None, self.algid, report_date,
                        self.thisfilename, self.todaydate)
                 self.db_insert_session.add_info(rec)
         # 跑半年
         for date_list in self.half_year_date_list:
             report_date = date_list[0]
             tmp_date = date_list[1]
             start_date = pd.date_range(
                 start=tmp_date, periods=2,
                 freq='D').strftime('%Y%m%d').tolist()[-1]
             try:
                 rst_value = compute_alpha_categroy(code,
                                                    start_date,
                                                    report_date,
                                                    run_type=0)
                 my_logger.info('start_date: {} end_date: {}'.format(
                     start_date, report_date))
                 my_logger.info('code {} year_lag {} alpha:{}'.format(
                     code, 'half', rst_value))
             except Exception as err:
                 my_logger.warning(err)
                 rst_value = None
             rec = (code, 'AlphaCategroyBenchmark6Month'.format(year_lag),
                    str(rst_value), None, self.algid, report_date,
                    self.thisfilename, self.todaydate)
             self.db_insert_session.add_info(rec)
     self.db_insert_session.finish()
     return
Ejemplo n.º 2
0
    def run_alpha0(self):
        fund_code = self.get_fund_code_by_filter(ALPHA_FILTER)
        run_cnt = 0
        fd_num = len(fund_code)
        logging.info('funds number: {}'.format(fd_num))
        for code in fund_code:
            run_cnt += 1
            logging.info('code: {} pct: {} / {}'.format(code, run_cnt, fd_num))
            for date_list in self.run_date_list:
                report_date = date_list[0]

                start_date = self.find_fund_clr(code)

                if start_date > report_date:
                    continue

                try:
                    rst_value = compute_alpha_categroy(code, start_date, report_date, run_type=0)
                    logging.info('start_date: {} end_date: {}'.format(start_date, report_date))
                    logging.info('code {} year_lag {} alpha{}'.format(code, "", rst_value))
                except Exception as err:
                    logging.warning(err)
                    rst_value = None
                rec = (
                code, 'AlphaCategroyBenchmarkAll', str(rst_value), None, self.algid, report_date, self.thisfilename,
                self.todaydate)
                print(rec)
                self.db_insert_session.add_info(rec)
        self.db_insert_session.finish()
Ejemplo n.º 3
0
def compute_alpha(code, start_date, end_date):
    ''' 计算相对收益alpha
    '''
    # to do
    try:
        alpha = compute_alpha_categroy(code, start_date, end_date)
    except Exception as err:
        print(err)
        logging.debug(err)
        alpha = None
    logging.info('\t alpha: {}'.format(alpha))
    return alpha
Ejemplo n.º 4
0
def test_compute_alpha_categroy():
    codes = '960042' 
    codes = '370010'
    codes = '110011'
    codes = '002073'
    start, end = '20151231', '20181231'

    # run_type: 0 同类基准, 1 同类平均, 2 基金基准
    # 基金基准 空值过多
    codes, start, end = '519644', '20150101', '20161231'
    codes, start, end = '370010', '20151001', '20180930'
    '''
    run_type = 2
    rst = compute_alpha_categroy(codes, start, end, run_type)
    print(rst)
    run_type = 1
    rst = compute_alpha_categroy(codes, start, end, run_type)
    print(rst)
    '''
    run_type = 0
    rst = compute_alpha_categroy(codes, start, end, run_type)
    print(rst)
    '''
    # 同类基准
    run_type = 0
    rst = compute_alpha_categroy(codes, start, end, run_type)
    print(rst)
    # 测试是否根据不同净值取不同基准
    start, end = '20120101', '20180101'
    run_type = 0
    rst = compute_alpha_categroy(codes, start, end, run_type)
    print(rst)
    return
    '''
    '''
    # 同类平均, 时间开销测试
    s_time = time.time()
    start, end = '20160331', '20190331'
    run_type = 1
    rst = compute_alpha_categroy(codes, start, end, run_type)
    print(rst)
    e_time = time.time()
    print('caculate cost {}'.format(e_time - s_time))
    '''
    return
Ejemplo n.º 5
0
def test_compute_alpha_categroy():
    # 计算基金基准
    code, start, end = '050004', '20040622', '20150710'
    run_type = 2
    rst = compute_alpha_categroy(code, start, end, run_type)
    print(rst)

    # 基金基准不足,改取同类基准进行计算
    code, start, end = '050004', '20040622', '20190710'
    run_type = 2
    rst = compute_alpha_categroy(code, start, end, run_type)
    print(rst)

    codes, start, end = '519644', '20150101', '20161231'
    run_type = 2
    rst = compute_alpha_categroy(code, start, end, run_type)
    print(rst)
    '''
    # 货币型 没有同类基准
    code, start, end = '370010', '20151001', '20180930'
    run_type = 0
    rst = compute_alpha_categroy(code, start, end, run_type)
    print(rst)
    '''

    # 测试 基准不够去同类基准流程
    code, start, end = '184691', '19990504', '20140409'
    run_type = 2
    rst = compute_alpha_categroy(code, start, end, run_type)
    print(rst)
    '''
    # run_type: 0 同类基准, 1 同类平均, 2 基金基准
    # 基金基准 空值过多
    codes, start, end = '519644', '20150101', '20161231'
    codes, start, end = '370010', '20151001', '20180930'
    run_type = 2
    rst = compute_alpha_categroy(codes, start, end, run_type)
    print(rst)
    run_type = 1
    rst = compute_alpha_categroy(codes, start, end, run_type)
    print(rst)
    run_type = 0
    rst = compute_alpha_categroy(codes, start, end, run_type)
    print(rst)
    # 同类基准
    run_type = 0
    rst = compute_alpha_categroy(codes, start, end, run_type)
    print(rst)
    # 测试是否根据不同净值取不同基准
    start, end = '20120101', '20180101'
    run_type = 0
    rst = compute_alpha_categroy(codes, start, end, run_type)
    print(rst)
    return
    # 同类平均, 时间开销测试
    s_time = time.time()
    start, end = '20160331', '20190331'
    run_type = 1
    rst = compute_alpha_categroy(codes, start, end, run_type)
    print(rst)
    e_time = time.time()
    print('caculate cost {}'.format(e_time - s_time))
    '''
    return