378 просмотров
```study("ADX and DI improved")
len1 = input(title="Length", type=integer, defval=20)
th1 = input(title="threshold", type=integer, defval=20)

TrueRange1 = max(max(high-low, abs(high-nz(close))), abs(low-nz(close)))
DirectionalMovementPlus1 = high-nz(high) > nz(low)-low ? max(high-nz(high), 0): 0
DirectionalMovementMinus1 = nz(low)-low > high-nz(high) ? max(nz(low)-low, 0): 0

SmoothedTrueRange1 = nz(SmoothedTrueRange1) - (nz(SmoothedTrueRange1)/len1) + TrueRange1
SmoothedDirectionalMovementPlus1 = nz(SmoothedDirectionalMovementPlus1) - (nz(SmoothedDirectionalMovementPlus1)/len1) + DirectionalMovementPlus1
SmoothedDirectionalMovementMinus1 = nz(SmoothedDirectionalMovementMinus1) - (nz(SmoothedDirectionalMovementMinus1)/len1) + DirectionalMovementMinus1

DIPlus1 = SmoothedDirectionalMovementPlus1 / SmoothedTrueRange1 * 100
DIMinus1 = SmoothedDirectionalMovementMinus1 / SmoothedTrueRange1 * 100
DX1 = abs(DIPlus1-DIMinus1) / (DIPlus1+DIMinus1)*100

// TEMA
ema1 = ema(DX1, 14)
ema2 = ema(ema1, 14)
ema3 = ema(ema2, 14)
ADX1 = 3 * (ema1 - ema2) + ema3

plot(DIPlus1, color=green, title="DI+")
plot(DIMinus1, color=red, title="DI-")
hline(th1, color=black, linestyle=dashed)

// ---

len2 = input(title="Length", type=integer, defval=18)
th2 = input(title="threshold", type=integer, defval=20)

TrueRange2 = max(max(high-low, abs(high-nz(close))), abs(low-nz(close)))
DirectionalMovementPlus2 = high-nz(high) > nz(low)-low ? max(high-nz(high), 0): 0
DirectionalMovementMinus2 = nz(low)-low > high-nz(high) ? max(nz(low)-low, 0): 0

SmoothedTrueRange2 = nz(SmoothedTrueRange2) - (nz(SmoothedTrueRange2)/len2) + TrueRange2
SmoothedDirectionalMovementPlus2 = nz(SmoothedDirectionalMovementPlus2) - (nz(SmoothedDirectionalMovementPlus2)/len2) + DirectionalMovementPlus2
SmoothedDirectionalMovementMinus2 = nz(SmoothedDirectionalMovementMinus2) - (nz(SmoothedDirectionalMovementMinus2)/len2) + DirectionalMovementMinus2

DIPlus2 = SmoothedDirectionalMovementPlus2 / SmoothedTrueRange2 * 100
DIMinus2 = SmoothedDirectionalMovementMinus2 / SmoothedTrueRange2 * 100
DX2 = abs(DIPlus2-DIMinus2) / (DIPlus2+DIMinus2)*100

plot(DIPlus2, color=green, title="DI+")
plot(DIMinus2, color=red, title="DI-")

// ---

len3 = input(title="Length", type=integer, defval=16)
th3 = input(title="threshold", type=integer, defval=20)

TrueRange3 = max(max(high-low, abs(high-nz(close))), abs(low-nz(close)))
DirectionalMovementPlus3 = high-nz(high) > nz(low)-low ? max(high-nz(high), 0): 0
DirectionalMovementMinus3 = nz(low)-low > high-nz(high) ? max(nz(low)-low, 0): 0

SmoothedTrueRange3 = nz(SmoothedTrueRange3) - (nz(SmoothedTrueRange3)/len3) + TrueRange3
SmoothedDirectionalMovementPlus3 = nz(SmoothedDirectionalMovementPlus3) - (nz(SmoothedDirectionalMovementPlus3)/len3) + DirectionalMovementPlus3
SmoothedDirectionalMovementMinus3 = nz(SmoothedDirectionalMovementMinus3) - (nz(SmoothedDirectionalMovementMinus3)/len3) + DirectionalMovementMinus3

DIPlus3 = SmoothedDirectionalMovementPlus3 / SmoothedTrueRange3 * 100
DIMinus3 = SmoothedDirectionalMovementMinus3 / SmoothedTrueRange3 * 100
DX3 = abs(DIPlus3-DIMinus3) / (DIPlus3+DIMinus3)*100

plot(DIPlus3, color=green, title="DI+")
plot(DIMinus3, color=red, title="DI-")

// ---

len4 = input(title="Length", type=integer, defval=14)
th4 = input(title="threshold", type=integer, defval=20)

TrueRange4 = max(max(high-low, abs(high-nz(close))), abs(low-nz(close)))
DirectionalMovementPlus4 = high-nz(high) > nz(low)-low ? max(high-nz(high), 0): 0
DirectionalMovementMinus4 = nz(low)-low > high-nz(high) ? max(nz(low)-low, 0): 0

SmoothedTrueRange4 = nz(SmoothedTrueRange4) - (nz(SmoothedTrueRange4)/len4) + TrueRange4
SmoothedDirectionalMovementPlus4 = nz(SmoothedDirectionalMovementPlus4) - (nz(SmoothedDirectionalMovementPlus4)/len4) + DirectionalMovementPlus4
SmoothedDirectionalMovementMinus4 = nz(SmoothedDirectionalMovementMinus4) - (nz(SmoothedDirectionalMovementMinus4)/len4) + DirectionalMovementMinus4

DIPlus4 = SmoothedDirectionalMovementPlus4 / SmoothedTrueRange4 * 100
DIMinus4 = SmoothedDirectionalMovementMinus4 / SmoothedTrueRange4 * 100
DX4 = abs(DIPlus4-DIMinus4) / (DIPlus4+DIMinus4)*100

plot(DIPlus4, color=green, title="DI+")
plot(DIMinus4, color=red, title="DI-")

// ---

len5 = input(title="Length", type=integer, defval=12)
th5 = input(title="threshold", type=integer, defval=20)

TrueRange5 = max(max(high-low, abs(high-nz(close))), abs(low-nz(close)))
DirectionalMovementPlus5 = high-nz(high) > nz(low)-low ? max(high-nz(high), 0): 0
DirectionalMovementMinus5 = nz(low)-low > high-nz(high) ? max(nz(low)-low, 0): 0

SmoothedTrueRange5 = nz(SmoothedTrueRange5) - (nz(SmoothedTrueRange5)/len5) + TrueRange5
SmoothedDirectionalMovementPlus5 = nz(SmoothedDirectionalMovementPlus5) - (nz(SmoothedDirectionalMovementPlus5)/len5) + DirectionalMovementPlus5
SmoothedDirectionalMovementMinus5 = nz(SmoothedDirectionalMovementMinus5) - (nz(SmoothedDirectionalMovementMinus5)/len5) + DirectionalMovementMinus5

DIPlus5 = SmoothedDirectionalMovementPlus5 / SmoothedTrueRange5 * 100
DIMinus5 = SmoothedDirectionalMovementMinus5 / SmoothedTrueRange5 * 100
DX5 = abs(DIPlus5-DIMinus5) / (DIPlus5+DIMinus5)*100

plot(DIPlus5, color=green, title="DI+")
plot(DIMinus5, color=red, title="DI-")

// ---

len6 = input(title="Length", type=integer, defval=10)
th6 = input(title="threshold", type=integer, defval=20)

TrueRange6 = max(max(high-low, abs(high-nz(close))), abs(low-nz(close)))
DirectionalMovementPlus6 = high-nz(high) > nz(low)-low ? max(high-nz(high), 0): 0
DirectionalMovementMinus6 = nz(low)-low > high-nz(high) ? max(nz(low)-low, 0): 0

SmoothedTrueRange6 = nz(SmoothedTrueRange6) - (nz(SmoothedTrueRange6)/len6) + TrueRange6
SmoothedDirectionalMovementPlus6 = nz(SmoothedDirectionalMovementPlus6) - (nz(SmoothedDirectionalMovementPlus6)/len6) + DirectionalMovementPlus6
SmoothedDirectionalMovementMinus6 = nz(SmoothedDirectionalMovementMinus6) - (nz(SmoothedDirectionalMovementMinus6)/len6) + DirectionalMovementMinus6

DIPlus6 = SmoothedDirectionalMovementPlus6 / SmoothedTrueRange6 * 100
DIMinus6 = SmoothedDirectionalMovementMinus6 / SmoothedTrueRange6 * 100
DX6 = abs(DIPlus6-DIMinus6) / (DIPlus6+DIMinus6)*100

plot(DIPlus6, color=green, title="DI+")
plot(DIMinus6, color=red, title="DI-")

// ---

len7 = input(title="Length", type=integer, defval=8)
th7 = input(title="threshold", type=integer, defval=20)

TrueRange7 = max(max(high-low, abs(high-nz(close))), abs(low-nz(close)))
DirectionalMovementPlus7 = high-nz(high) > nz(low)-low ? max(high-nz(high), 0): 0
DirectionalMovementMinus7 = nz(low)-low > high-nz(high) ? max(nz(low)-low, 0): 0

SmoothedTrueRange7 = nz(SmoothedTrueRange7) - (nz(SmoothedTrueRange7)/len7) + TrueRange7
SmoothedDirectionalMovementPlus7 = nz(SmoothedDirectionalMovementPlus7) - (nz(SmoothedDirectionalMovementPlus7)/len7) + DirectionalMovementPlus7
SmoothedDirectionalMovementMinus7 = nz(SmoothedDirectionalMovementMinus7) - (nz(SmoothedDirectionalMovementMinus7)/len7) + DirectionalMovementMinus7

DIPlus7 = SmoothedDirectionalMovementPlus7 / SmoothedTrueRange7 * 100
DIMinus7 = SmoothedDirectionalMovementMinus7 / SmoothedTrueRange7 * 100
DX7 = abs(DIPlus7-DIMinus7) / (DIPlus7+DIMinus7)*100