,

@connie.heaney

Rate of Change (ROC) is a financial indicator that measures the rate at which a stock's price is changing over a specific period of time. To calculate ROC in JavaScript, you can use the following formula: ROC = ((Closing Price - Closing Price N periods ago) / Closing Price N periods ago) * 100

Here is a sample code snippet to calculate ROC in JavaScript:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Sample array of closing prices const closingPrices = [100, 105, 110, 115, 120, 125, 130]; // Number of periods to calculate ROC const n = 3; function calculateROC(closingPrices, n) { const roc = []; for (let i = n; i < closingPrices.length; i++) { const currentPrice = closingPrices[i]; const previousPrice = closingPrices[i - n]; const rocValue = ((currentPrice - previousPrice) / previousPrice) * 100; roc.push(rocValue); } return roc; } const rocValues = calculateROC(closingPrices, n); console.log(rocValues); |

In this code snippet, we first define an array `closingPrices`

that contains the closing prices of a stock over a period of time. We also define the number of periods `n`

over which we want to calculate ROC.

We then define a function `calculateROC`

that takes the closing prices array and number of periods as input parameters. Inside the function, we iterate over the closing prices array and calculate the ROC value for each period using the formula mentioned above. The calculated ROC values are then stored in an array and returned by the function.

Finally, we call the `calculateROC`

function with the sample closing prices array and number of periods, and log the calculated ROC values to the console.

15