Пример #1
0
def calculate():
    df_whole = pd.DataFrame()
    conn = engine_read.connect()

    year = process_date.year
    month = process_date.month

    month_range = cld.monthrange(year, month)[1]
    time_to_fill = sf.Time(dt.datetime(year, month, month_range))
    # year, month = time_to_fill.year, time_to_fill.month
    # month_range = time_to_fill.month_range

    sql_bm = sf.SQL.market_index(time_to_fill.today)  # Get benchmark prices
    sql_pe = sf.SQL.pe_index(time_to_fill.today, freq="m")  ###w->m

    bm = pd.read_sql(sql_bm, engine_read)
    bm["y1_treasury_rate"] = bm["y1_treasury_rate"].fillna(method="backfill")
    bm["y1_treasury_rate"] = bm["y1_treasury_rate"].apply(su.annually2monthly)
    bm["statistic_date"] = bm["statistic_date"].apply(su.date2tstp)
    pe = pd.read_sql(sql_pe, engine_read)
    pe["statistic_date"] = pe["statistic_date"].apply(su.date2tstp)
    conn.close()

    prices_bm = [
        bm["hs300"].tolist(), bm["csi500"].tolist(), bm["sse50"].tolist(),
        bm["cbi"].tolist(), bm["nfi"]
    ]
    price_pe = pe["index_value"].tolist()
    r_tbond = bm["y1_treasury_rate"].tolist()
    t_bm = bm["statistic_date"].tolist()
    t_pe = pe["statistic_date"].tolist()

    intervals = table.intervals
    intervals5 = [1, 2, 3, 4, 5, 6, 10, 11]
    intervals6 = [2, 3, 4, 5, 6, 10, 11]

    result = []

    conn = engine_read.connect()

    # Get Data
    date_s = sf.Time(process_date -
                     dt.timedelta(process_date.day))  # Generate statistic_date

    sql_fids_updated = sf.SQL.ids_updated_sd(date_s.today, "om")
    ids_updated = tuple(
        x[0]
        for x in conn.execute(sql_fids_updated).fetchall())  # 找到当月净值有更新的基金

    sql_o_updated = "SELECT DISTINCT fom.org_id FROM fund_org_mapping fom \
             JOIN org_info oi ON fom.org_id = oi.org_id \
             WHERE org_type_code = 1 AND oi.found_date <= '{0}'  AND fund_id IN {1}".format(
        date_s.today - relativedelta(months=3),
        ids_updated)  # 根据净值更新的基金确定需要计算的投顾
    o_updated = tuple(x[0] for x in conn.execute(sql_o_updated).fetchall())

    sql_fom = "SELECT fom.org_id, fom.fund_id, oi.found_date, oi.org_name FROM fund_org_mapping fom \
               JOIN org_info oi ON fom.org_id = oi.org_id \
               JOIN fund_info fi ON fom.fund_id = fi.fund_id \
               WHERE fom.org_id IN {0} AND fom.org_type_code = 1 AND oi.found_date <= '{1}' AND fi.foundation_date <= '{2}'".format(
        o_updated, date_s.today - relativedelta(months=3),
        date_s.today - relativedelta(months=1))
    fom = pd.read_sql(sql_fom, conn)  # 根据需要计算的投顾找到其旗下管理的所有基金

    fid_used = tuple(fom["fund_id"])
    sql_fnd = sf.SQL.nav(fid_used)
    fnd = pd.read_sql(sql_fnd, conn)
    fnd = fnd.dropna()
    fnd.index = range(len(fnd))

    data = fom.merge(fnd, how="inner", on="fund_id")
    data = data.sort_values(by=["org_id", "fund_id", "statistic_date"],
                            ascending=[True, True, False])
    t_mins = data.groupby(["org_id"])["statistic_date"].min().tolist()
    t_mins_tstp = [time.mktime(x.timetuple()) for x in t_mins]
    data["statistic_date"] = data["statistic_date"].apply(
        lambda x: time.mktime(x.timetuple()))
    data.index = range(len(data))

    ids_o = data["org_id"].drop_duplicates().tolist()
    names_o = data.drop_duplicates(subset=["org_id"])["org_name"].tolist()
    idx4slice_o = su.idx4slice(data, "org_id")
    dfs = [
        data[idx4slice_o[i]:idx4slice_o[i + 1]]
        if i != len(idx4slice_o) - 1 else data[idx4slice_o[i]:]
        for i in range(len(idx4slice_o) - 1)
    ]

    # Proprocess
    # 标准序列
    t_stds = [
        tu.timeseries_std(date_s.today,
                          interval,
                          periods_y=12,
                          use_lastday=True,
                          extend=1) for interval in intervals
    ]
    t_std_y5 = t_stds[6]
    t_stds_len = [len(x) - 1 for x in t_stds]

    # 基金标准序列_成立以来
    t_std_alls = [
        tu.timeseries_std(date_s.today,
                          tu.periods_in_interval(date_s.today, t_min, 12),
                          periods_y=12,
                          use_lastday=True,
                          extend=6) for t_min in t_mins
    ]  # 标准序列_成立以来
    t_std_alls = [
        t_std_all[:len([x for x in t_std_all if x >= t_min]) + 1]
        for t_std_all, t_min in zip(t_std_alls, t_mins_tstp)
    ]

    # 基准指数的标准序列_成立以来
    matchs_bm = [
        tu.outer_match4indicator_m(t_bm, t_std_all, False)
        for t_std_all in t_std_alls
    ]
    idx_matchs_bm = [x[1] for x in matchs_bm]
    price_bm0_all = [[
        prices_bm[0][ix] if ix is not None else None for ix in idx.values()
    ] for idx in idx_matchs_bm]
    price_bm1_all = [[
        prices_bm[1][ix] if ix is not None else None for ix in idx.values()
    ] for idx in idx_matchs_bm]
    price_bm2_all = [[
        prices_bm[2][ix] if ix is not None else None for ix in idx.values()
    ] for idx in idx_matchs_bm]
    price_bm3_all = [[
        prices_bm[3][ix] if ix is not None else None for ix in idx.values()
    ] for idx in idx_matchs_bm]
    price_bm4_all = [[
        prices_bm[4][ix] if ix is not None else None for ix in idx.values()
    ] for idx in idx_matchs_bm]

    matchs_pe = [
        tu.outer_match4indicator_m(t_pe, t_std_all, False)
        for t_std_all in t_std_alls
    ]
    idx_matchs_pe = [x[1] for x in matchs_pe]
    price_pe_all = [[
        price_pe[ix] if ix is not None else None for ix in idx.values()
    ] for idx in idx_matchs_pe]

    # 基准指标的收益率_成立以来
    r_bm0_all = [fi.gen_return_series(x) for x in price_bm0_all]
    r_bm1_all = [fi.gen_return_series(x) for x in price_bm1_all]
    r_bm2_all = [fi.gen_return_series(x) for x in price_bm2_all]
    r_bm3_all = [fi.gen_return_series(x) for x in price_bm3_all]
    r_bm4_all = [fi.gen_return_series(x) for x in price_bm4_all]

    r_pe_all = [fi.gen_return_series(x) for x in price_pe_all]

    tmp = [len(idx_matchs_bm[i]) for i in range(len(idx_matchs_bm))]
    tmp_id = tmp.index(max(tmp))
    tmp_list = [
        r_tbond[ix] if ix is not None else None
        for ix in idx_matchs_bm[tmp_id].values()
    ]
    tmp = pd.DataFrame(tmp_list)[0].fillna(method="backfill").tolist()

    r_f_all = [[
        r_tbond[idx[k]] if idx[k] is not None else tmp[k] for k in idx.keys()
    ] for idx in idx_matchs_bm]
    r_f_all = [x[1:] for x in r_f_all]

    # 基准指标的收益率_不同频率
    matchs_bm = tu.outer_match4indicator_m(t_bm, t_std_y5,
                                           False)  # 基准指数标准序列_成立以来
    matchs_pe = tu.outer_match4indicator_m(t_pe, t_std_y5, False)
    idx_matchs_bm = matchs_bm[1]
    idx_matchs_pe = matchs_pe[1]
    price_bm0_y5 = [
        prices_bm[0][ix] if ix is not None else None
        for ix in idx_matchs_bm.values()
    ]
    price_bm1_y5 = [
        prices_bm[1][ix] if ix is not None else None
        for ix in idx_matchs_bm.values()
    ]
    price_bm2_y5 = [
        prices_bm[2][ix] if ix is not None else None
        for ix in idx_matchs_bm.values()
    ]
    price_bm3_y5 = [
        prices_bm[3][ix] if ix is not None else None
        for ix in idx_matchs_bm.values()
    ]
    price_bm4_y5 = [
        prices_bm[4][ix] if ix is not None else None
        for ix in idx_matchs_bm.values()
    ]

    price_pe_y5 = [
        price_pe[ix] if ix is not None else None
        for ix in idx_matchs_pe.values()
    ]

    # 基准指标的收益率_不同频率
    r_bm0_y5 = fi.gen_return_series(price_bm0_y5)
    r_bm1_y5 = fi.gen_return_series(price_bm1_y5)
    r_bm2_y5 = fi.gen_return_series(price_bm2_y5)
    r_bm3_y5 = fi.gen_return_series(price_bm3_y5)
    r_bm4_y5 = fi.gen_return_series(price_bm4_y5)
    r_pe_y5 = fi.gen_return_series(price_pe_y5)

    r_f_y5 = [
        r_tbond[ix] if ix is not None else None
        for ix in idx_matchs_bm.values()
    ]
    r_f_y5 = r_f_y5[1:]

    rs_bm0 = [r_bm0_y5[:length - 1] for length in t_stds_len]
    rs_bm1 = [r_bm1_y5[:length - 1] for length in t_stds_len]
    rs_bm2 = [r_bm2_y5[:length - 1] for length in t_stds_len]
    rs_bm3 = [r_bm3_y5[:length - 1] for length in t_stds_len]
    rs_bm4 = [r_bm4_y5[:length - 1] for length in t_stds_len]

    rs_pe = [r_pe_y5[:length - 1] for length in t_stds_len]
    rs_f = [r_f_y5[:length - 1] for length in t_stds_len]

    benchmark = {
        1: rs_bm0,
        2: rs_bm1,
        3: rs_bm2,
        4: rs_pe,
        6: rs_bm3,
        7: rs_bm4
    }
    benchmark_all = {
        1: r_bm0_all,
        2: r_bm1_all,
        3: r_bm2_all,
        4: r_pe_all,
        6: r_bm3_all,
        7: r_bm4_all
    }

    for i in range(len(ids_o)):
        df = dfs[i]
        df.index = range(len(df))
        idx4slice = su.idx4slice(df, "fund_id")
        navs = su.slice(df, idx4slice, "nav")
        t_reals = su.slice(df, idx4slice, "statistic_date")

        matchs_all = [
            tu.outer_match4indicator_m(t_real, t_std_alls[i], drop_none=False)
            for t_real in t_reals
        ]
        idx_matchs_all = [x[1] for x in matchs_all]
        nav_matchs_all = [[
            nav[ix] if ix is not None else np.NaN for ix in idx.values()
        ] for nav, idx in zip(navs, idx_matchs_all)]

        nv_matrix = np.array(nav_matchs_all).T
        r_total = np.nanmean((nv_matrix[:-1] / nv_matrix[1:] - 1), axis=1)
        price_total = np.nancumprod(1 + r_total[::-1])[::-1].tolist()
        price_total.append(1)  # 定义基期伪价格为1
        r_total = fi.gen_return_series(price_total)

        prices = []
        for j in range(7):
            if t_mins[i] + relativedelta(months=intervals[j]) <= date_s.today:
                length = min(len(price_total), t_stds_len[j])
                prices.append(price_total[:length])
            else:
                prices.append(None)

        for j in range(7, 11):
            length = min(len(price_total), t_stds_len[j])
            prices.append(price_total[:length])

        prices.append(price_total)
        navs2 = [prices[i] for i in intervals5]
        navs3 = [prices[i] for i in intervals6]
        rs2 = [fi.gen_return_series(x) for x in navs2]
        rs3 = [fi.gen_return_series(x) for x in navs3]

        rs_f_ = rs_f.copy()
        rs_f_.append(r_f_all[i])
        rs_f2_ = [rs_f_[i] for i in intervals5]
        rs_f3_ = [rs_f_[i] for i in intervals6]

        for k in benchmark.keys():
            rs_bm_ = benchmark[k].copy()  # 指定benchmark
            rs_bm_.append(benchmark_all[k][i])
            rs_bm2 = [rs_bm_[i] for i in intervals5]
            rs_bm3 = [rs_bm_[i] for i in intervals6]

            s_time = [
                fi.competency_timing(r, r_bm, r_f)
                for r, r_bm, r_f in zip(rs3, rs_bm3, rs_f3_)
            ]
            s_security = [
                fi.competency_stock(r, r_bm, r_f)
                for r, r_bm, r_f in zip(rs3, rs_bm3, rs_f3_)
            ]
            persistence = [
                fi.persistence_er(r, r_bm) for r, r_bm in zip(rs2, rs_bm2)
            ]
            odds = [fi.odds(r, r_bm) for r, r_bm in zip(rs2, rs_bm2)]

            tmp = [odds, persistence, s_time, s_security]
            result_i = [
                ids_o[i], names_o[i], k, 1, 1, nv_matrix.shape[1], 60001,
                "全产品", 6000101, "全产品", date_s.today
            ]
            for x in tmp:
                result_i.extend(x)
            result.append(result_i)

    df = pd.DataFrame(result)
    df[list(range(11, 41))] = df[list(range(11, 41))].astype(np.float64)
    df[list(range(11, 41))] = df[list(range(11,
                                            41))].apply(lambda x: round(x, 6))
    df.columns = columns
    df_whole = df_whole.append(df)

    return df_whole
Пример #2
0
engines = cfg.load_engine()
engine_rd = engines["2Gb"]
engine_wt = engines["2Gb"]

df_whole = pd.DataFrame()


for year in range(2017, 2018):
    for month in range(2, 3):
        conn = engine_rd.connect()

        now = dt.datetime.now()

        year, month = year, month
        month_range = cld.monthrange(year, month)[1]
        time_to_fill = sf.Time(dt.datetime(year, month, month_range))
        year, month = time_to_fill.year, time_to_fill.month
        month_range = time_to_fill.month_range

        sql_bm = sf.SQL.market_index(time_to_fill.today)  # Get benchmark prices
        sql_pe = sf.SQL.pe_index(time_to_fill.today, freq="m")  ###w->m

        bm = pd.read_sql(sql_bm, engine_rd)
        bm["y1_treasury_rate"] = bm["y1_treasury_rate"].fillna(method="backfill")
        bm["y1_treasury_rate"] = bm["y1_treasury_rate"].apply(su.annually2monthly)
        bm["statistic_date"] = bm["statistic_date"].apply(su.date2tstp)
        pe = pd.read_sql(sql_pe, engine_rd)
        pe["statistic_date"] = pe["statistic_date"].apply(su.date2tstp)
        conn.close()

        prices_bm = [bm["hs300"].tolist(), bm["csi500"].tolist(), bm["sse50"].tolist(), bm["cbi"].tolist(), bm["nfi"]]
Пример #3
0
def calculate():
    conn = engine_rd.connect()

    year, month = yesterday.year, yesterday.month
    month_range = cld.monthrange(year, month)[1]
    time_to_fill = sf.Time(dt.datetime(year, month, month_range))
    year, month = time_to_fill.year, time_to_fill.month

    bms_used = [
        "hs300", "csi500", "sse50", "ssia", "cbi", "y1_treasury_rate", "nfi"
    ]
    sql_bm = sf.SQL.market_index(date=time_to_fill.today,
                                 benchmarks=bms_used,
                                 whole=True)  # Get benchmark prices
    bm = pd.read_sql(sql_bm, conn)
    # bm.loc[bm["statistic_date"] == dt.date(1995, 8, 16), "y1_treasury_rate"] = 2.35

    bm["y1_treasury_rate"] = bm["y1_treasury_rate"].fillna(method="backfill")
    bm["y1_treasury_rate"] = bm["y1_treasury_rate"].apply(su.annually2weekly)
    bm["statistic_date"] = bm["statistic_date"].apply(su.date2tstp)

    prices_bm = [
        bm.dropna(subset=[bm_name])[bm_name].tolist() for bm_name in bms_used
    ]
    ts_bm = [
        bm.dropna(subset=[bm_name])["statistic_date"].tolist()
        for bm_name in bms_used
    ]

    prices = prices_bm.copy()
    ts = ts_bm.copy()

    t_mins_pe_all = sf.PEIndex().firstmonday  # 寻找指数中可被计算的
    pesid_used = []
    for k in t_mins_pe_all:
        if t_mins_pe_all[k].year < year:
            pesid_used.append(k)
        elif t_mins_pe_all[k].year == year:
            if t_mins_pe_all[k].month <= month:
                pesid_used.append(k)
            else:
                continue
        else:
            continue

    prices_pe = []
    ts_pe = []
    pes_used = []
    for idx in pesid_used:
        PE = sf.PEIndex(idx)
        pes_used.append(PE.id)
        sql_pe = sf.SQL.pe_index(time_to_fill.today, index_id=PE.id, freq="w")
        pe = pd.read_sql(sql_pe, conn)
        pe["statistic_date"] = pe["statistic_date"].apply(su.date2tstp)
        prices_pe.append(pe["index_value"].tolist())
        ts_pe.append(pe["statistic_date"].tolist())
    conn.close()

    prices.extend(prices_pe)
    ts.extend(ts_pe)

    t_mins_tstp = [min(x) for x in ts]
    t_mins = tu.tr(t_mins_tstp)

    intervals = table.intervals
    intervals1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
    intervals3 = [0, 1, 2, 3, 4, 5, 6, 10, 11]

    index_used = bms_used.copy()
    index_used.extend(pes_used)

    index_name = {
        "FI01": "私募全市场指数",
        "FI02": "阳光私募指数",
        "FI03": "私募FOF指数",
        "FI04": "股票多头策略私募指数",
        "FI05": "股票多空策略私募指数",
        "FI06": "市场中性策略私募指数",
        "FI07": "债券基金私募指数",
        "FI08": "管理期货策略私募指数",
        "FI09": "宏观策略私募指数",
        "FI10": "事件驱动策略私募指数",
        "FI11": "相对价值策略私募指数",
        "FI12": "多策略私募指数",
        "FI13": "组合投资策略私募指数",
        "hs300": "沪深300指数",
        "csi500": "中证500指数",
        "sse50": "上证50指数",
        "ssia": "上证A股指数",
        "cbi": "中债指数",
        "nfi": "南华商品指数",
        "y1_treasury_rate": "y1_treasury_rate"
    }

    result = []
    for mday in range(1, yesterday.day + 1):
        print("Day {0}: {1}".format(mday, dt.datetime.now()))

        date_s = sf.Time(dt.datetime(year, month,
                                     mday))  # Generate statistic_date

        #
        t_stds = [
            tu.timeseries_std(date_s.today, interval, 52, extend=1)
            for interval in intervals
        ]  # 标准序列
        t_std_lens = [len(x) - 1 for x in t_stds]  # 标准序列净值样本个数
        t_std_week = tu.timeseries_std(date_s.today, "w", 52, 1)  # 标准序列_本周
        ts_std_total = [
            tu.timeseries_std(date_s.today,
                              tu.periods_in_interval(date_s.today, t_min, 12),
                              extend=4) for t_min in t_mins
        ]  # 标准序列_成立以来
        ts_std_total = [
            t_std_total[:len([x for x in t_std_total if x >= t_min]) + 1]
            for t_std_total, t_min in zip(ts_std_total, t_mins_tstp)
        ]

        # 基准指数的标准序列_成立以来
        matchs = [
            tu.outer_match4indicator_w(t, t_std_all, False)
            for t, t_std_all in zip(ts, ts_std_total)
        ]
        idx_matchs = [x[1] for x in matchs]
        prices_total = [[
            price[ix] if ix is not None else None for ix in idx.values()
        ] for price, idx in zip(prices, idx_matchs)]

        # 基准指标的收益率_不同频率
        rs_total = [
            fi.gen_return_series(price_total) for price_total in prices_total
        ]

        # 无风险国债的收益率
        r_f_total = prices_total[5][
            1:]  # the list `y1_treasury_rate` in prices_total is not price, but return
        r_f_total = pd.DataFrame(r_f_total).fillna(
            method="backfill")[0].tolist()
        r_f_all = [r_f_total[:length - 1] for length in t_std_lens]
        r_f_all.append(r_f_total)

        # for i in range(len(index_used)):
        for i in range(len(index_used)):

            if index_name[index_used[i]] == "y1_treasury_rate": continue

            price_all = []
            r_all = []
            for j in range(7):
                if dt.date.fromtimestamp(
                    (t_mins[i] + relativedelta(months=intervals[j])
                     ).timestamp()) <= date_s.today:
                    price_all.append(prices_total[i][:t_std_lens[j]])
                    r_all.append(rs_total[i][:t_std_lens[j] - 1])
                else:
                    price_all.append([])
                    r_all.append([])
            for j in range(7, 11):
                price_all.append(prices_total[i][:t_std_lens[j]])
                if rs_total[i] is not None:
                    r_all.append(rs_total[i][:t_std_lens[j] - 1])
                else:
                    r_all.append([])

            price_all.append(prices_total[i])
            r_all.append(rs_total[i])
            price_all1 = [price_all[i] for i in intervals1]
            price_all3 = [price_all[i] for i in intervals3]
            r_all1 = [r_all[i] for i in intervals1]
            r_all3 = [r_all[i] for i in intervals3]

            r_f_all1 = [r_f_all[i] for i in intervals1][:-1]
            r_f_all3 = [r_f_all[i] for i in intervals3][:-1]
            r_f_all1.append(r_f_all[-1][:len(r_all[-1])])
            r_f_all3.append(r_f_all[-1][:len(r_all[-1])])

            ir = [fi.accumulative_return(price) for price in price_all1]
            ir_a = [fi.return_a(r) for r in r_all1]
            stdev_a = [fi.standard_deviation_a(r) for r in r_all3]
            dd_a = [
                fi.downside_deviation_a(r, r_f)
                for r, r_f in zip(r_all3, r_f_all3)
            ]
            mdd = [fi.max_drawdown(price)[0] for price in price_all3]
            sharpe_a = [
                fi.sharpe_a(r, r_f) for r, r_f in zip(r_all3, r_f_all3)
            ]
            calmar_a = [
                fi.calmar_a(price, r_f)
                for price, r_f in zip(price_all3, r_f_all3)
            ]
            sortino_a = [
                fi.sortino_a(r, r_f) for r, r_f in zip(r_all3, r_f_all3)
            ]
            p_earning_weeks = [fi.periods_positive_return(r) for r in r_all3]
            n_earning_weeks = [fi.periods_npositive_return(r) for r in r_all3]
            con_rise_weeks = [fi.periods_continuous_rise(r)[0] for r in r_all3]
            con_fall_weeks = [fi.periods_continuous_fall(r)[0] for r in r_all3]

            tmp = [
                ir, ir_a, stdev_a, dd_a, mdd, sharpe_a, calmar_a, sortino_a,
                p_earning_weeks, n_earning_weeks, con_rise_weeks,
                con_fall_weeks
            ]
            result_i = [index_used[i], index_name[index_used[i]], date_s.today]
            for x in tmp:
                result_i.extend(x)
            result.append(result_i)

    df = pd.DataFrame(result)
    df[list(range(3, 117))] = df[list(range(3, 117))].astype(np.float64)
    df[list(range(3, 117))] = df[list(range(3,
                                            117))].apply(lambda x: round(x, 6))
    df.columns = columns

    df.index_id = df.index_id.apply(lambda x: x.upper())
    return df