How To Calculate Parabolic SAR (Stop and Reverse) using Visual Basic?

by filiberto , in category: Third Party Scripts , 10 months ago

How To Calculate Parabolic SAR (Stop and Reverse) using Visual Basic?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 10 months ago

@filiberto 

To calculate Parabolic SAR using Visual Basic, you can follow these steps:

  1. Define the initial acceleration factor (AF), starting value for SAR, and other necessary variables:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Dim AF As Double
Dim SAR As Double
Dim EP As Double
Dim high As Double
Dim low As Double
Dim direction As Integer
Dim previous_direction As Integer

AF = 0.02
SAR = High ' use the current high as starting value for SAR
EP = Low ' use the current low as the extreme point
direction = 1 ' initialize the direction as upward
previous_direction = -1 ' initialize the previous direction as downward


  1. Calculate the SAR value for each data point:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
For i = 1 To DataPoints.Count
    If direction = 1 Then ' calculate SAR for uptrend
        If DataPoints(i).Low < SAR Then
            direction = -1 ' change direction to downward
            SAR = High
            EP = Low
            AF = 0.02
        End If
    ElseIf direction = -1 Then ' calculate SAR for downtrend
        If DataPoints(i).High > SAR Then
            direction = 1 ' change direction to upward
            SAR = Low
            EP = High
            AF = 0.02
        End If
    End If
    
    If direction = 1 Then ' calculate SAR for uptrend
        If DataPoints(i).High > EP Then
            EP = DataPoints(i).High
            AF = AF + 0.02
            If AF > 0.2 Then
                AF = 0.2
            End If
        End If
    ElseIf direction = -1 Then ' calculate SAR for downtrend
        If DataPoints(i).Low < EP Then
            EP = DataPoints(i).Low
            AF = AF + 0.02
            If AF > 0.2 Then
                AF = 0.2
            End If
        End If
    End If
    
    If direction = 1 Then ' calculate SAR for uptrend
        If i > 1 And DataPoints(i).High > DataPoints(i - 1).High Then
            AF = AF + 0.02
            If AF > 0.2 Then
                AF = 0.2
            End If
        End If
    ElseIf direction = -1 Then ' calculate SAR for downtrend
        If i > 1 And DataPoints(i).Low < DataPoints(i - 1).Low Then
            AF = AF + 0.02
            If AF > 0.2 Then
                AF = 0.2
            End If
        End If
    End If
    
    SAR = SAR + AF * (EP - SAR)
Next i


  1. Display or use the calculated SAR value for each data point:
1
2
3
For i = 1 To DataPoints.Count
    Console.WriteLine("SAR for data point " & i & ": " & SAR)
Next i


By following these steps, you can calculate the Parabolic SAR values using Visual Basic for your trading or analysis purposes.