Пример #1
0
def _refresh_idx_betas(x):
    _d_comb = x
    comb_prices = _d_comb.retrieve("Close")
    tc = comb_prices.tick_cols()

    spy_prices = d_pr_cl.retrieve("SPY")
    for j in range(60, 300, 60):
        # j = 60
        comb_spy_beta = comb_prices.copy()
        comb_spy_beta = cruf.DataFrame.merge(comb_spy_beta, spy_prices[["Date", "Close"]], how="left", on="Date")
        comb_spy_beta.sort("Date")
        comb_spy_beta["Close"] = crtf.fill(comb_spy_beta["Close"].values)
        comb_spy_price = comb_spy_beta["Close"].values
        del comb_spy_beta["Close"]
        for i in tc:
            comb_spy_beta[i] = crtf.beta_cc(comb_spy_beta[i].values, comb_spy_price, j)
        _d_comb.store("SPY_Beta_" + str(int(j / 20)) + "m", comb_spy_beta)

    mdy_prices = d_pr_cl.retrieve("MDY")
    for j in range(60, 300, 60):
        # j = 60
        comb_mdy_beta = comb_prices.copy()
        comb_mdy_beta = cruf.DataFrame.merge(comb_mdy_beta, mdy_prices[["Date", "Close"]], how="left", on="Date")
        comb_mdy_beta.sort("Date")
        comb_mdy_beta["Close"] = crtf.fill(comb_mdy_beta["Close"].values)
        comb_mpy_price = comb_mdy_beta["Close"].values
        del comb_mdy_beta["Close"]
        for i in tc:
            comb_mdy_beta[i] = crtf.beta_cc(comb_mdy_beta[i].values, comb_mpy_price, j)
        _d_comb.store("MDY_Beta_" + str(int(j / 20)) + "m", comb_mdy_beta)
    return None
Пример #2
0
def _create_combined_data(x):
    _clus, _d_comb = x
    tick_sym_all = d_un.retrieve(_clus)['Ticker'].values.astype('str')
    for i in tick_sym_all:
        if i == tick_sym_all[0]:
            comb_data_hor = d_cl.retrieve(i)
            comb_data_hor = comb_data_hor[comb_data_hor['Date'] >= dt.datetime(1999, 12, 31), :]
            comb_data_op = comb_data_hor[['Date', 'Open']]
            comb_data_hi = comb_data_hor[['Date', 'High']]
            comb_data_lo = comb_data_hor[['Date', 'Low']]
            comb_data_cl = comb_data_hor[['Date', 'Close']]
            comb_data_vl = comb_data_hor[['Date', 'Volume']]
            comb_data_tr = comb_data_hor[['Date', 'Turnover']]
            comb_data_op.rename({'Open': i})
            comb_data_hi.rename({'High': i})
            comb_data_lo.rename({'Low': i})
            comb_data_cl.rename({'Close': i})
            comb_data_vl.rename({'Volume': i})
            comb_data_tr.rename({'Turnover': i})
        else:
            comb_data_hor_new = d_cl.retrieve(i)
            comb_data_hor_new = comb_data_hor_new[comb_data_hor_new['Date'] >= dt.datetime(1999, 12, 31), :]
            comb_data_op_new = comb_data_hor_new[['Date', 'Open']]
            comb_data_hi_new = comb_data_hor_new[['Date', 'High']]
            comb_data_lo_new = comb_data_hor_new[['Date', 'Low']]
            comb_data_cl_new = comb_data_hor_new[['Date', 'Close']]
            comb_data_vl_new = comb_data_hor_new[['Date', 'Volume']]
            comb_data_tr_new = comb_data_hor_new[['Date', 'Turnover']]
            comb_data_op_new.rename({'Open': i})
            comb_data_hi_new.rename({'High': i})
            comb_data_lo_new.rename({'Low': i})
            comb_data_cl_new.rename({'Close': i})
            comb_data_vl_new.rename({'Volume': i})
            comb_data_tr_new.rename({'Turnover': i})
            # merge them
            comb_data_op = cruf.DataFrame.merge(comb_data_op, comb_data_op_new, on='Date')
            comb_data_hi = cruf.DataFrame.merge(comb_data_hi, comb_data_hi_new, on='Date')
            comb_data_lo = cruf.DataFrame.merge(comb_data_lo, comb_data_lo_new, on='Date')
            comb_data_cl = cruf.DataFrame.merge(comb_data_cl, comb_data_cl_new, on='Date')
            comb_data_vl = cruf.DataFrame.merge(comb_data_vl, comb_data_vl_new, on='Date')
            comb_data_tr = cruf.DataFrame.merge(comb_data_tr, comb_data_tr_new, on='Date')
            comb_data_op.sort('Date')
            comb_data_hi.sort('Date')
            comb_data_lo.sort('Date')
            comb_data_cl.sort('Date')
            comb_data_vl.sort('Date')
            comb_data_tr.sort('Date')
    tick_cols = comb_data_cl.tick_cols()
    for i in tick_cols:
        comb_data_cl[i] = crtf.fill(comb_data_cl[i].values)
        comb_data_op[i] = crtf.fill2(comb_data_op[i].values, comb_data_cl[i].values)
        comb_data_hi[i] = crtf.fill2(comb_data_hi[i].values, comb_data_cl[i].values)
        comb_data_lo[i] = crtf.fill2(comb_data_lo[i].values, comb_data_cl[i].values)
        comb_data_vl[i] = crtf.fill1(comb_data_vl[i].values, 0)
        comb_data_tr[i] = crtf.fill1(comb_data_tr[i].values, 0)
    # save the data
    _d_comb.store('Open', comb_data_op)
    _d_comb.store('High', comb_data_hi)
    _d_comb.store('Low', comb_data_lo)
    _d_comb.store('Close', comb_data_cl)
    _d_comb.store('Volume', comb_data_vl)
    _d_comb.store('Turnover', comb_data_tr)
    return None