fund_rank.py 13.3 KB
Newer Older
李宗熹's avatar
李宗熹 committed
1 2 3 4 5 6
# db = create_engine(
#     'mysql+pymysql://tamp_fund:@imeng408@tamper.mysql.polardb.rds.aliyuncs.com:3306/tamp_fund?charset=utf8mb4',
#     pool_size=50,
#     pool_recycle=3600,
#     pool_pre_ping=True)
# con = db.connect()
李宗熹's avatar
李宗熹 committed
7

wang zhengwei's avatar
wang zhengwei committed
8 9
# import logging
# logging.basicConfig(level=logging.INFO)
李宗熹's avatar
李宗熹 committed
10

李宗熹's avatar
李宗熹 committed
11
from app.api.engine import tamp_fund_engine, TAMP_SQL, tamp_product_engine
李宗熹's avatar
李宗熹 committed
12
from app.utils.week_evaluation import *
李宗熹's avatar
李宗熹 committed
13 14


李宗熹's avatar
李宗熹 committed
15
def get_tamp_nav(fund, start_date, rollback=False, invest_type=2):
李宗熹's avatar
李宗熹 committed
16 17 18 19 20 21
    """获取基金ID为fund, 起始日期为start_date, 终止日期为当前日期的基金净值表

    Args:
        fund[str]:基金ID
        start_date[date]:起始日期
        rollback[bool]:当起始日期不在净值公布日历中,是否往前取最近的净值公布日
李宗熹's avatar
李宗熹 committed
22
        invest_type[num]:0:公募 1:私募 2:优选
李宗熹's avatar
李宗熹 committed
23 24 25 26

    Returns:df[DataFrame]: 索引为净值公布日, 列为复权净值的净值表; 查询失败则返回None

    """
李宗熹's avatar
李宗熹 committed
27
    with TAMP_SQL(tamp_product_engine) as tamp_product, TAMP_SQL(tamp_fund_engine) as tamp_fund:
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
28
        tamp_product_session = tamp_product.session
李宗熹's avatar
李宗熹 committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
        tamp_fund_session = tamp_fund.session
        # if invest_type == "private":
        #     sql = "SELECT fund_id, price_date, cumulative_nav FROM fund_nav " \
        #           "WHERE fund_id='{}'".format(fund)
        #     # df = pd.read_sql(sql, con).dropna(how='any')
        #     cur = tamp_product_session.execute(sql)
        if invest_type == 0:
            sql = """select distinct `id`, `end_date`, `accum_nav` from `public_fund_nav` where `id`='{}'  order by `end_date` ASC""".format(
                fund)
            cur = tamp_fund_session.execute(sql)
        elif invest_type == 1:
            sql = """select distinct `fund_id`, `price_date`,`cumulative_nav` from `fund_nav` where `fund_id`='{}'  order by `price_date` ASC""".format(
                fund)
            cur = tamp_fund_session.execute(sql)
        elif invest_type == 2:
            sql = """select distinct `fund_id`,`price_date`,`cumulative_nav` from `fund_nav` where `fund_id`='{}'  order by `price_date` ASC""".format(
                fund)
李宗熹's avatar
李宗熹 committed
46 47
            cur = tamp_product_session.execute(sql)

李宗熹's avatar
李宗熹 committed
48 49 50
        data = cur.fetchall()
        df = pd.DataFrame(data, columns=['fund_id', 'price_date', 'cumulative_nav']).dropna(how='any')
        df.rename({'price_date': 'end_date', 'cumulative_nav': 'adj_nav'}, axis=1, inplace=True)
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
51
        df['end_date'] = pd.to_datetime(df['end_date'])
李宗熹's avatar
李宗熹 committed
52

pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
53 54
        if rollback and df['end_date'].min() < start_date < df['end_date'].max():
            while start_date not in list(df['end_date']):
李宗熹's avatar
李宗熹 committed
55 56
                start_date -= datetime.timedelta(days=1)

pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
57 58 59 60
        df = df[df['end_date'] >= start_date]
        df.drop_duplicates(subset='end_date', inplace=True, keep='first')
        df.set_index('end_date', inplace=True)
        df.sort_index(inplace=True, ascending=True)
李宗熹's avatar
李宗熹 committed
61
    return df
李宗熹's avatar
李宗熹 committed
62 63 64


def get_frequency(df):
李宗熹's avatar
李宗熹 committed
65 66 67 68 69 70 71 72
    """获取基金净值一年当中公布的频率

    Args:
        df[DataFrame]:以基金净值公布日期为索引的基金净值表

    Returns:[int]: 年公布频率;查询失败则返回ValueError

    """
李宗熹's avatar
李宗熹 committed
73
    index_series = df.index.to_series()
李宗熹's avatar
李宗熹 committed
74 75
    # freq_series = index_series - index_series.shift(1)
    freq_series = index_series.diff(1)
wang zhengwei's avatar
wang zhengwei committed
76
    # logging.log(logging.DEBUG, freq_series.describe())
赵杰's avatar
赵杰 committed
77 78 79 80
    try:
        f = freq_series.mode()[0].days
    except:
        return 250
李宗熹's avatar
李宗熹 committed
81 82 83 84 85 86 87 88 89 90 91
    if f in range(0, 3):
        return 250
    elif f in range(6, 9):
        return 52
    elif f in range(13, 18):
        return 24
    elif f in range(28, 33):
        return 12
    elif f in range(110, 133):
        return 3
    else:
赵杰's avatar
赵杰 committed
92
        return 250
李宗熹's avatar
李宗熹 committed
93 94


李宗熹's avatar
李宗熹 committed
95 96
def get_trade_cal():
    """获取上交所交易日历表
李宗熹's avatar
李宗熹 committed
97

李宗熹's avatar
李宗熹 committed
98
    Returns:df[DataFrame]: 索引为交易日, 列为交易日的上交所交易日历表
李宗熹's avatar
李宗熹 committed
99

李宗熹's avatar
李宗熹 committed
100
    """
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
101 102
    with TAMP_SQL(tamp_fund_engine) as tamp_product:
        tamp_product_session = tamp_product.session
李宗熹's avatar
李宗熹 committed
103
        sql = 'SELECT cal_date FROM stock_trade_cal WHERE is_open=1'
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
104
        cur = tamp_product_session.execute(sql)
李宗熹's avatar
李宗熹 committed
105 106 107 108 109 110
        data = cur.fetchall()
        df = pd.DataFrame(list(data), columns=['cal_date']).dropna(how='all')
        # df = pd.read_sql(sql, con)
        df['end_date'] = pd.to_datetime(df['cal_date'])
        df.set_index('end_date', drop=False, inplace=True)
        return df
李宗熹's avatar
李宗熹 committed
111 112 113 114 115 116 117 118 119 120 121


def get_manager(invest_type):
    """获取基金对应基金经理表

    Args:
        invest_type: 资产类型:公募, 私募等

    Returns:

    """
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
122 123
    with TAMP_SQL(tamp_fund_engine) as tamp_product:
        tamp_product_session = tamp_product.session
李宗熹's avatar
李宗熹 committed
124 125 126
        if invest_type == 'public':
            sql = 'SELECT ts_code, name FROM public_fund_manager WHERE end_date IS NULL'
            # df = pd.read_sql(sql, con)
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
127
            cur = tamp_product_session.execute(sql)
李宗熹's avatar
李宗熹 committed
128
            data = cur.fetchall()
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
129
            df = pd.DataFrame(list(data), columns=['ts_code', 'name'])
李宗熹's avatar
李宗熹 committed
130
        else:
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
131
            sql = 'SELECT fund_id, fund_manager_id FROM fund_manager_mapping'
李宗熹's avatar
李宗熹 committed
132 133 134
            # df = pd.read_sql(sql, con)
            cur = tamp_product_session.execute(sql)
            data = cur.fetchall()
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
135 136
            df = pd.DataFrame(list(data), columns=['fund_id', 'fund_manager_id'])
        return df
李宗熹's avatar
李宗熹 committed
137 138


李宗熹's avatar
李宗熹 committed
139
def get_fund_info(fund, end_date, invest_type):
李宗熹's avatar
李宗熹 committed
140 141 142 143 144 145 146 147 148
    """[summary]

    Args:
        end_date ([type]): [description]
        invest_type ([type]): [description]

    Returns:
        [type]: [description]
    """
李宗熹's avatar
李宗熹 committed
149 150
    with TAMP_SQL(tamp_fund_engine) as tamp_fund:
        tamp_fund_session = tamp_fund.session
李宗熹's avatar
李宗熹 committed
151 152 153 154
        if invest_type == 'public':
            sql = "SELECT ts_code, fund_type, management FROM public_fund_basic " \
                  "WHERE delist_date IS NULL AND (due_date IS NULL OR due_date>'{}')".format(end_date.strftime('%Y%m%d'))
            # df = pd.read_sql(sql, con).dropna(how='all')
李宗熹's avatar
李宗熹 committed
155
            cur = tamp_fund_session.execute(sql)
李宗熹's avatar
李宗熹 committed
156 157
            data = cur.fetchall()

pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
158 159
            df = pd.DataFrame(list(data), columns=['ts_code', 'fund_type', 'management'])
            manager_info = get_manager(invest_type)
李宗熹's avatar
李宗熹 committed
160

pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
161 162
            df.rename({'ts_code': 'fund_id'}, axis=1, inplace=True)
            df = pd.merge(df, manager_info, how="left", on='fund_id')
李宗熹's avatar
李宗熹 committed
163
        else:
李宗熹's avatar
李宗熹 committed
164 165 166 167 168
            sql = "SELECT a.id, a.substrategy, b.fund_manager_id " \
                  "FROM fund_info as a LEFT JOIN fund_manager_mapping as b " \
                  "ON a.id = b.fund_id WHERE a.delete_tag=0 " \
                  "AND a.substrategy!=-1 AND a.id='{}'".format(fund)
            cur = tamp_fund_session.execute(sql)
李宗熹's avatar
李宗熹 committed
169
            data = cur.fetchall()
李宗熹's avatar
李宗熹 committed
170
            df = pd.DataFrame(list(data), columns=['fund_id', 'substrategy', 'manager'])
李宗熹's avatar
李宗熹 committed
171 172
            # df = pd.read_sql(sql, con).dropna(how='all')

李宗熹's avatar
李宗熹 committed
173 174 175
            # df.rename({'id': 'fund_id'}, axis=1, inplace=True)
            # manager_info = get_manager(invest_type)
            # df = pd.merge(df, manager_info, how="inner", on='fund_id')
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
176
        return df
李宗熹's avatar
李宗熹 committed
177 178


李宗熹's avatar
李宗熹 committed
179
def resample(df, trading_cal, freq, simple_flag=True):
李宗熹's avatar
李宗熹 committed
180 181 182 183 184 185 186 187 188 189 190 191 192
    """对基金净值表进行粒度不同的重采样,并剔除不在交易日中的结果

    Args:
        df ([DataFrame]): [原始基金净值表]
        trading_cal ([DataFrame]): [上交所交易日表]
        freq ([int]): [重采样频率: 1:工作日,2:周, 3:月, 4:半月, 5:季度]

    Returns:
        [DataFrame]: [重采样后剔除不在交易日历中的净值表和交易日历以净值日期为索引的合表]
    """
    freq_dict = {250: 'B', 52: 'W-FRI', 12: 'M', 24: 'SM', 3: 'Q'}
    resample_freq = freq_dict[freq]
    # 按采样频率进行重采样并进行净值的前向填充
李宗熹's avatar
李宗熹 committed
193 194 195 196 197 198 199
    df = df.resample(rule=resample_freq, closed='right').ffill()

    # 计算年化指标时简化重采样过程
    if simple_flag and freq == 250:
        return pd.merge(df, trading_cal, how='inner', left_index=True, right_index=True)
    elif simple_flag and freq != 250:
        return df
李宗熹's avatar
李宗熹 committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

    # 根据采样频率确定最大日期偏移量(保证偏移后的日期与重采样的日期在同一周,同一月,同一季度等)
    timeoffset_dict = {250: 1, 52: 5, 12: 30, 24: 15, 3: 120}
    timeoffsetmax = timeoffset_dict[freq]

    # Dataframe不允许直接修改index,新建一份index的复制并转为list
    new_index = list(df.index)
    # 遍历重采样后的日期
    for idx, date in enumerate(df.index):
        # 如果重采样后的日期不在交易日历中
        if date not in trading_cal['end_date']:
            # 对重采样后的日期进行偏移
            for time_offset in range(1, timeoffsetmax):
                # 如果偏移后的日期在交易日历中,保留偏移后的日期
                if date - datetime.timedelta(days=time_offset) in trading_cal['end_date']:
                    new_index[idx] = date - datetime.timedelta(days=time_offset)
                    # 任意一天满足立即退出循环
                    break

    # 更改净值表的日期索引为重采样后且在交易日内的日期
    df.index = pd.Series(new_index)
    return pd.merge(df, trading_cal, how='inner', left_index=True, right_index=True)


def z_score(annual_return_rank, downside_risk_rank, max_drawdown_rank, sharp_ratio_rank):
    return 25 * annual_return_rank + 25 * downside_risk_rank + 25 * max_drawdown_rank + 25 * sharp_ratio_rank


def cal_date(date, period_type, period):
    year, month, day = map(int, date.strftime('%Y-%m-%d').split('-'))
    if period_type == 'Y':
        cal_year = year - period
        return datetime.datetime(cal_year, month, day)
    elif period_type == 'm':
        cal_month = month - period
        if cal_month > 0:
            return datetime.datetime(year, cal_month, day)
        else:
            return datetime.datetime(year - 1, cal_month + 12, day)
    elif period_type == 'd':
        return date - datetime.timedelta(days=period)


def metric_rank(df):
李宗熹's avatar
李宗熹 committed
244
    for metric in ['annual_return', 'downside_risk', 'max_drawdown', 'sharp_ratio']:
李宗熹's avatar
李宗熹 committed
245 246 247 248
        if metric in ['downside_risk', 'max_drawdown']:
            ascending = False
        else:
            ascending = True
李宗熹's avatar
李宗熹 committed
249
        df['{}_rank'.format(metric)] = df.groupby(['substrategy'])[metric].rank(ascending=ascending, pct=True)
李宗熹's avatar
李宗熹 committed
250 251 252
    return df


李宗熹's avatar
李宗熹 committed
253
def fund_rank(start_date, end_date, invest_type=1):
李宗熹's avatar
李宗熹 committed
254 255 256 257
    fund_info = get_fund_info(end_date, invest_type=invest_type)

    group = fund_info.groupby('substrategy')
    grouped_fund = group['fund_id'].unique()
李宗熹's avatar
李宗熹 committed
258

李宗熹's avatar
李宗熹 committed
259
    trading_cal = get_trade_cal()
李宗熹's avatar
李宗熹 committed
260

李宗熹's avatar
李宗熹 committed
261 262
    metric_df = pd.DataFrame(columns=('fund_id', 'range_return', 'annual_return', 'max_drawdown', 'sharp_ratio',
                                      'volatility', 'sortino_ratio', 'downside_risk', 'substrategy'))
李宗熹's avatar
李宗熹 committed
263 264

    skipped_funds = []
李宗熹's avatar
李宗熹 committed
265 266
    for substrategy in grouped_fund.index:
        for fund in grouped_fund[substrategy]:
李宗熹's avatar
李宗熹 committed
267

李宗熹's avatar
李宗熹 committed
268
            df = get_tamp_nav(fund, start_date, rollback=False, invest_type=invest_type)
李宗熹's avatar
李宗熹 committed
269 270 271 272

            try:
                if df.index[-1] - df.index[0] < 0.6 * (end_date - start_date):
                    skipped_funds.append(fund)
wang zhengwei's avatar
wang zhengwei committed
273
                    # logging.log(logging.INFO, 'Skipped {}'.format(fund))
李宗熹's avatar
李宗熹 committed
274
                    continue
李宗熹's avatar
李宗熹 committed
275
                n = get_frequency(df)
李宗熹's avatar
李宗熹 committed
276
            except:
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
277
                # logging.log(logging.ERROR, repr(e))
wang zhengwei's avatar
wang zhengwei committed
278
                # logging.log(logging.INFO, 'Skipped {}'.format(fund))
李宗熹's avatar
李宗熹 committed
279 280
                continue

pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
281
            df = resample(df, trading_cal, n)
李宗熹's avatar
李宗熹 committed
282 283 284 285 286

            try:
                _ = get_frequency(df)
            except ValueError:
                continue
李宗熹's avatar
李宗熹 committed
287

wang zhengwei's avatar
wang zhengwei committed
288
            # logging.log(logging.INFO, "Dealing with {}".format(fund))
李宗熹's avatar
李宗熹 committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302
            net_worth = df['adj_nav'].astype(float)

            end_df, begin_df = net_worth.values[-1], net_worth.values[0]

            sim_return = simple_return(net_worth)
            ex_return = excess_return(sim_return, bank_rate=0.015, n=n)
            drawdown = float(max_drawdown(net_worth)[0])
            shp_ratio = sharpe_ratio(ex_return, sim_return, n)
            rng_return = float(range_return(end_df, begin_df))
            ann_return = annual_return(rng_return, net_worth, n)
            vol = volatility(sim_return, n)
            down_risk = downside_risk(sim_return, bank_rate=0.015, n=n)
            sor_ratio = sortino_ratio(ex_return, down_risk, n)

pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
303
            manager = fund_info[fund_info['fund_id'] == fund]['fund_manager_id'].values
李宗熹's avatar
李宗熹 committed
304
            # management = fund_info[fund_info['fund_id'] == fund]['management'].values
李宗熹's avatar
李宗熹 committed
305 306

            row = pd.Series([fund, rng_return, ann_return, drawdown, shp_ratio,
李宗熹's avatar
李宗熹 committed
307 308
                             vol, sor_ratio, down_risk, substrategy, manager],
                            index=['fund_id', 'range_return', 'annual_return', 'max_drawdown',
李宗熹's avatar
李宗熹 committed
309
                                   'sharp_ratio', 'volatility', 'sortino_ratio', 'downside_risk',
李宗熹's avatar
李宗熹 committed
310
                                   'substrategy', 'manager'])
李宗熹's avatar
李宗熹 committed
311
            metric_df = metric_df.append(row, ignore_index=True)
李宗熹's avatar
李宗熹 committed
312
    metric_df.set_index('fund_id', inplace=True)
李宗熹's avatar
李宗熹 committed
313 314 315 316 317 318 319 320 321 322 323 324

    df = metric_rank(metric_df)
    df['z_score'] = z_score(df['annual_return_rank'],
                            df['downside_risk_rank'],
                            df['max_drawdown_rank'],
                            df['sharp_ratio_rank'])
    return df


if __name__ == '__main__':
    end_date = datetime.datetime.now() - datetime.timedelta(days=1)
    start_date = cal_date(end_date, 'Y', 1)
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
325 326 327
    fund_rank = fund_rank(start_date, end_date, False)
    # fund_rank.to_csv("fund_rank.csv", encoding='gbk')
    # df = pd.read_csv('fund_rank.csv')
李宗熹's avatar
李宗熹 committed
328
    # df.to_sql("fund_rank", con, if_exists='replace')
李宗熹's avatar
李宗熹 committed
329
    # con.close()