How To Calculate Williams %R in Go?

by coty_beier , in category: General Help , 9 months ago

How To Calculate Williams %R in Go?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by enrico , 9 months ago

@coty_beier 

To calculate Williams %R in Go, you can use the following formula:


Williams %R = (Highest High - Close) / (Highest High - Lowest Low) * -100


Here is a sample code snippet in Go to calculate Williams %R:

 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
package main

import (
    "fmt"
)

func williamsR(high []float64, low []float64, close []float64, period int) []float64 {
    var result []float64

    for i := period; i < len(high); i++ {
        highestHigh := findMax(high[i-period : i])
        lowestLow := findMin(low[i-period : i])
        wR := ((highestHigh - close[i]) / (highestHigh - lowestLow)) * -100
        result = append(result, wR)
    }

    return result
}

func findMax(arr []float64) float64 {
    max := arr[0]
    for _, value := range arr {
        if value > max {
            max = value
        }
    }
    return max
}

func findMin(arr []float64) float64 {
    min := arr[0]
    for _, value := range arr {
        if value < min {
            min = value
        }
    }
    return min
}

func main() {
    high := []float64{10.0, 12.0, 14.0, 15.0, 16.0, 14.0, 12.0}
    low := []float64{8.0, 10.0, 12.0, 13.0, 14.0, 12.0, 10.0}
    close := []float64{9.0, 11.0, 13.0, 14.0, 15.0, 13.0, 11.0}
    period := 5

    result := williamsR(high, low, close, period)
    fmt.Println("Williams %R values:", result)
}


In this code snippet, we define a function williamsR that calculates Williams %R for each period based on the high, low, and close prices. We also define helper functions findMax and findMin to find the maximum and minimum values in a given array.


You can input your own high, low, and close price values and the period length in the main function to calculate Williams %R for your specific data set.