ADX+DMI MTF byPeterO

Peter_O · study · 56 行 · 点赞 1,263 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=3
study(title="ADX+DMI MTF byPeterO", shorttitle="ADX+DMI MTF byPeterO")
//The goal of this study was to use ADX from Higher Timeframe - to determine trend direction
//Why? Because ADX is very sensitive, able to show trend ending without any delay, but not in the middle of it.
//Being able to see such immediate trend change on higher timeframe, is a great indicator of trend direction.
//Adding just security() calls to 'highest', 'lowest' and 'close' didn't seem right, because it produced some ugly ADX, D+ and D- plotlines.
//I wanted to see plotlines, which look exactly like those on actual higher timeframe.
//Therefore I modified the calculations.
//
//On top of all that, I added interpretation of DMI readings, because it is not as simple as plus>minus + ADXrising = uptrend.
//So GREEN background means higher timeframe uptrend and RED background means downtrend.
 
 
lenadx = input(14, minval=1, title="DI Length")
lensig = input(14, title="ADX Smoothing", minval=1, maxval=50)
 
mtf=input(15, title='MTF for ADX trend')
up_mtf=highest(high,mtf)-highest(high,mtf)[mtf]
down_mtf=-(lowest(low,mtf)-lowest(low,mtf)[mtf])
plusDM_mtf = na(up_mtf) ? na : (up_mtf > down_mtf and up_mtf > 0 ? up_mtf : 0)
minusDM_mtf = na(down_mtf) ? na : (down_mtf > up_mtf and down_mtf > 0 ? down_mtf : 0)
tr_mtf = max(max(high-low, abs(high-nz(close[mtf]))), abs(low-nz(close[mtf])))
trur_mtf = rma(tr_mtf, lenadx*mtf)
plus_mtf = fixnan(100 * rma(plusDM_mtf, lenadx*mtf) / trur_mtf)
minus_mtf = fixnan(100 * rma(minusDM_mtf, lenadx*mtf) / trur_mtf)
sum_mtf = plus_mtf + minus_mtf
adx_mtf = 100 * rma(abs(plus_mtf - minus_mtf) / (sum_mtf == 0 ? 1 : sum_mtf), lensig*mtf)
 
plot(plus_mtf, color=green, title="+DI MTF")
plot(minus_mtf, color=red, title="-DI MTF")
plot(adx_mtf, color=black, title="ADX MTF")
hline(25)
 
// Interpreting ADX to find trend direction
colorbg=input(true, title='Color bacground to show direction')
adx_mtf_direction=50, adx_mtf_direction:=nz(adx_mtf_direction[1])
adx_mtf_top_enduptrend=not falling(adx_mtf,1)[1] and falling(adx_mtf,1) and valuewhen(rising(adx_mtf,1),plus_mtf,0)>valuewhen(rising(adx_mtf,1),minus_mtf,0)
adx_mtf_top_enddowntrend=not falling(adx_mtf,1)[1] and falling(adx_mtf,1) and valuewhen(rising(adx_mtf,1),minus_mtf,0)>valuewhen(rising(adx_mtf,1),plus_mtf,0)
adx_mtf_bottom_enduptrend=not rising(adx_mtf,1)[1] and rising(adx_mtf,1) and minus_mtf>plus_mtf
adx_mtf_bottom_enddowntrend=not rising(adx_mtf,1)[1] and rising(adx_mtf,1) and plus_mtf>minus_mtf
adx_mtf_switch_endofuptrend=rising(adx_mtf,1) and crossover(minus_mtf,plus_mtf)
adx_mtf_switch_endofdowntrend=rising(adx_mtf,1) and crossover(plus_mtf,minus_mtf)
 
if adx_mtf_top_enduptrend
    adx_mtf_direction:=20
if adx_mtf_top_enddowntrend
    adx_mtf_direction:=80
if adx_mtf_bottom_enduptrend
    adx_mtf_direction:=20
if adx_mtf_bottom_enddowntrend
    adx_mtf_direction:=80
if adx_mtf_switch_endofuptrend
    adx_mtf_direction:=20
if adx_mtf_switch_endofdowntrend
    adx_mtf_direction:=80
bgcolor(adx_mtf_direction==20 ? red : adx_mtf_direction==80 ? lime : na, title='ADX Trend')

← 返回列表