[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
htps //w w.ぃそ ft. 코 m / 테친 d / 조금 네 s

Talib
htps : // 기주 b. 코 m / mrjbq7 / 타 ぃ b

중요한 계산 부분 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

좋은 웹페이지 즐겨찾기