How To Calculate Williams %R using Kotlin?

Member

by jamison , in category: SEO Tools , 10 months ago

How To Calculate Williams %R using Kotlin?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by cameron_walter , 10 months ago

@jamison 

To calculate Williams %R in Kotlin, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
fun williamsPercentR(highs: List<Double>, lows: List<Double>, closes: List<Double>, period: Int): List<Double> {
    val result = mutableListOf<Double>()

    for (i in period - 1 until highs.size) {
        val highestHigh = highs.subList(i - period + 1, i + 1).maxOrNull() ?: 0.0
        val lowestLow = lows.subList(i - period + 1, i + 1).minOrNull() ?: 0.0
        val currentClose = closes[i]

        val percentR = ((highestHigh - currentClose) / (highestHigh - lowestLow)) * -100
        result.add(percentR)
    }

    return result
}


This function takes in three lists of high prices, low prices, and closing prices, as well as the period for which you want to calculate the Williams %R. It then loops through the lists to calculate the highest high and lowest low within the specified period, and then calculates the Williams %R based on the formula (highestHigh - currentClose) / (highestHigh - lowestLow) * -100.


You can call this function with your data and desired period to calculate Williams %R for each data point.