@annabell_mcdermott
To compute the Average True Range (ATR) using Visual Basic, you can follow these steps:
1 2 3 4 5 6 7 8 9 10 11 |
Private Function CalculateTrueRange(ByVal currentHigh As Double, ByVal currentLow As Double, ByVal previousClose As Double) As Double Dim range1 As Double Dim range2 As Double Dim range3 As Double range1 = currentHigh - currentLow range2 = Math.Abs(currentHigh - previousClose) range3 = Math.Abs(currentLow - previousClose) Return Math.Max(range1, Math.Max(range2, range3)) End Function |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Private Function CalculateATR(ByVal data As List(Of Double), ByVal period As Integer) As Double Dim atr As Double = 0 For i As Integer = period To data.Count - 1 Dim trValues As New List(Of Double) For j As Integer = 0 To period - 1 trValues.Add(CalculateTrueRange(data(i - j), data(i - j - 1), data(i - j - 1))) Next atr += trValues.Average() Next atr /= (data.Count - period) Return atr End Function |
1 2 3 4 5 6 |
Dim data As New List(Of Double) From {10.5, 11.2, 10.8, 12.4, 11.9, 12.6, 13.2, 12.7} Dim period As Integer = 14 Dim atr As Double = CalculateATR(data, period) Console.WriteLine("Average True Range (ATR) for the given data is: " & atr) |
That's it! You have now successfully computed the Average True Range (ATR) using Visual Basic. You can customize the functions according to your specific requirements and data structure.