파인스크립트로 DMI 지표를 만들어보자
트레이딩뷰 하단에 지표를 만드는 창이있습니다.
1. DMI(방향 이동 지수)는 어떤 지표일까?
DMI(방향성 이동 지수)는 1978년 J. Welles Wilder가 개발한 지표입니다.
자산 가격(시장의 추세)이 어느 방향으로 움직이고 있는지를 나타냅니다.
평균 방향 지수 (ADX)의 선은 상승 추세 또는 하락 추세의 강도를 나타냅니다.
+DI가 -DI보다 높으면 가격의 하방 압력보다 매수세가 더 큽니다.
반대로, -DI가 +DI보다 높으면 가격에 더 많은 매도세가 있습니다.
더욱 자세하게 공부하고 싶다면 아래에 링크에서 참고하시면 됩니다.
방향 이동 지수(DMI) 정의 (investopedia.com)
2. 트레이딩 뷰에서 제공하는 DMI의 기본 소스코드
//@version=5
indicator(title="Directional Movement Index", shorttitle="DMI", format=format.price, precision=4, timeframe="", timeframe_gaps=true)
lensig = input.int(14, title="ADX Smoothing", minval=1, maxval=50)
len = input.int(14, minval=1, title="DI Length")
up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
trur = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / trur)
minus = fixnan(100 * ta.rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)
plot(adx, color=#F50057, title="ADX")
plot(plus, color=#2962FF, title="+DI")
plot(minus, color=#FF6D00, title="-DI")
트레이딩 뷰에서 제공하는 DMI(방향 이동 지수)기본 소스코드를 이용해서
아래에서는 이 내용에 추가적인 기능을 넣어서 조금 더 쉽고 간결하게 만들어보겠습니다.
3. 파인스크립트 지표의 버전과 타이틀 설정하기
//https://goingrunv.com
//@version=5
// 타이틀
indicator(title="GRV-DMI", shorttitle="GRV-DMI", format=format.price, precision=4, timeframe="", timeframe_gaps=true)
지표를 만들기에 앞서 어떤 언어를 사용할 건지에 대한 버전을 입력해야 합니다.
앞으로 다루게 될 내용에서는 버전 5를 사용할 예정입니다.
이유는 버전 5 밑으로는 트레이딩 뷰에서 리버전 하는 기능이 있기에 호환이 가능합니다.
그리고 또한 구현하는 함수들이 사용하기 쉽도록 더욱 간결해졌습니다.
- title = 스크립트의 이름을 설정합니다.
- shorttitle = 스크립트에 단축된 이름을 설정합니다.
- overlay = 스크립트를 차트 위에 표시합니다.
4. DMI(방향 이동 지수)함수 설정하고 라인 출력하기
// DMI 함수정의
lensig = input.int(14, title="ADX 스무딩", minval=1, maxval=50)
len = input.int(14, minval=1, title="DI 길이")
up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
trur = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / trur)
minus = fixnan(100 * ta.rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)
// DMA 라인출력
plot(adx, color=color.new(color.white, 0), title="ADX - 추세의 강도", linewidth=2)
plot(plus, color=color.new(color.green, 0), title="+DI - 매수세", linewidth=2)
plot(minus, color=color.new(color.red, 0), title="-DI - 매도세", linewidth=2)
DMI의 기본 기간은 14일로 설정을 추천드리며
ADX는 추세의 강도를 나타냅니다.
plus는 매수세를 나타내고 minus는 매도세를 나타냅니다.
5. DMI(방향 이동 지수) 횡보장,상승장,하락장 배경만들기
// 횡보장
noneTrend = adx < 20
// 상승장
longTrend = adx > 20 and plus > minus
// 하락장
shortTrend = adx > 20 and plus < minus
// DMI 추세장 출력
bgcolor(noneTrend? color.new(color.orange, 70) : na)
bgcolor(longTrend? color.new(color.green, 70) : na)
bgcolor(shortTrend? color.new(color.red, 70) : na)
배경을 만들기 이전에 간단한 조건을 만들어보겠습니다.
횡보장은 ADX가 20미만일때라고 정의하였습니다.
상승장은 ADX가 20이상이며 매수세가 매도세를 이길때라고 정의하였습니다.
하락장은 ADX가 20이상이며 매도세가 매수세를 이길때라고 정의하였습니다.
6. DMI(방향 이동 지수) 최종 코드
//https://goingrunv.com
//@version=5
// 타이틀
indicator(title="GRV-DMI", shorttitle="GRV-DMI", format=format.price, precision=4, timeframe="", timeframe_gaps=true)
// DMI 함수정의
lensig = input.int(14, title="ADX 스무딩", minval=1, maxval=50)
len = input.int(14, minval=1, title="DI 길이")
up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
trur = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / trur)
minus = fixnan(100 * ta.rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)
// DMA 라인출력
plot(adx, color=color.new(color.white, 0), title="ADX - 추세의 강도", linewidth=2)
plot(plus, color=color.new(color.green, 0), title="+DI - 매수세", linewidth=2)
plot(minus, color=color.new(color.red, 0), title="-DI - 매도세", linewidth=2)
// 횡보장
noneTrend = adx < 20
// 상승장
longTrend = adx > 20 and plus > minus
// 하락장
shortTrend = adx > 20 and plus < minus
// DMI 추세장 출력
bgcolor(noneTrend? color.new(color.orange, 70) : na)
bgcolor(longTrend? color.new(color.green, 70) : na)
bgcolor(shortTrend? color.new(color.red, 70) : na)
'파인 스크립트 지표 만들기 > 기본 보조지표' 카테고리의 다른 글
파인스크립트로 MA(이동평균선) 지표를 만들어보자 (0) | 2022.07.13 |
---|
댓글0