[Python]Choppiness index


ビットコインのalgorithmic trading botを作るうえでレンジ判断に使える指標がないかと探していたら、Choppiness indexというものが見つかったのですがTaLibには無かったので自作しました。
I was looking for an indicator to identify sideways trend of a market and came across this index called "Choppiness index".I made it because the index was not in Ta-Lib, so please feel free to use it if you are interested.

Choppiness index 引用元 reference
https://www.linnsoft.com/techind/choppiness-index

Talib
https://github.com/mrjbq7/ta-lib

重要な計算部分 Important calculation part
tpの基本設定は"14"らしいです the basic setting of tp is "14" according to the webisite

ATR = ta.ATR(high,low,close, timeperiod=tp)
nmrt = np.log10(np.sum(ATR[i-tp:i])/(max(high[i-tp:i])-min(low[i-tp:i])))
dnmnt = np.log10(tp)
choppiness = 100*nmrt / dnmnt

Pandas DataFrame1 -> calculation -> pandas DataFrame2

choppiness.py
import talib as ta
import numpy as np
import pandas as pd
from collections import deque

def choppiness(tp,candlestick):
    high = candlestick["high"]
    low = candlestick["low"]
    close = candlestick["close"]
    ATR = ta.ATR(high,low,close, timeperiod=tp)
    Timestamp = deque()
    CP = deque()
    for i in range(len(candlestick)):
        if i < tp*2:
            Timestamp.append(candlestick.index[i])
            CP.append(0)
        else:
            nmrt = np.log10(np.sum(ATR[i-tp:i])/(max(high[i-tp:i])-min(low[i-tp:i])))
            dnmnt = np.log10(tp)
            Timestamp.append(candlestick.index[i])
            CP.append(round(100*nmrt / dnmnt))
    CP = pd.DataFrame({"CP" : CP}, index=Timestamp)
    return CP

example of DataFrame1

example of DataFrame2

graph of the result above

間違っている部分や効率化できる部分があればコメントいただける幸いです。
Please leave your comment if there is anything wrong or to make it more efficient