def _refresh_correl(x): _d_comb, _d_cor_dy, _d_cor_st = x comb_prices = _d_comb.retrieve('Close') tc = comb_prices.tick_cols() ndt = comb_prices.shape[0] ntc = len(tc) _prc = comb_prices[tc].values.astype('float64') window1 = 240 window2 = 120 ntc1 = ntc*(ntc-1)/2 _cor1 = np.empty([ndt, ntc1], dtype='float32')*np.nan _cor2 = np.empty([ndt, ntc1], dtype='float32')*np.nan # calculate the correlations cnt = 0 for i in range(0, ntc-1): for j in range(i+1, ntc): # _cor1[:, cnt] = _spear(_prc[:, i], _prc[:, j], window1) # _cor2[:, cnt] = _spear(_prc[:, i], _prc[:, j], window2) _cor1[:, cnt] = crtf.cor_cc(_prc[:, i], _prc[:, j], window1, zl=False) _cor2[:, cnt] = crtf.cor_cc(_prc[:, i], _prc[:, j], window2, zl=False) cnt += 1 # if cnt % 100 == 0: # print(cnt) # remove correlations before date st_dt cp_dt = crup.pd_dt_to_str(comb_prices['Date', list]) cp_dt_idx = [i for i, j in enumerate(cp_dt) if j >= st_dt] for i in cp_dt_idx: # i = cp_dt_idx[0] _cor_mat1 = np.empty([ntc, ntc])*np.nan _cor_mat2 = np.empty([ntc, ntc])*np.nan cnt = 0 for j in range(0, ntc): for k in range(j, ntc): if j == k: _cor_mat1[j, j] = 1 _cor_mat2[j, j] = 1 else: _cor_mat1[j, k] = _cor1[i, cnt] _cor_mat1[k, j] = _cor_mat1[j, k] _cor_mat2[j, k] = _cor2[i, cnt] _cor_mat2[k, j] = _cor_mat2[j, k] cnt += 1 _d_cor_st.save(cp_dt[i], _cor_mat1) _d_cor_dy.save(cp_dt[i], _cor_mat2) return None
crup.plot_ts(dt, x12_act) # base case n = x1.shape[0] mi = 240 mi2 = (2*mi+1)/3 # x12_corr2 = np.ones(n)*np.nan # x12_corr2 = (crtf.ema(x1*x2, mi)-crtf.ema(x1, mi)*crtf.ema(x2, mi))/\ # np.sqrt((crtf.ema(x1**2, mi)-crtf.ema(x1, mi)**2)*(crtf.ema(x2**2, mi)-crtf.ema(x2, mi)**2)) # crup.plot_ts(dt, x12_corr2) # x12_corr2 = np.ones(n)*np.nan # x12_corr2 = (crtf.sma(x1*x2, mi)-crtf.sma(x1, mi)*crtf.sma(x2, mi))/\ # np.sqrt((crtf.sma(x1**2, mi)-crtf.sma(x1, mi)**2)*(crtf.sma(x2**2, mi)-crtf.sma(x2, mi)**2)) # crup.plot_ts(dt, x12_corr2) x12_corr61 = crtf.cor_cc(testc1, testc2, mi, smth=7, zl=False) crup.plot_ts(dt, x12_corr61[xidx]) x12_corr61 = crtf.cor_gk3(testo1, testc1, testo2, testc2, mi, smth=7, zl=False) crup.plot_ts(dt, x12_corr61[xidx]) x12_corr61 = crtf.cor_pb(testo1, testh1, testl1, testc1, testo2, testh2, testl2, testc2, mi, smth=7, zl=False) crup.plot_ts(dt, x12_corr61[xidx]) # # x12_corr61 = crtf.cor_cc(testc1, testc2, mi, smth=13, zl=False) # crup.plot_ts(dt, x12_corr61[xidx]) # x12_corr61 = crtf.cor_cc(testc1, testc2, mi, smth=19, zl=False) # crup.plot_ts(dt, x12_corr61[xidx]) # x12_corr61 = crtf.cor_cc(testc1, testc2, mi, smth=7, zl=True) # crup.plot_ts(dt, x12_corr61[xidx]) #
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 testo2 = testo[tic2].values testh1 = testh[tic1].values