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

/**
 * - CCI (Commodity Channel Index)
 * @param {number[]} CLOSE - closing prices series
 * @param {number[]}  HIGH - highest prices series
 * @param {number[]}  LOW - lowest prices series
 * @param {number} N - number of periods,default 14
 * @returns {{ CCI: TMixed[] }} - CCI values
 * TYP:=(HIGH+LOW+CLOSE)/3;
 * CCI:(TYP-MA(TYP,N))*1000/(15*AVEDEV(TYP,N));
 */
export function CCI(
  CLOSE: number[],
  HIGH: number[],
  LOW: number[],
  N: number = 14
): { CCI: TMixed[] } {
  checkArrayOfNumbers(CLOSE, "CLOSE");
  checkArrayOfNumbers(HIGH, "HIGH");
  checkArrayOfNumbers(LOW, "LOW");
  checkNumber(N, 2, 1000, "N", "CCI");

  const typ = CLOSE.map((cur, index) => (HIGH[index] + LOW[index] + cur) / 3);
  const mytyp = funMA(typ, N);
  const avedevtype = funAVEDEV(typ, N);
  const CCI = typ.map((cur, index) =>
    mytyp[index] === null || avedevtype[index] === null
      ? null
      : funRound(((cur - mytyp[index]) * 1000) / (15 * avedevtype[index]), 3)
  );

  return { CCI };
}
