How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Feature Request FAQ Help & Wiki Twitter

452 0
Average Directional Index is a way to determine trend strength (and whether there is a trend). One of the caveats is when you have trendless conditions on a higher timeframe. You can get whipsaw conditions on the current timeframe in that case.

The purpose of this script is to put ADX into the context of a higher degree trend on a single indicator.
```//@version=2
//
// Show ADX for current timeframe and next "Degree" higher
// Daily shows Weekly, Weekly shows Monthly, Monthly shows Quarterly, and Intraday uses a 4x multiple
// if ADX is below 20, it could be sideways choppy conditions
dilen = input(14, title="DI Length")
choplevel = input(20, title="Chop Level", minval=1)
// get current resolution
res = interval
upper_tf = isintraday ? res >= 240 ? '2D' : tostring(res * 4) : isdaily ? 'W' : isweekly ? 'M' : ismonthly ? '3M' : 'D'
//upper_tf = 'W'
multiple = isintraday ? 4 : isdaily ? 5 : isweekly ? 4 : ismonthly ? 3 : 1

get_tr(cur_high, cur_low, prior_close) =>
range1 = abs(cur_high - cur_low)
range2 = abs(cur_high - prior_close)
range3 = abs(cur_low - prior_close)
max1 = max(range1,range2)
get_tr = max(max1,range3)

get_vals(tf) =>
tf_high = tf == 0 ? high : security(ticker,upper_tf,high)
tf_low = tf == 0 ? low : security(ticker,upper_tf,low)
tf_close = tf == 0 ? close : security(ticker,upper_tf,close)
tf_tr = tf == 0 ? tr : get_tr(tf_high,tf_low,tf_close[1])
[tf_high,tf_low,tf_tr]

dirmov(len,tf) =>
[tf_high, tf_low, tf_tr] = get_vals(tf)
up = change(tf_high)
down = -change(tf_low)
truerange = rma(tf_tr, len)
plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange)
minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange)
[plus, minus]

[plus, minus] = dirmov(dilen,tf)
sum = plus + minus
adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)

hline(choplevel,color=black)```
United States
United Kingdom
India
España
France
Italia
Polska
Brasil
Россия
Türkiye
Indonesia

한국
Home Stock Screener Forex Signal Finder Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter