Пример #1
0
def tba(target_quote, alpha=0.05, bounds=(-5, 5)):
    ba_ratio = np.log(target_quote['bv1'] / target_quote['av1'])
    clipped_ba_ratio = np.clip(ba_ratio, bounds[0], bounds[1]).values
    ba_ratio_ema = nbr.numba_ewma(clipped_ba_ratio,
                                  alpha=alpha,
                                  state=None,
                                  adjust=True,
                                  ignore_na=True,
                                  minp=1)[0]
    return ba_ratio_ema
Пример #2
0
def dbook(target_quote, n=1, alpha=0.05):
    bv1, av1 = target_quote[['bv1', 'av1']].values.astype(np.float).T
    diff_bv1 = np.insert(np.sign(np.diff(bv1, n=n)), [0] * n, 0)
    diff_av1 = np.insert(np.sign(np.diff(av1, n=n)), [0] * n, 0)
    rs = nbr.numba_ewma((diff_bv1 - diff_av1),
                        alpha=alpha,
                        state=None,
                        adjust=True,
                        ignore_na=True,
                        minp=1)[0]
    return rs
Пример #3
0
def tba(target_quote, alpha=0.05):
    # ba_ratio = np.log(target_quote['bv1'] / target_quote['av1'])
    bv1, av1 = target_quote[['bv1', 'av1']].values.astype(np.float).T
    ba_ratio = np.sqrt(
        np.divide(bv1, av1, out=np.full_like(bv1, np.nan), where=av1 != 0))
    ba_ratio_ema = nbr.numba_ewma(ba_ratio,
                                  alpha=alpha,
                                  state=None,
                                  adjust=True,
                                  ignore_na=True,
                                  minp=1)[0]
    return ba_ratio_ema
Пример #4
0
def calc_absr(df, window):
    """
    Estimate the active trading volume (absolute buy sell ratio).
    ask, bid volume and amount must be float.

    """
    ask, bid, volume, amount = df[['ap1', 'bp1', 'volume',
                                   'turnover']].values.T

    vol = volume.copy().astype(float)
    np.place(vol, vol == 0, np.nan)
    avgcost = np.divide(amount, vol)
    spread = ask - bid

    # Seperate
    avgc_over_ask = avgcost[1:] > ask[:-1]
    avgc_below_bid = avgcost[1:] < bid[:-1]
    avgc_between = (~avgc_over_ask) & (~avgc_below_bid)
    vol_1 = vol[1:]

    # calculate active buyer
    actibuy = vol_1 * avgc_over_ask
    ab_ratios = np.divide((avgcost[1:] - bid[:-1]), spread[:-1])
    actibuy[avgc_between] = vol_1[avgc_between] * ab_ratios[avgc_between]

    # calculate active seller
    actisell = vol_1 * avgc_below_bid
    as_ratios = 1 - ab_ratios
    actisell[avgc_between] = vol_1[avgc_between] * as_ratios[avgc_between]

    actibuy_end = nbr.numba_ewma(actibuy, alpha=0.05)[0][-1]
    actisell_end = np.max([nbr.numba_ewma(actisell, alpha=0.05)[0][-1], 1])
    out = actibuy_end / actisell_end
    # active_volume = np.vstack([actibuy, actisell]).T.astype('float64')
    # active_volume = np.nan_to_num(active_volume)
    # active_volume_arr = np.full((len(vol), 2), np.nan)
    # active_volume_rolsum = np.sum(lut.sliding_window_view(active_volume.T, window), axis=2).T
    # active_volume_arr[window:] = active_volume_rolsum
    # out = np.log(np.divide(active_volume_arr[:, 0], active_volume_arr[:, 1], out=np.full(len(active_volume_arr), np.nan), where=active_volume_arr[:, 1]!=0))
    return out
Пример #5
0
def calc_factor1(df):
    # 基于订单簿的筹码倾向:以价格为标尺
    _obp = np.ravel(df.filter(regex=r'[ab]p\d+'))
    _obv = np.ravel(df.filter(regex=r'[ab]v\d+'))
    # ob_agg = pd.Series({p: np.mean(_obv[_obp == p]) for p in np.unique(_obp) if p != 0})
    ob_agg = pd.Series({
        p: nbr.numba_ewma(_obv[_obp == p], alpha=0.05)[-1][0]
        for p in np.unique(_obp) if p != 0
    })
    _mid = df['new_price'].iloc[-1]
    over = ob_agg[ob_agg.index > _mid].copy()
    wgt_over_sum = np.sum(over * over.index / _mid)
    under = ob_agg[ob_agg.index < _mid].copy()
    # wgt_under_sum = np.sum(under * _mid / under.index)
    wgt_under_sum = np.max([np.sum(under * _mid / under.index), 1])
    order_ratio = wgt_over_sum / wgt_under_sum
    return order_ratio