How To Compute Average Directional Index (ADX) using Visual Basic?

by gabrielle.kub , in category: General Help , 2 months ago

How To Compute Average Directional Index (ADX) using Visual Basic?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jewel , 2 months ago

@gabrielle.kub 

To compute the Average Directional Index (ADX) using Visual Basic, you can use the following steps:

  1. First, import the necessary libraries for performing mathematical calculations in Visual Basic. You can do this by including the following line at the beginning of your code:
1
Imports System.Math


  1. Create a function to calculate the True Range (TR) for each period. The True Range is calculated as the maximum of the high and low price differences, high and previous close price differences, or low and previous close price differences. Here is an example function to calculate the True Range:
1
2
3
4
5
6
7
8
9
Function TrueRange(high As Double, low As Double, previousClose As Double) As Double
    Dim tr1 As Double = Abs(high - low)
    Dim tr2 As Double = Abs(high - previousClose)
    Dim tr3 As Double = Abs(low - previousClose)
    
    Dim trueRange As Double = Max(Max(tr1, tr2), tr3)
    
    Return trueRange
End Function


  1. Next, create a function to calculate the Directional Movement (DM) for each period. The DM is calculated as the difference between the current high and the previous high (positive directional movement) or the difference between the previous low and the current low (negative directional movement). Here is an example function to calculate the Directional Movement:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Function DirectionalMovement(high As Double, low As Double, previousHigh As Double, previousLow As Double) As Double
    Dim positiveDM As Double = high - previousHigh
    Dim negativeDM As Double = previousLow - low
    
    If positiveDM > negativeDM Then
        Return positiveDM
    Else
        Return 0
    End If
End Function


  1. Then, create a function to calculate the Average True Range (ATR) for each period. The ATR is calculated as the smoothed average of the True Range over a specified period. Here is an example function to calculate the Average True Range:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Function AverageTrueRange(trueRangeValues As List(Of Double), period As Integer) As Double
    Dim atr As Double = 0
    
    If trueRangeValues.Count >= period Then
        Dim sum As Double = 0
        
        For i As Integer = 0 To period - 1
            sum += trueRangeValues(i)
        Next
        
        atr = sum / period
    End If
    
    Return atr
End Function


  1. Finally, create a function to calculate the Average Directional Index (ADX) using the calculated DM and ATR values. The ADX is calculated as the smoothed average of the directional movement index over a specified period. Here is an example function to calculate the Average Directional Index:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Function AverageDirectionalIndex(positiveDMValues As List(Of Double), negativeDMValues As List(Of Double), atrValues As List(Of Double), period As Integer) As Double
    Dim diPlus As Double = (SMA(positiveDMValues, period) / SMA(atrValues, period)) * 100
    Dim diMinus As Double = (SMA(negativeDMValues, period) / SMA(atrValues, period)) * 100
    
    Dim dx As Double = Abs(diPlus - diMinus) / (diPlus + diMinus) * 100
    
    Dim adx As Double = SMA(dx, period)
    
    Return adx
End Function


Note: In the above examples, SMA represents the simple moving average function that calculates the average of a list of values over a specified period.


You can use these functions to calculate the Average Directional Index (ADX) in Visual Basic by passing the necessary input data (high, low, close prices) and the specified period for the calculations.