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
tic1 = 'SPY' tic2 = 'QQQ' testc = d_comb[6].retrieve('Close') testo = d_comb[6].retrieve('Open') testh = d_comb[6].retrieve('High') testl = d_comb[6].retrieve('Low') testc1 = testc[tic1].values testc2 = testc[tic2].values testo1 = testo[tic1].values testo2 = testo[tic2].values testh1 = testh[tic1].values testh2 = testh[tic2].values testl1 = testl[tic1].values testl2 = testl[tic2].values x1 = crtf.ret(testc1) x2 = crtf.ret(testc2) xidx = ~np.isnan(x1) & ~np.isnan(x2) x1 = x1[xidx] x2 = x2[xidx] dt = testc['Date'].values dt = dt[xidx] # static x_act = np.corrcoef(x1, x2)[0, 1] x12_act = np.ones(x1.shape[0])*x_act crup.plot_ts(dt, x12_act) # base case n = x1.shape[0] mi = 240
cnt1gk3 = 0 # best for 240 cnt1pb = 0 cnt1rs = 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):
import matplotlib.pyplot as plt d_un = crsf.cr_un d_pr_cl = crsf.cr_pr_cl d_comb = crsf.cr_comb d_sig = crsf.cr_sig tic1 = 'SPY' tic2 = 'QQQ' testc = d_comb[6].retrieve('Close') testc1 = testc[tic1].values testc2 = testc[tic2].values testr1 = crtf.ret(testc1) testr2 = crtf.ret(testc2) test_idx = ~np.isnan(testr1) & ~np.isnan(testr2) testr1 = testr1[test_idx] testr2 = testr2[test_idx] n = testr1.shape[0] test_c1 = np.empty(n)*np.nan test_c2 = np.empty(n)*np.nan test_c3 = np.empty(n)*np.nan def _smoother(x, m): return crtf.fish_inv_trans(crtf.lrbeta(crtf.cumsum_special(crtf.fish_trans(x)), m, lg=False))
def _generate_ret(_d_comb): comb_data_close = _d_comb.retrieve('Close') tick_cols = comb_data_close.tick_cols() for i in tick_cols: comb_data_close[i] = crtf.ret(comb_data_close[i].values) _d_comb.store('Returns', comb_data_close)