def get_alt_price_weekly():
    univ_ib_op = cr_cret.retrieve(univ_ib_eqidx_ext + "Open")
    univ_ib_cl = cr_cret.retrieve(univ_ib_eqidx_ext + "Close")
    tick_cols = univ_ib_cl.tick_cols()
    univ_ib_gd = cr_cret.retrieve(univ_ib_eqidx_ext + "ExchOpen")
    univ_ib_ap1 = univ_ib_cl.copy()
    univ_ib_ap2 = univ_ib_cl.copy()
    univ_ib_ap3 = univ_ib_cl.copy()

    for i in tick_cols:
        # i = 'SP500'
        z = np.where(univ_ib_gd[i].values.astype("int") == 1)[0]
        if z.shape[0] > 0:
            univ_ib_cl_ = univ_ib_cl[i].values[z]
            univ_ib_op_ = univ_ib_op[i].values[z]
            # univ_ib_dt_ = univ_ib_cl['Date'].values[z]  # temp
            # convert to weekly
            nb = filt.fst_nan(univ_ib_cl_)
            ne = len(univ_ib_cl_)
            univ_ib_ord = np.ones(ne) * 99
            univ_ib_ap1_ = np.zeros(ne) * np.nan
            univ_ib_ap2_ = np.zeros(ne) * np.nan
            univ_ib_ap3_ = np.zeros(ne) * np.nan
            for k in range(0, 5):
                univ_ib_ord[nb + 4 + k : ne : 5] = k
            univ_ib_ord = univ_ib_ord.astype("int")
            for k in range(0, 5):
                # k = 0
                zk = np.where(univ_ib_ord == k)[0]
                univ_ib_cl__ = univ_ib_cl_[list(zk)]
                univ_ib_op__ = univ_ib_op_[list(zk - 4)]
                univ_ib_ap1__ = filt.smth_price(univ_ib_op__, univ_ib_cl__, 120, 10, 10, 6, 1, 13)
                univ_ib_ap2__ = filt.smth_price(univ_ib_op__, univ_ib_cl__, 180, 15, 10, 8, 1, 13)
                univ_ib_ap3__ = filt.smth_price(univ_ib_op__, univ_ib_cl__, 140, 30, 10, 8, 1, 16)

                univ_ib_ap1_[list(zk)] = univ_ib_ap1__
                univ_ib_ap2_[list(zk)] = univ_ib_ap2__
                univ_ib_ap3_[list(zk)] = univ_ib_ap3__

            univ_ib_ap1_ = filt.sma(univ_ib_ap1_, 5)
            univ_ib_ap2_ = filt.sma(univ_ib_ap2_, 5)
            univ_ib_ap3_ = filt.sma(univ_ib_ap3_, 5)
            univ_ib_ap1[list(z), i] = univ_ib_ap1_
            univ_ib_ap2[list(z), i] = univ_ib_ap2_
            univ_ib_ap3[list(z), i] = univ_ib_ap3_
        else:
            univ_ib_ap1[i] = np.nan
            univ_ib_ap2[i] = np.nan
            univ_ib_ap3[i] = np.nan
    cr_cret.store(univ_ib_eqidx_ext + "AdjSer20W", univ_ib_ap1)
    cr_cret.store(univ_ib_eqidx_ext + "AdjSer30W", univ_ib_ap2)
    cr_cret.store(univ_ib_eqidx_ext + "AdjSer40W", univ_ib_ap3)
def _get_alt_price(i):
    print('Calculating alternating prices for %s ' % i)
    univ_ib_ret = mkt_retrieve(i, 'Stats', 'Returns')
    univ_ib_op = univ_ib_ret['Open'].values
    univ_ib_cl = univ_ib_ret['Close'].values

    univ_ib_sprc = univ_ib_ret[['Contract', 'Date']]
    for smth in _smth:  # range(6, _smth + 1, 3)
        _sp = smth_param(smth)
        for days in range(1, _days+1):
            nb = filt.fst_nan(univ_ib_cl)
            ne = len(univ_ib_cl)
            univ_ib_ord = np.ones(ne, dtype=int) * 99
            univ_ib_ap = np.zeros(ne) * np.nan
            for k in range(0, days):
                univ_ib_ord[nb + days + k - 1:ne:days] = k
            for k in range(0, days):
                zk = np.where(univ_ib_ord == k)[0]
                cl_ = univ_ib_cl[list(zk)]
                op_ = univ_ib_op[list(zk - days + 1)]
                univ_ib_ap[list(zk)] = filt.smth_price(op_, cl_, _sp[0], _sp[1], _sp[2], _sp[3], 0, _sp[4])
            univ_ib_ap = filt.sma(univ_ib_ap, days)
            univ_ib_ap = filt.fill(univ_ib_ap)
            if (days < 10) and (smth < 10):
                univ_ib_sprc['D0'+str(days)+'S0'+str(smth)] = univ_ib_ap
            elif (days >= 10) and (smth < 10):
                univ_ib_sprc['D'+str(days)+'S0'+str(smth)] = univ_ib_ap
            elif (days < 10) and (smth >= 10):
                univ_ib_sprc['D0'+str(days)+'S'+str(smth)] = univ_ib_ap
            else:
                univ_ib_sprc['D'+str(days)+'S'+str(smth)] = univ_ib_ap
    del univ_ib_sprc['Contract']
    mkt_store(i, 'Stats', 'AltReturns', univ_ib_sprc)
def response_curve(x1, _ticker='SP500', f=None, md=True, sigd=False):
    univ_ib_gd = cr_cret.retrieve(univ_ib_eqidx_ext + 'ExchOpen')[_ticker].values
    univ_ib_vl = cr_vol_all_adj.retrieve(univ_ib_eqidx_ext + 'vol_pb_120')[_ticker].values
    univ_ib_s1 = cr_sig_mr_sg.retrieve(univ_ib_eqidx_ext + x1)[_ticker].values
    univ_ib_cl = cr_cret.retrieve(univ_ib_eqidx_ext + 'Close')[_ticker].values
    z = list(np.where(univ_ib_gd.astype('int') == 1)[0])
    univ_ib_s1 = univ_ib_s1[z]
    univ_ib_cl = univ_ib_cl[z]
    univ_ib_vl = univ_ib_vl[z]

    univ_ib_cl = filt.ret(univ_ib_cl)/filt.lag(univ_ib_vl, 1)
    univ_ib_s1 = filt.lag(univ_ib_s1, 1)/filt.lag(univ_ib_vl, 1)

    univ_ib_cl, univ_ib_s1 = reduce_nonnan(univ_ib_cl, univ_ib_s1)

    _bins = 20
    _range = np.maximum(np.percentile(univ_ib_s1, 99), -np.percentile(univ_ib_s1, 1))
    _delta = _range/_bins
    if f is not None:
        pyl.figure(f)
    else:
        pyl.figure(1)
    for i in range(0, 16):
        if i == 0:
            uis1 = univ_ib_s1
            uic1 = univ_ib_cl
        else:
            uis1 = filt.lag(univ_ib_s1, i)
            uic1 = filt.sma(univ_ib_cl, i+1)
        uis1, uic1 = reduce_nonnan(uis1, uic1)
        uis1_b = np.linspace(-_range, _range, num=_bins+1)
        uic1_b = np.zeros(_bins+1)*np.nan
        for j in range(0, _bins+1):
            # j = 1
            if j==0:
                tmp__ = np.where(uis1 <= uis1_b[j]+_delta)[0]
            elif j == _bins+1:
                tmp__ = np.where(uis1 > uis1_b[j]-_delta)[0]
            else:
                tmp__ = np.where((uis1 <= uis1_b[j]+_delta) & (uis1 > uis1_b[j]-_delta))[0]
            if tmp__.shape[0] > 0:
                if md:
                    if not sigd:
                        uic1_b[j] = np.nanmedian(uic1[tmp__]) #/np.nanstd(uic1[tmp__])
                    else:
                        uic1_b[j] = np.nanmedian(uic1[tmp__])/np.nanstd(uic1[tmp__])
                else:
                    if not sigd:
                        uic1_b[j] = np.nanmean(uic1[tmp__]) #/np.nanstd(uic1[tmp__])
                    else:
                        uic1_b[j] = np.nanmean(uic1[tmp__])/np.nanstd(uic1[tmp__])
        pyl.subplot(4, 4, i+1)
        pyl.plot(uis1_b, uic1_b)