How To Compute Williams %R in JavaScript?

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

How To Compute Williams %R in JavaScript?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by cecelia , 2 months ago

@caesar_kertzmann 

To compute Williams %R in JavaScript, you can use the following code snippet. Williams %R is a momentum indicator that measures overbought or oversold levels of an asset.

 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
function williamsR(highs, lows, closes, period) {
    let williamsRValues = [];

    for (let i = period - 1; i < highs.length; i++) {
        let highestHigh = Math.max(...highs.slice(i - period + 1, i + 1));
        let lowestLow = Math.min(...lows.slice(i - period + 1, i + 1));
        
        let currentClose = closes[i];
        let williamsR = ((highestHigh - currentClose) / (highestHigh - lowestLow)) * -100;
        
        williamsRValues.push(williamsR);
    }

    return williamsRValues;
}

// Example usage
let highs = [25, 30, 35, 40, 45];
let lows = [20, 25, 30, 35, 40];
let closes = [22, 28, 33, 38, 43];
let period = 5;

let williamsRValues = williamsR(highs, lows, closes, period);

console.log(williamsRValues);


You can input the high, low, and close prices of the asset in arrays, specify the period for calculation, and then call the williamsR function to get the Williams %R values for the given data.