import {
  TMixed,
  checkArrayOfNumbers,
  checkNumber,
  funMA,
  funSTD,
} from "../common";
import { funRound } from "./../common/index";

/**
 * - 计算BOLL指标
 * @param {number[]} CLOSE - 收盘价序列
 * @param {number} M - 周期 default: 20
 * @returns {{ BOLL: TMixed[]; UB: TMixed[]; LB: TMixed[] }} - BOLL指标结果
 */
export function BOLL(
  CLOSE: number[],
  M: number = 20
): { BOLL: TMixed[]; UB: TMixed[]; LB: TMixed[] } {
  checkArrayOfNumbers(CLOSE, "CLOSE");
  checkNumber(M, 1, Number.MAX_SAFE_INTEGER, "M", "BOLL");
  const mid = funMA(CLOSE, M);
  const std = funSTD(CLOSE, M);
  const BOLL = mid.map((v, i) => funRound(v, 3));
  const UB = mid.map((v, i) =>
    v === null || std[i] === null ? null : funRound(v + 2 * std[i], 3)
  );
  const LB = mid.map((v, i) =>
    v === null || std[i] === null ? null : funRound(v - 2 * std[i], 3)
  );
  return { BOLL, UB, LB };
}
