How To Compute Williams %R in JavaScript?

Member

by dedrick , in category: Third Party Scripts , 2 months ago

How To Compute Williams %R in JavaScript?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by edmond_brakus , 2 months ago

@dedrick 

To compute Williams %R in JavaScript, you can create a function that takes in an array of price data and a period as input, and then calculate the Williams %R value for each data point. Here's an example code snippet to compute Williams %R:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
function calculateWilliamsR(prices, period) {
    let williamsR = [];

    for (let i = period - 1; i < prices.length; i++) {
        let highestHigh = Math.max(...prices.slice(i - period + 1, i + 1));
        let lowestLow = Math.min(...prices.slice(i - period + 1, i + 1));

        let currentPrice = prices[i];
        let williamsValue = ((highestHigh - currentPrice) / (highestHigh - lowestLow)) * -100;

        williamsR.push(williamsValue);
    }

    return williamsR;
}

// Example usage
let prices = [10, 12, 15, 14, 16, 13, 11, 10];
let period = 5;

let williamsRValues = calculateWilliamsR(prices, period);
console.log(williamsRValues);


In this code snippet, the calculateWilliamsR function loops through the price data array and calculates the highest high and lowest low prices over the specified period. It then computes the Williams %R value for each data point and stores them in an array. Finally, the function returns the array of Williams %R values.


You can adjust the prices array and period variable according to your data and requirements.