Summary: This indicator script measures the negative part of the current period's price range that is lower than the previous period's price range of an underlying symbol. Calculation: Complex, see script. Developer: J. Welles Wilder, Jr. Interpretation: 1. Upwards Trend: When the indicator script is moving upwards, the underlying indicator is in a downwards trend.
' Assign the parameters to script variables. _symbolIndex = symbolIndex _periods = periods _firstPeriods = firstPeriods
' Get the highest bar prices of the symbol, indexed by bar index. Define high() As Number = BarHigh(_symbolIndex, barIndex, length + _periods + _firstPeriods) ' Get the lowest bar prices of the symbol, indexed by bar index. Define low() As Number = BarLow(_symbolIndex, barIndex, length + _periods + _firstPeriods) ' Get the closing bar prices of the symbol, indexed by bar index. Define close() As Number = BarClose(_symbolIndex, barIndex, length + _periods + _firstPeriods) ' Use for the true range. Define TR As Number ' Use for the average directional movement. Define ADM As Number ' Use for the average true range. Define ATR As Number ' Use for the plus and minus dm. Define PDM, MDM As Number ' Use for the Directional Movement Minus indicator values, indexed by bar index. Define results(length - 1) As Number ' Use for the smooth factor Define smoothFactor As Number = 1 / _periods ' Calculate the Directional Movement Minus values for the specified bar range. For i As Integer = length - 1 To 0 Step -1 ' Calculate the Directional Movement Minus value for the current bar. ADM = 0 ATR = 0 For j As Integer = i + _periods + _firstPeriods - 1 To i Step -1 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 (ADM <> 0 Or ATR <> 0) Then ADM = (1 - smoothFactor) * ADM + smoothFactor * MDM ATR = (1 - smoothFactor) * ATR + smoothFactor * TR Else ADM = MDM ATR = TR End If Next If (ATR <> 0) Then results(i) = MathRound((100 * ADM) / ATR, 0) Else results(i) = MathRound(ADM, 0) End If Next Return results