Summary: This indicator script measures the strength of a trend and can be used to detect when a market is trending and when it is not. Calculation: Complex, see script. Developer: J. Welles Wilder, Jr. Interpretation: 1. Strong Trend: When the indicator script is moving upwards the underlying indicator trend is becoming stronger. 2. Weak Trend: When the indicator script is moving downwards the underlying indicator trend is becoming weaker.
' Assign the parameters to script variables. _symbolIndex = symbolIndex _periods = periods _firstPeriods = firstPeriods
' Get the highest prices of the symbol, indexed by bar index. Define high() As Number = BarHigh(_symbolIndex, barIndex, length + 2 * _periods + 2 * _firstPeriods) ' Get the lowest prices of the symbol, indexed by bar index. Define low() As Number = BarLow(_symbolIndex, barIndex, length + 2 * _periods + 2 * _firstPeriods) ' Get the closing prices of the symbol, indexed by bar index. Define close() As Number = BarClose(_symbolIndex, barIndex, length + 2 * _periods + 2 * _firstPeriods) ' Use for the plus and minus average directional movement. Define PADM, MADM As Number ' Use for the positive and negative directional index. Define PDI, NDI As Number ' Use for the true range and average true range. Define TR, ATR As Number ' Use for the plus dm and minus dm. Define PDM, MDM As Number ' Use for the calculated Average Directional Movement Index values, indexed by bar index. Define results(length - 1) As Number ' Use for the dx values indexed by bar index. Define dx(length + _periods + _firstPeriods - 1) As Number ' Use for the moving average smooth factor. Define smoothFactor As Number = 1 / _periods ' Calculate the Average Directional Movement Index for the specified bar range. For i As Integer = length + _periods + _firstPeriods - 1 To 0 Step -1 ' Calculate the Average Directional Movement Index for the current bar. PADM = 0 MADM = 0 ATR = 0 For j As Integer = i + _periods + _firstPeriods - 1 To i Step -1 ' Calculate the current true range. If (high(j) > close(j+1)) Then TR = high(j) Else TR = close(j+1) End If If (low(j) < close(j+1)) Then TR -= low(j) Else TR -= close(j+1) End If ' Calculate the plus directional movement. PDM = high(j) - high(j+1) ' Calculate the minus directional movement. MDM = low(j+1) - low(j) If (PDM > MDM Or MDM < 0) Then MDM = 0 End If If (PDM < MDM Or PDM < 0) Then PDM = 0 End If If ((PDM < 0 And MDM < 0) Or PDM = MDM) Then PDM = 0 MDM = 0 End If If (PADM <> 0 Or MADM <> 0 Or ATR <> 0) Then ' Increament the average directional movement and average true range. PADM = (1 - smoothFactor) * PADM + smoothFactor * PDM MADM = (1 - smoothFactor) * MADM + smoothFactor * MDM ATR = (1 - smoothFactor) * ATR + smoothFactor * TR Else PADM = PDM MADM = MDM ATR = TR End If Next ' Calculate the plus directional indicator value for the current bar. If (ATR <> 0) Then PDI = (100 * PADM) / ATR NDI = (100 * MADM) / ATR Else PDI = PADM NDI = MADM End If If (PDI + NDI <> 0) Then dx(i) = MathAbs((PDI - NDI) / (PDI + NDI)) * 100 Else dx(i) = 0 End If Next Define ADX As Number ' Calculate the Average Directional Movement Index values for the specified bar range. For i As Integer = length - 1 To 0 Step -1 ADX = 0 ' Calculate the Average Directional Movement Index value for the current bar. For j As Integer = i + _periods + _firstPeriods - 1 To i Step -1 If (ADX <> 0) Then ADX = (1 - smoothFactor) * ADX + smoothFactor * dx(j) Else ADX = dx(j) End If Next ' Assign the Average Directional Movement Index value for the current bar. results(i) = ADX Next Return results