@albert
Here is an example code snippet showing how to compute the Williams %R indicator in TypeScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function calculateWilliamsR(high: number[], low: number[], close: number[], period: number): number[] {
const result: number[] = [];
for (let i = period - 1; i < high.length; i++) {
const maxHigh = Math.max(...high.slice(i - period + 1, i + 1));
const minLow = Math.min(...low.slice(i - period + 1, i + 1));
const currentClose = close[i];
const williamsR = ((maxHigh - currentClose) / (maxHigh - minLow)) * -100;
result.push(williamsR);
}
return result;
}
// Example usage
const high = [30, 35, 40, 45, 50, 55, 60];
const low = [20, 25, 30, 35, 40, 45, 50];
const close = [25, 30, 35, 40, 45, 50, 55];
const period = 5;
const williamsRValues = calculateWilliamsR(high, low, close, period);
console.log(williamsRValues); // Output: [-100, -75, -50, -25, 0]
|
In this code snippet, the calculateWilliamsR function takes in arrays representing high, low, and close prices, as well as the period for the Williams %R calculation. The function then calculates the Williams %R values based on the provided data and returns an array of the computed values.