Saturday, September 04, 2010

Directional Movement Minus [DIM]

Descripton
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.


Variables
TypeIdentifierDescription
Integer_symbolIndexUse for the underlying symbol index on which to calculate the Directional Movement Minus.
Integer_periodsUse for the number of periods to include in the Directional Movement Minus calculation.
Integer_firstPeriodsUse for the number of periods to include in the calculation of the first value of the exponential moving averages.

OnInitialize
Function Parameters
TypeIdentifierDescription
SymbolsymbolIndexUse for the underlying symbol index on which to calculate the Directional Movement Minus.
IntegerperiodsUse for the number of periods to include in the Directional Movement Minus calculation. [Default 10]
IntegerfirstPeriodsUse for the number of periods to include in the calculation of the first value of the exponential moving averages. [Default 0]
Implementation
	 ' Assign the parameters to script variables.
	_symbolIndex = symbolIndex
	_periods = periods
	_firstPeriods = firstPeriods

OnValues
Function Parameters
TypeIdentifierDescription
IntegerbarIndex
Integerlength
Implementation
	 ' 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

Copyright © 2010 IQBroker, LLC. All rights reserved.