UNPKG

@thi.ng/transducers-stats

Version:

Transducers for statistical / technical analysis

31 lines (30 loc) 824 B
import { compR } from "@thi.ng/transducers/compr"; import { __iter } from "@thi.ng/transducers/iterator"; import { step } from "@thi.ng/transducers/step"; import { ema } from "./ema.js"; function macd(...args) { return __iter(macd, args) || ((rfn) => { const reduce = rfn[2]; const maFast = step(ema(args[0] || 12)); const maSlow = step(ema(args[1] || 26)); const maSmooth = step(ema(args[2] || 9)); return compR(rfn, (acc, x) => { const fast = maFast(x); const slow = maSlow(x); if (slow == null) return acc; const macd2 = fast - slow; const signal = maSmooth(macd2); if (signal == null) return acc; return reduce(acc, { macd: macd2, signal, div: macd2 - signal, fast, slow }); }); }); } export { macd };