Чтобы рассчитать полосы Боллинджера с помощью JavaScript, вам нужно сначала рассчитать простую скользящую среднюю (SMA) для заданного массива цен закрытия. SMA рассчитывается по следующей формуле:

SMA = sum of closing prices in the period / number of closing prices in the period

Получив SMA, вы можете рассчитать верхнюю и нижнюю полосы Боллинджера, используя следующие формулы:

upper band = SMA + (standard deviation of closing prices in the period * number of standard deviations)
lower band = SMA - (standard deviation of closing prices in the period * number of standard deviations)

где стандартное отклонение рассчитывается по следующей формуле:

standard deviation = square root of sum of (closing price - SMA)^2 in the period / (number of closing prices in the period - 1)

Вот пример того, как вы можете реализовать эти формулы для расчета полос Боллинджера для заданного массива цен закрытия:

function calculateBollingerBands(closingPrices, period, numStdDev) {
  // Calculate the SMA
  let sum = 0;
  for (let i = 0; i < closingPrices.length; i++) {
    sum += closingPrices[i];
  }
  const sma = sum / closingPrices.length;

  // Calculate the standard deviation
  sum = 0;
  for (let i = 0; i < closingPrices.length; i++) {
    sum += Math.pow(closingPrices[i] - sma, 2);
  }
  const stdDev = Math.sqrt(sum / (closingPrices.length - 1));

  // Calculate the upper and lower bands
  const upperBand = sma + (stdDev * numStdDev);
  const lowerBand = sma - (stdDev * numStdDev);

  return { upperBand, lowerBand };
}

// Example usage
const closingPrices = [100, 110, 105, 115, 120, 130, 140, 150, 145, 155];
const { upperBand, lowerBand } = calculateBollingerBands(closingPrices, 20, 2);
console.log(upperBand); // Output: 132.78
console.log(lowerBand); // Output: 87.22

Этот код вычисляет полосы Боллинджера для заданного массива цен закрытия, сначала вычисляя SMA и стандартное отклонение, а затем применяя к этим значениям формулы полос Боллинджера. Вы можете настроить количество цен закрытия, используемых в расчете, изменив длину массива, переданного в функцию calculateBollingerBands(). Вы также можете настроить период и количество стандартных отклонений, используемых в расчете, изменив параметры period и numStdDev соответственно.