Python으로 돈치안채널(Donchian Channels) 구현하기

돈치안 채널(Donchian Channels)은 Richard Donchian이 개발한 추세 추종 지표입니다. 돈치안 채널은 최근 n개의 기간 동안의 최고가(highest high)와 최저가(lowest low)를 기준으로 형성됩니다. 해당 기간 동안 최고가와 최저가 사이의 영역이 채널을 형성합니다.

pip install pandas ccxt

import pandas as pd
import ccxt

# 예시) 바이낸스 선물 BTCUSDT, 1시간 봉 
bnb = ccxt.binance({'options': { 'defaultType': 'future' }}) 


def DC(length=20):
    ohlcv = bnb.fetch_ohlcv(symbol="BTC/USDT", timeframe="1h", limit=500)
    df = pd.DataFrame(ohlcv, columns=['time', 'open', 'high', 'low', 'close', 'volume'])

    df['upper'] = df['high'].rolling(window=length).max().round(1)
    df['lower'] = df['low'].rolling(window=length).min().round(1)
    df['basis'] = ((df['upper'] + df['lower']) / 2).round(1)

    return df

print(DC(length=20)) # 트레이딩뷰 디폴트 값

트레이딩뷰 차트와 비교

basis / upper / lower 순서

Pine Script와 Python 간의 약간의 오차는 숫자 처리 방식, 소수점 자릿수, 데이터 정렬, 초기값 설정, 반올림 차이 등에서 발생할 수 있습니다.