UNPKG

stock-indicator

Version:

Stock Indicator Library

44 lines (40 loc) 1.35 kB
import { funRound, TMixed, funEMA, checkArrayOfNumbers, checkNumber, } from "../common"; /** * - MACD (Moving Average Convergence Divergence) * @param {number[]} CLOSE - close prices * @param {number} SHORT - short period,default 12 * @param {number} LONG - long period,default 26 * @param {number} MID - mid period,default 9 * @returns { DIF: TMixed[], DEA: TMixed[], MACD: TMixed[] } - DIF: Difference between fast and slow EMA, DEA: Exponential Moving Average of DIF, MACD: DIF minus DEA */ export function MACD( CLOSE: number[], SHORT: number = 12, LONG: number = 26, MID: number = 9 ): { DIF: TMixed[]; DEA: TMixed[]; MACD: TMixed[] } { // - check input data checkArrayOfNumbers(CLOSE, "CLOSE"); // - check n and m parameters checkNumber(SHORT, 2, LONG, "SHORT", "LONG"); checkNumber(LONG, SHORT, CLOSE, "LONG", "CLOSE"); checkNumber(MID, 2, CLOSE, "MID", "CLOSE"); const fastMA = funEMA(CLOSE, SHORT); const slowMA = funEMA(CLOSE, LONG); const DIF: TMixed[] = fastMA.map((item, index) => item !== null && slowMA[index] !== null ? funRound(item - slowMA[index], 3) : null ); const DEA = funEMA(DIF, MID).map((item) => funRound(item, 3)); const MACD = DIF.map((item, index) => item && DEA[index] ? funRound(2 * (item - DEA[index]), 3) : null ); return { DIF, DEA, MACD }; }