@thi.ng/transducers-stats
Version:
Transducers for statistical / technical analysis
27 lines (26 loc) • 802 B
JavaScript
import { compR } from "@thi.ng/transducers/compr";
import { __iter } from "@thi.ng/transducers/iterator";
import { step } from "@thi.ng/transducers/step";
import { donchian } from "./donchian.js";
import { sma } from "./sma.js";
function stochastic(...args) {
return __iter(stochastic, args) || ((rfn) => {
const reduce = rfn[2];
const xfD = step(donchian(args[0] || 5));
const ma1 = step(sma(args[1] || 3));
const ma2 = step(sma(args[2] || 3));
return compR(rfn, (acc, x) => {
const b = xfD(x);
if (b == null) return acc;
const k = (x - b[0]) / (b[1] - b[0]);
const d1 = ma1(k);
if (d1 == null) return acc;
const d2 = ma2(d1);
if (d2 == null) return acc;
return reduce(acc, { k, d1, d2 });
});
});
}
export {
stochastic
};