3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RCI - 順位相関指数

Last updated at Posted at 2021-08-15

RCI - Rank Correlation Index
RCI(順位相関指数)がTA-Libに無いのでPythonで書いてみた。

rci.py
    import numpy as np

    def rci(close: np.ndarray,
            timeperiod: int = 9) -> np.ndarray:
        rci = np.full_like(close, np.nan)
        rank_period = np.arange(1, timeperiod + 1)
        for i in range(timeperiod - 1, len(close)):
            rank_price = close[i - timeperiod + 1:i + 1]
            rank_price = np.argsort(np.argsort(rank_price)) + 1
            aa = 6 * sum((rank_period - rank_price)**2)
            bb = timeperiod * (timeperiod**2 - 1)
            rci[i] = (1 - aa / bb) * 100
        return rci

ちょっと改良版
forループをやめて高速化しました。
ただしtimeperiodが大きすぎるとメモリ消費が激しく
Forループ版との速度差が縮まっていきます。

rci_np.py
def rci(close: np.ndarray,
        timeperiod: int = 9) -> np.ndarray:
    rank_target = [np.roll(close, i, axis=-1) for i in range(timeperiod)]
    rank_target = np.vstack(rank_target)[:, timeperiod - 1:]
    price_rank = np.argsort(np.argsort(rank_target[::-1], axis=0), axis=0) + 1
    time_rank = np.arange(1, timeperiod + 1).reshape(timeperiod, -1)
    aa = np.sum((time_rank - price_rank)**2, axis=0, dtype=float) * 6
    bb = float(timeperiod * (timeperiod**2 - 1))
    cc = np.divide(aa, bb, out=np.zeros_like(aa), where=bb != 0)
    rci = (1 - cc) * 100
    rci = np.concatenate([np.full(timeperiod - 1, np.nan), rci], axis=0)
    return rci
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?