How To Calculate Williams %R in TypeScript?

by chasity.halvorson , in category: SEO Tools , a month ago

How To Calculate Williams %R in TypeScript?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by jose_gulgowski , a month ago

@chasity.halvorson 

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

 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 calculateWilliamsR(prices: number[], period: number): number[] {
  const result: number[] = [];

  for (let i = 0; i < prices.length; i++) {
    if (i < period - 1) {
      result.push(null);
    } else {
      const minPrice = Math.min(...prices.slice(i - period + 1, i + 1));
      const maxPrice = Math.max(...prices.slice(i - period + 1, i + 1));
      const currentPrice = prices[i];
      
      const williamsR = ((maxPrice - currentPrice) / (maxPrice - minPrice)) * -100;
      result.push(williamsR);
    }
  }

  return result;
}

// Example usage
const prices = [10, 12, 14, 16, 18, 20, 22];
const period = 5;

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


In this code snippet, the calculateWilliamsR function takes an array of prices and a period as input and calculates the Williams %R values for each price in the array. The function loops through the prices array, calculates the min and max prices over the specified period, and then calculates the Williams %R value using the formula ((maxPrice - currentPrice) / (maxPrice - minPrice)) * -100. Finally, it returns an array of Williams %R values.