示例#1
0
文件: ad.py 项目: quantiacs/toolbox
def ad_ratio_np(prices: np.ndarray) -> np.ndarray:
    """
     Advance/Decline Ratio (numpy)
    :param prices: last dimension - time
    :return:
    """
    ch = change(prices, 1)
    ch = np.nan_to_num(ch)
    advance = np.where(ch > 0, 1, 0).sum(axis=0)
    decline = np.where(ch < 0, 1, 0).sum(axis=0)
    return advance / decline
示例#2
0
def rsi(
        series: nda.NdType,
        ma: tp.Any = 14  # lambda series: wilder_ma(series, 14)
) -> nda.NdType:
    if isinstance(ma, int):
        ma_period = ma
        ma = lambda series: wilder_ma(series, ma_period)

    ch = change(series)
    up = np.maximum(ch, 0)  # positive changes
    down = -np.minimum(ch, 0)  # negative changes (inverted)

    up = ma(up)
    down = ma(down)

    rs = up / down
    rsi = 100 * (1 - 1 / (1 + rs))

    return rsi