Example #1
0
def _refresh_idx_betas(x):
    _d_comb = x
    comb_prices = _d_comb.retrieve("Close")
    tc = comb_prices.tick_cols()

    spy_prices = d_pr_cl.retrieve("SPY")
    for j in range(60, 300, 60):
        # j = 60
        comb_spy_beta = comb_prices.copy()
        comb_spy_beta = cruf.DataFrame.merge(comb_spy_beta, spy_prices[["Date", "Close"]], how="left", on="Date")
        comb_spy_beta.sort("Date")
        comb_spy_beta["Close"] = crtf.fill(comb_spy_beta["Close"].values)
        comb_spy_price = comb_spy_beta["Close"].values
        del comb_spy_beta["Close"]
        for i in tc:
            comb_spy_beta[i] = crtf.beta_cc(comb_spy_beta[i].values, comb_spy_price, j)
        _d_comb.store("SPY_Beta_" + str(int(j / 20)) + "m", comb_spy_beta)

    mdy_prices = d_pr_cl.retrieve("MDY")
    for j in range(60, 300, 60):
        # j = 60
        comb_mdy_beta = comb_prices.copy()
        comb_mdy_beta = cruf.DataFrame.merge(comb_mdy_beta, mdy_prices[["Date", "Close"]], how="left", on="Date")
        comb_mdy_beta.sort("Date")
        comb_mdy_beta["Close"] = crtf.fill(comb_mdy_beta["Close"].values)
        comb_mpy_price = comb_mdy_beta["Close"].values
        del comb_mdy_beta["Close"]
        for i in tc:
            comb_mdy_beta[i] = crtf.beta_cc(comb_mdy_beta[i].values, comb_mpy_price, j)
        _d_comb.store("MDY_Beta_" + str(int(j / 20)) + "m", comb_mdy_beta)
    return None
Example #2
0
def _refresh_mom_betas(x):
    _d_comb = x
    comb_prices = _d_comb.retrieve("Close")
    tc = comb_prices.tick_cols()
    comb_ret = _d_comb.retrieve("Returns")[tc].values.astype("float64")
    comb_ret[np.isnan(comb_ret)] = 0
    comb_vol = _d_comb.retrieve("Vol_CC_240D")

    # get the 12-1 momentum
    comb_mom = comb_prices.copy()
    for i in tc:
        comb_mom[i] = (
            crtf.lrma(crtf.ret(comb_mom[i].values, 250) - crtf.ret(comb_mom[i].values, 21), 20) / comb_vol[i].values
        )
    comb_mom_val = comb_mom[tc].values.astype("float64")
    comb_mom_val_st = np.sum(~np.isnan(comb_mom_val), axis=1)
    comb_mom_idx = np.where(comb_mom_val_st > 30)[0][0]
    dummy = np.empty(len(tc)) * np.nan
    for j in range(0, comb_mom_idx):
        comb_mom_val[j, :] = dummy
    for j in range(comb_mom_idx, comb_mom_val.shape[0]):
        comb_mom_val[j, :] = crup.full_norm_rankit(comb_mom_val[j, :])
    comb_mom[:, tc] = comb_mom_val
    comb_mom_val[np.isnan(comb_mom_val)] = 0

    # calculate the momentum factor returns
    comb_mom["Close"] = 2 * np.sum(comb_ret * comb_mom_val, axis=1) / np.sum(np.abs(comb_mom_val), axis=1)
    comb_mom = comb_mom[["Date", "Close"]]
    comb_mom["Close"] = crup.conv_to_price(comb_mom["Close"].values)

    # calculate the momentum beta
    comb_mom_val = comb_mom["Close"].values
    for j in range(60, 300, 60):
        comb_mom_beta = comb_prices.copy()
        for i in tc:
            comb_mom_beta[i] = crtf.beta_cc(comb_mom_beta[i].values, comb_mom_val, j)
        _d_comb.store("MOM_Beta_" + str(int(j / 20)) + "m", comb_mom_beta)
    return None