How To Calculate Parabolic SAR (Stop and Reverse) in Swift?

Member

by adolf , in category: General Help , 2 months ago

How To Calculate Parabolic SAR (Stop and Reverse) in Swift?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by keegan.towne , 2 months ago

@adolf 

Parabolic SAR (Stop and Reverse) is a technical indicator used for determining the future direction of a security's price movement. It is calculated by tracking the highest and lowest prices of the security over a specific period of time and then plotting them on the chart. Here is how you can calculate Parabolic SAR in Swift:

 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
func calculateParabolicSAR(data: [Double], accelerationFactor: Double, maximumAcceleration: Double) -> [Double] {
    var sarValues = [Double](repeating: 0.0, count: data.count)
    
    var sar = data[0]
    var acceleration = accelerationFactor
    var extremePoint = data[0]
    var isUpTrend = true
    
    for i in 1..<data.count {
        let previousSar = sar
        
        if isUpTrend {
            if data[i] > extremePoint {
                extremePoint = data[i]
                acceleration = min(acceleration + accelerationFactor, maximumAcceleration)
            }
            
            sar += acceleration * (extremePoint - sar)
            
            if sar > data[i] {
                sar = extremePoint
                acceleration = accelerationFactor
                isUpTrend = false
            }
            
        } else {
            if data[i] < extremePoint {
                extremePoint = data[i]
                acceleration = min(acceleration + accelerationFactor, maximumAcceleration)
            }
            
            sar += acceleration * (extremePoint - sar)
            
            if sar < data[i] {
                sar = extremePoint
                acceleration = accelerationFactor
                isUpTrend = true
            }
        }
        
        sarValues[i] = sar
    }
    
    return sarValues
}

// Example usage
let data = [10.0, 11.0, 12.0, 14.0, 15.0, 16.0, 18.0]
let accelerationFactor = 0.02
let maximumAcceleration = 0.20

let sarValues = calculateParabolicSAR(data: data, accelerationFactor: accelerationFactor, maximumAcceleration: maximumAcceleration)
print(sarValues)


In this code snippet, the calculateParabolicSAR function takes an array of price data, an acceleration factor, and a maximum acceleration as input parameters, and returns an array of Parabolic SAR values. The function iterates through the price data, updating the SAR value based on the current trend direction and acceleration factor. The SAR values are then stored in an array and returned.


You can modify the data, accelerationFactor, and maximumAcceleration variables to test the function with different price data and parameters. The output will be an array of Parabolic SAR values for each data point.