How To Compute Parabolic SAR (Stop and Reverse) in Groovy?

by jaycee_rowe , in category: SEO Tools , 9 months ago

How To Compute Parabolic SAR (Stop and Reverse) in Groovy?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by zion , 9 months ago

@jaycee_rowe 

Here is an example code snippet in Groovy to compute Parabolic SAR (Stop and Reverse) :

 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
54
55
56
57
def computeSAR(def high, def low, def acceleration, def maxAcceleration) {
    def sar = []
    
    // initial values
    def sarPrev = low[1]
    def acc = acceleration
    def extremePoint = high[0]
    def trend = 1
    
    // loop through the data to calculate SAR
    for (int i = 2; i < high.size(); i++) {
        if (trend == 1) {
            if (low[i] < sarPrev) {
                sar.add(sarPrev)
                sarPrev = extremePoint
                acc = acceleration
                trend = -1
                extremePoint = low[i]
            } else {
                def sarCurrent = sarPrev + acc * (extremePoint - sarPrev)
                sar.add(sarCurrent)
                
                if (high[i] > extremePoint) {
                    extremePoint = high[i]
                    acc += acceleration
                }
            }
        } else {
            if (high[i] > sarPrev) {
                sar.add(sarPrev)
                sarPrev = extremePoint
                acc = acceleration
                trend = 1
                extremePoint = high[i]
            } else {
                def sarCurrent = sarPrev - acc * (sarPrev - extremePoint)
                sar.add(sarCurrent)
                
                if (low[i] < extremePoint) {
                    extremePoint = low[i]
                    acc += acceleration
                } 
            }
        }
    }
    
    return sar
}

// Example usage
def high = [10, 11, 12, 13, 14, 15, 16]
def low = [9, 10, 11, 12, 13, 14, 15]
def acceleration = 0.02
def maxAcceleration = 0.2

def sar = computeSAR(high, low, acceleration, maxAcceleration)
println sar


This code calculates the Parabolic SAR values based on the high and low prices of a financial instrument, as well as the acceleration and maxAcceleration parameters. The function computeSAR returns an array of Parabolic SAR values. You can customize the input data and parameters as needed for your specific use case.