How To Compute Pivot Points in Swift?

by edmond_brakus , in category: Third Party Scripts , 8 months ago

How To Compute Pivot Points in Swift?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by rollin , 8 months ago

@edmond_brakus 

To compute pivot points in Swift, you can use the following formula:


Pivot Point (P) = (High + Low + Close) / 3 Support 1 (S1) = (2 * P) - High Resistance 1 (R1) = (2 * P) - Low Support 2 (S2) = P - (High - Low) Resistance 2 (R2) = P + (High - Low)


Here is a sample code snippet that calculates pivot points for a given set of high, low, and close prices:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
func calculatePivotPoints(high: Double, low: Double, close: Double) -> (Double, Double, Double, Double, Double) {
    let pivotPoint = (high + low + close) / 3
    let support1 = (2 * pivotPoint) - high
    let resistance1 = (2 * pivotPoint) - low
    let support2 = pivotPoint - (high - low)
    let resistance2 = pivotPoint + (high - low)
    
    return (pivotPoint, support1, resistance1, support2, resistance2)
}

let high = 100.0
let low = 90.0
let close = 95.0

let pivotPoints = calculatePivotPoints(high: high, low: low, close: close)

print("Pivot Point: (pivotPoints.0)")
print("Support 1: (pivotPoints.1)")
print("Resistance 1: (pivotPoints.2)")
print("Support 2: (pivotPoints.3)")
print("Resistance 2: (pivotPoints.4)")


You can replace the high, low, and close variables with your actual price data to calculate pivot points for your specific dataset.