stock-indicator
Version:
Stock Indicator Library
46 lines • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KDJ = KDJ;
const common_1 = require("../common");
/**
* - KDJ 随机指标
* @param {number[]} CLOSE - 收盘价序列
* @param {number[]} HIGH - 最高价序列
* @param {number[]} LOW - 最低价序列
* @param {number} N - 周期,default: 9
* @param {number} M1 - 平滑参数,default: 3
* @param {number} M2 - 平滑参数,default: 3
* @returns {{ K: TMixed[]; D: TMixed[]; J: TMixed[] }}
*/
function KDJ(CLOSE, HIGH, LOW, N = 9, M1 = 3, M2 = 3) {
// - check input parameters
(0, common_1.checkArrayOfNumbers)(CLOSE, "CLOSE");
(0, common_1.checkArrayOfNumbers)(HIGH, "HIGH");
(0, common_1.checkArrayOfNumbers)(LOW, "LOW");
(0, common_1.checkNumber)(N, 1, CLOSE, "N", "CLOSE");
(0, common_1.checkNumber)(M1, 1, N, "M1", "N");
(0, common_1.checkNumber)(M2, 1, N, "M2", "N");
// - calculate KDJ
const llv = LOW.reduce((prev, cur, i) => {
return [
...prev,
Math.min(...LOW.slice(Math.max(0, i - N + 1), i + 1).map((v) => v !== null && v !== void 0 ? v : 0)),
];
}, []);
const hhv = HIGH.reduce((prev, cur, i) => {
return [
...prev,
Math.max(...HIGH.slice(Math.max(0, i - N + 1), i + 1).map((v) => v !== null && v !== void 0 ? v : 0)),
];
}, []);
const RSV = CLOSE.map((v, i) => (((v !== null && v !== void 0 ? v : 0) - llv[i]) / (hhv[i] - llv[i])) * 100);
const K = (0, common_1.funSMA)(RSV, M1, 1);
const D = (0, common_1.funSMA)(K, M2, 1);
const J = K.map((v, i) => { var _a; return (0, common_1.funRound)(3 * (v !== null && v !== void 0 ? v : 0) - 2 * ((_a = D[i]) !== null && _a !== void 0 ? _a : 0), 3); });
return {
K: K.map((v) => (0, common_1.funRound)(v, 3)),
D: D.map((v) => (0, common_1.funRound)(v, 3)),
J,
};
}
//# sourceMappingURL=kdj.js.map