Ejemplo n.º 1
0
def display_monthly_ret():
    ret = crsf.cr_un.retrieve('FinalPort_Returns')
    ret['Price'] = np.cumprod(1+ret['Returns'].values)
    x = ret['Date', list]
    x = [(i.year*100+i.month) for i in x]
    x_ = x.copy()
    len(x[0:-1])
    len(x_[1:])
    loc_ = [i for i, j in enumerate(zip(x[0:-1], x_[1:])) if j[0] != j[1]]
    loc_.append(ret.shape[0]-1)

    ret = ret[loc_, :]
    del ret['Returns']
    ret['Returns'] = crtf.ret(ret['Price'].values)
    ret = ret[1:, :]
    del ret['Price']
    crsf.cr_un.store('FinalPort_MonthlyReturns', ret)
    ret.show_all()
Ejemplo n.º 2
0
def _refresh_stats(x):
    _d_comb, _d_sig = x
    comb_prices = _d_comb.retrieve('Close')
    spy_prices = d_pr_cl.retrieve('SPY')

    # store the leverage
    comb_vol = comb_prices.copy()
    for i in comb_vol.tick_cols():
        comb_vol[i] = crtf.vol_cc(comb_prices[i].values, 30)   # changed it back
    comb_lev = comb_vol.copy()
    for i in comb_lev.tick_cols():
        comb_lev[i] = crtf.lrma(0.01/comb_vol[i].values, 30, lg=True)
    _d_comb.store('Lev_CC_30D', comb_lev)
    for i in comb_vol.tick_cols():
        comb_vol[i] = crtf.lrma(comb_vol[i].values, 30, lg=True)
    _d_comb.store('Vol_CC_30D', comb_vol)

    comb_vol = comb_prices.copy()
    for i in comb_vol.tick_cols():
        comb_vol[i] = crtf.vol_cc(comb_prices[i].values, 60)   # changed it back
    comb_lev = comb_vol.copy()
    for i in comb_lev.tick_cols():
        comb_lev[i] = crtf.lrma(0.01/comb_vol[i].values, 30, lg=True)
    _d_comb.store('Lev_CC_60D', comb_lev)
    for i in comb_vol.tick_cols():
        comb_vol[i] = crtf.lrma(comb_vol[i].values, 30, lg=True)
    _d_comb.store('Vol_CC_60D', comb_vol)

    comb_vol = comb_prices.copy()
    for i in comb_vol.tick_cols():
        comb_vol[i] = crtf.vol_cc(comb_prices[i].values, 120)   # changed it back
    comb_lev = comb_vol.copy()
    for i in comb_lev.tick_cols():
        comb_lev[i] = crtf.lrma(0.01/comb_vol[i].values, 30, lg=True)
    _d_comb.store('Lev_CC_120D', comb_lev)
    for i in comb_vol.tick_cols():
        comb_vol[i] = crtf.lrma(comb_vol[i].values, 30, lg=True)
    _d_comb.store('Vol_CC_120D', comb_vol)

    comb_vol = comb_prices.copy()
    for i in comb_vol.tick_cols():
        comb_vol[i] = crtf.vol_cc(comb_prices[i].values, 240)   # changed it back
    comb_lev = comb_vol.copy()
    for i in comb_lev.tick_cols():
        comb_lev[i] = crtf.lrma(0.01/comb_vol[i].values, 30, lg=True)
    _d_comb.store('Lev_CC_240D', comb_lev)
    for i in comb_vol.tick_cols():
        comb_vol[i] = crtf.lrma(comb_vol[i].values, 30, lg=True)
    _d_comb.store('Vol_CC_240D', comb_vol)

    # generate the signals
    for j in range(1, 51):
        comb_ret_qr = comb_prices.copy()
        comb_ret_lr = comb_prices.copy()
        for i in comb_ret_lr.tick_cols():
            comb_ret_qr[i] = crtf.qrbeta(comb_ret_qr[i].values, j*10)
            comb_ret_lr[i] = crtf.lrbeta(comb_ret_lr[i].values, j*10)
        _d_sig.store('QrB_'+str(j*10)+'D', comb_ret_qr)
        _d_sig.store('LrB_'+str(j*10)+'D', comb_ret_lr)
        del comb_ret_lr
        del comb_ret_qr

    # generate 12-1 momentum, its score and factor returns
    comb_mom = comb_prices.copy()
    for i in comb_mom.tick_cols():
        comb_mom[i] = crtf.lrma(crtf.ret(comb_mom[i].values, 250)-crtf.ret(comb_mom[i].values, 21), 20)
    _d_comb.store('MOM_12m1v', comb_mom)
    comb_mom1 = comb_mom.copy()
    comb_mom1_tc = comb_mom.tick_cols()
    comb_mom1_val = comb_mom[comb_mom1_tc].values
    comb_mom1_val_st = np.sum(~np.isnan(comb_mom1_val), axis=1)
    comb_mom1_idx = np.where(comb_mom1_val_st>30)[0][0]
    dummy = np.empty(len(comb_mom1_tc)) * np.nan
    for j in range(0, comb_mom1_idx):
        comb_mom1_val[j, :] = dummy
    for j in range(comb_mom1_idx, comb_mom1_val.shape[0]):
        comb_mom1_val[j, :] = crup.full_norm_rankit(comb_mom1_val[j, :])
    comb_mom1[:, comb_mom1_tc] = comb_mom1_val
    _d_comb.store('MOM_12m1s', comb_mom1)
    comb_mom1['Close'] = np.nan
    comb_ret = _d_comb.retrieve('Returns')[comb_mom1_tc].values
    comb_ret[np.isnan(comb_ret)] = 0
    comb_mom1_val[np.isnan(comb_mom1_val)] = 0
    comb_mom1['Close'] = 2*np.sum(comb_ret * comb_mom1_val, axis=1)/np.sum(abs(comb_mom1_val), axis=1)
    comb_mom1 = comb_mom1[['Date', 'Close']]
    comb_mom1['Close'] = crup.conv_to_price(comb_mom1['Close'].values)
    _d_comb.store('MOMf', comb_mom1)

    # calculate mom_beta
    comb_mom_val = comb_mom1['Close'].values
    comb_mom_beta = comb_prices.copy()
    for i in comb_mom_beta.tick_cols():
        comb_mom_beta[i] = crtf.beta_cc(comb_mom_beta[i].values, comb_mom_val, 120)
    _d_comb.store('MOM_Beta_6m', comb_mom_beta)
    comb_mom_beta = comb_prices.copy()
    for i in comb_mom_beta.tick_cols():
        comb_mom_beta[i] = crtf.beta_cc(comb_mom_beta[i].values, comb_mom_val, 60)
    _d_comb.store('MOM_Beta_3m', comb_mom_beta)
    comb_mom_beta = comb_prices.copy()
    for i in comb_mom_beta.tick_cols():
        comb_mom_beta[i] = crtf.beta_cc(comb_mom_beta[i].values, comb_mom_val, 180)
    _d_comb.store('MOM_Beta_9m', comb_mom_beta)
    comb_mom_beta = comb_prices.copy()
    for i in comb_mom_beta.tick_cols():
        comb_mom_beta[i] = crtf.beta_cc(comb_mom_beta[i].values, comb_mom_val, 240)
    _d_comb.store('MOM_Beta_12m', comb_mom_beta)

    # calculate low_vol_score
    comb_lowv = comb_prices.copy()
    for i in comb_lowv.tick_cols():
        comb_lowv[i] = crtf.lrma(crtf.vol_cc(comb_prices[i].values, 32, zl=False), 20)
    _d_comb.store('LVOL_32dv', comb_lowv)
    comb_lowv1 = comb_lowv.copy()
    comb_lowv1_tc = comb_lowv.tick_cols()
    comb_lowv1_val = comb_lowv[comb_lowv1_tc].values
    comb_lowv1_val_st = np.sum(~np.isnan(comb_lowv1_val), axis=1)
    comb_lowv1_idx = np.where(comb_lowv1_val_st>30)[0][0]
    dummy = np.empty(len(comb_lowv1_tc)) * np.nan
    for j in range(0, comb_lowv1_idx):
        comb_lowv1_val[j, :] = dummy
    for j in range(comb_lowv1_idx, comb_lowv1_val.shape[0]):
        comb_lowv1_val[j, :] = crup.full_norm_rankit(comb_lowv1_val[j, :])
    comb_lowv1[:, comb_lowv1_tc] = comb_lowv1_val
    _d_comb.store('LVOL_32ds', comb_lowv1)
    comb_lowv1['Close'] = np.nan
    comb_ret = _d_comb.retrieve('Returns')[comb_lowv1_tc].values
    comb_ret[np.isnan(comb_ret)] = 0
    comb_lowv1_val[np.isnan(comb_lowv1_val)] = 0
    comb_lowv1['Close'] = 2*np.sum(comb_ret * comb_lowv1_val, axis=1)/np.sum(abs(comb_lowv1_val), axis=1)
    comb_lowv1 = comb_lowv1[['Date', 'Close']]
    comb_lowv1['Close'] = crup.conv_to_price(comb_lowv1['Close'].values)
    _d_comb.store('LVOLf', comb_lowv1)

    # calculate lvol_beta
    comb_lowv_val = comb_lowv1['Close'].values
    comb_lowv_beta = comb_prices.copy()
    for i in comb_lowv_beta.tick_cols():
        comb_lowv_beta[i] = crtf.beta_cc(comb_lowv_beta[i].values, comb_lowv_val, 120)
    _d_comb.store('LVOL_Beta_6m', comb_lowv_beta)
    comb_lowv_beta = comb_prices.copy()
    for i in comb_lowv_beta.tick_cols():
        comb_lowv_beta[i] = crtf.beta_cc(comb_lowv_beta[i].values, comb_lowv_val, 60)
    _d_comb.store('LVOL_Beta_3m', comb_lowv_beta)
    comb_lowv_beta = comb_prices.copy()
    for i in comb_lowv_beta.tick_cols():
        comb_lowv_beta[i] = crtf.beta_cc(comb_lowv_beta[i].values, comb_lowv_val, 180)
    _d_comb.store('LVOL_Beta_9m', comb_lowv_beta)
    comb_lowv_beta = comb_prices.copy()
    for i in comb_lowv_beta.tick_cols():
        comb_lowv_beta[i] = crtf.beta_cc(comb_lowv_beta[i].values, comb_lowv_val, 240)
    _d_comb.store('LVOL_Beta_12m', comb_lowv_beta)


    # correlation with SPY
    comb_sp_beta = comb_prices.copy()
    comb_sp_beta2 = comb_prices.copy()
    comb_sp_beta3 = comb_prices.copy()
    comb_sp_beta4 = comb_prices.copy()
    for i in comb_sp_beta.tick_cols():
        x_ = cruf.DataFrame.merge(comb_sp_beta[['Date', i]], spy_prices[['Date', 'Close']], how='left', on='Date')
        x_.sort('Date')
        x_['Close'] = crtf.fill(x_['Close'].values)
        x_[i] = crtf.fill(x_[i].values)
        comb_sp_beta[i] = crtf.beta_cc(x_[i].values, x_['Close'].values, 120)
        comb_sp_beta2[i] = crtf.beta_cc(x_[i].values, x_['Close'].values, 60)
        comb_sp_beta3[i] = crtf.beta_cc(x_[i].values, x_['Close'].values, 180)
        comb_sp_beta4[i] = crtf.beta_cc(x_[i].values, x_['Close'].values, 240)
    _d_comb.store('SP_Beta_6m', comb_sp_beta)
    _d_comb.store('SP_Beta_3m', comb_sp_beta2)
    _d_comb.store('SP_Beta_9m', comb_sp_beta3)
    _d_comb.store('SP_Beta_12m', comb_sp_beta4)

    return None