def _refresh_signals(x): _d_comb, _d_sig = x # generate the signals (type 1) comb_prices = _d_comb.retrieve('Close') 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 the signals (type 2) comb_ret = _d_comb.retrieve('Returns') for j in range(1, 51): comb_ret_lr = comb_ret.copy() comb_ret_qr = comb_ret.copy() for i in comb_ret_lr.tick_cols(): comb_ret_qr[i] = crtf.lrma(crtf.lrma(crtf.qrma(comb_ret_qr[i].values, j*10), 5), 5) comb_ret_lr[i] = crtf.lrma(crtf.lrma(crtf.lrma(comb_ret_qr[i].values, j*10), 5), 5) _d_sig.store('QrS_'+str(j*10)+'D', comb_ret_qr) _d_sig.store('LrS_'+str(j*10)+'D', comb_ret_lr) del comb_ret_lr del comb_ret_qr return None
def _refresh_stats(x): _d_comb = x comb_prices_c = _d_comb.retrieve('Close') comb_prices_o = _d_comb.retrieve('Open') comb_prices_h = _d_comb.retrieve('High') comb_prices_l = _d_comb.retrieve('Low') # store the leverage for j in range(0, 4): jj = 30*(2**j) jjstr = str(jj) comb_vol = comb_prices_c.copy() for i in comb_vol.tick_cols(): comb_vol[i] = crtf.vol_pb(comb_prices_o[i].values, comb_prices_h[i].values, comb_prices_l[i].values, comb_prices_c[i].values, jj) # comb_vol[i] = crtf.vol_cc(comb_prices[i].values, jj) # 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) # replace this by qrma_multi(3, 5) _d_comb.store('Lev_CC_'+jjstr+'D', comb_lev) for i in comb_vol.tick_cols(): comb_vol[i] = crtf.lrma(comb_vol[i].values, 30, lg=True) # replace this by qrma_multi(3, 5) _d_comb.store('Vol_CC_'+jjstr+'D', comb_vol) return None
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
def _lrma_multi(x, n1, n2): for j in range(0, n2): x = crtf.lrma(x, n1) return x
crup.plot_ts(dt, test_c5) #------------------------------------ test = d_comb[6].retrieve('Close') test1 = crtf.ret(test['SPY'].values) test2 = crtf.ret(test['TLT'].values) testidx = ~np.isnan(test1) & ~np.isnan(test2) test1 = test1[testidx] test2 = test2[testidx] testc = test1*test2/np.sqrt((test1**2)*(test2**2)) dt = test['Date'].values dt = dt[testidx] # crup.plot_ts(dt, testc) print(np.any(np.isinf(testc))) tests = crtf.lrma(testc, 240, lg=False) crup.plot_ts(dt, tests) testcc = crtf.cor_cc(test['SPY'].values, test['TLT'].values, 240, zl=False) testcc = testcc[testidx] crup.plot_ts(dt, testcc) # test_c4 = crtf.cor_cc(testc1, testc2, window, zl=False) # crup.plot_ts(dt, test_c4[test_idx]) #------------------------ testo = d_comb[6].retrieve('Open') testh = d_comb[6].retrieve('High') testl = d_comb[6].retrieve('Low') testo1 = testo[tic1].values