ROC Calculation

by Rajesh Danabal

JavaScript

const prices = [100, 101, 102, 103, 104, 105, 106, 105, 102, 98, 100, 104, 107, 110, 115, 125];
const n = 10;

for (let i = 0; i <= prices.length - 1; i++) {
  if (i >= n) {

    const closePrice = prices[i];
    const closePriceNthPeriod = prices[i - n];
    const roc = ((closePrice - closePriceNthPeriod) / closePriceNthPeriod) * 100;

    console.log(i, roc);

  }
}