Esempio n. 1
0
def _refresh_lvol_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 30-day volatility
    comb_lowv = comb_prices.copy()
    for i in tc:
        comb_lowv[i] = crtf.lrma(crtf.vol_ewma(comb_prices[i].values, 22), 20) / comb_vol[i].values
    comb_lowv_val = comb_lowv[tc].values.astype("float64")
    comb_lowv_val_st = np.sum(~np.isnan(comb_lowv_val), axis=1)
    comb_lowv_idx = np.where(comb_lowv_val_st > 30)[0][0]
    dummy = np.empty(len(tc)) * np.nan
    for j in range(0, comb_lowv_idx):
        comb_lowv_val[j, :] = dummy
    for j in range(comb_lowv_idx, comb_lowv_val.shape[0]):
        comb_lowv_val[j, :] = -crup.full_norm_rankit(comb_lowv_val[j, :])
    comb_lowv[:, tc] = comb_lowv_val
    comb_lowv_val[np.isnan(comb_lowv_val)] = 0

    # calculate the low volatility factor returns
    comb_lowv["Close"] = 2 * np.sum(comb_ret * comb_lowv_val, axis=1) / np.sum(np.abs(comb_lowv_val), axis=1)
    comb_lowv = comb_lowv[["Date", "Close"]]
    comb_lowv["Close"] = crup.conv_to_price(comb_lowv["Close"].values)

    # calculate the low volatility beta
    comb_lowv_val = comb_lowv["Close"].values
    for j in range(60, 300, 60):
        comb_lowv_beta = comb_prices.copy()
        for i in tc:
            comb_lowv_beta[i] = crtf.beta_cc(comb_lowv_beta[i].values, comb_lowv_val, j)
        _d_comb.store("LVOL_Beta_" + str(int(j / 20)) + "m", comb_lowv_beta)
    return None
Esempio n. 2
0
    cnt1gk = 0
    cnt1yz = 0

    # prod_ = 1
    prod_ = 1.33456825

    for i in test_.tick_cols():
        # i = test_.tick_cols()[0]
        test = test_[i].values
        testo = testo_[i].values
        testh = testh_[i].values
        testl = testl_[i].values
        testr = crtf.ret(test)

        test1cc = crtf.vol_cc(test, n, zl=True)
        test1tr = crtf.vol_ewma(test, n)
        test1p = crtf.vol_p(testo, testh, testl, test, n, zl=True)
        test1gk3 = crtf.vol_gk3(testo, test, n, zl=True)
        test1pb = crtf.vol_pb(testo, testh, testl, test, n, zl=True)
        test1rs = crtf.vol_rs(testo, testh, testl, test, n, zl=True)
        test1gk = crtf.vol_gk(testo, testh, testl, test, n, zl=True)
        test1yz = crtf.vol_yz(testo, testh, testl, test, n, zl=True)
        tmp = chk_vol(testr, test1cc*prod_)
        if ~np.isnan(tmp):
            res1cc += tmp
            cnt1cc += 1
        tmp = chk_vol(testr, test1tr)
        if ~np.isnan(tmp):
            res1tr += tmp
            cnt1tr += 1
        tmp = chk_vol(testr, test1p*prod_)