stock-indicator
Version:
Stock Indicator Library
63 lines • 2.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DMI = DMI;
const common_1 = require("../common");
/**
* DMI - Directional Movement Index
* @param {number[]} CLOSE - close prices
* @param {number[]} HIGH - high prices
* @param {number[]} LOW - low prices
* @param {number} N - number of periods,default 14
* @param {number} M - number of periods for moving average,default 6
* @returns {Object} result
*/
function DMI(CLOSE, HIGH, LOW, N = 14, M = 6) {
var _a, _b, _c, _d;
// - check input data
(0, common_1.checkArrayOfNumbers)(CLOSE, "CLOSE");
(0, common_1.checkArrayOfNumbers)(HIGH, "HIGH");
(0, common_1.checkArrayOfNumbers)(LOW, "LOW");
(0, common_1.checkNumber)(N, 2, CLOSE, "N", "CLOSE");
(0, common_1.checkNumber)(M, 2, N, "M", "N");
// - calculate DMI
const tmpM = CLOSE.slice(1).reduce((acc, cur, index) => {
acc.push(Math.max(Math.max(HIGH[index + 1] - LOW[index + 1], Math.abs(HIGH[index + 1] - CLOSE[index])), Math.abs(CLOSE[index]) - LOW[index + 1]));
return acc;
}, [0]);
const MTR = (0, common_1.funMS)(tmpM, N);
const [HD, LD] = CLOSE.slice(1).reduce((acc, cur, index) => {
acc[0].push(HIGH[index + 1] - HIGH[index]);
acc[1].push(LOW[index] - LOW[index + 1]);
return acc;
}, [[0], [0]]);
const HDM = HD.map((v, i) => (v > 0 && (0, common_1.aGtB)(v, LD[i]) ? v : 0)); //浮点数比较大小,使用aGtB函数
const LDM = LD.map((v, i) => (v > 0 && (0, common_1.aGtB)(v, HD[i]) ? v : 0));
const DMP = (0, common_1.funMS)(HDM, N);
const DMM = (0, common_1.funMS)(LDM, N);
//以上数据正确
const PDI = [];
const MDI = [];
for (let i = 0; i < DMP.length; i++) {
PDI.push((((_a = DMP[i]) !== null && _a !== void 0 ? _a : 0) * 100) / ((_b = MTR[i]) !== null && _b !== void 0 ? _b : 1));
MDI.push((((_c = DMM[i]) !== null && _c !== void 0 ? _c : 0) * 100) / ((_d = MTR[i]) !== null && _d !== void 0 ? _d : 1));
}
const ADX = (0, common_1.funMA)((0, common_1.funArrDiv)((0, common_1.funArrAbs)((0, common_1.funArrSub)(MDI, PDI)), (0, common_1.funArrAdd)(MDI, PDI)).map((v) => v !== null ? v * 100 : null), M);
const ADXR = ADX.map((v, i) => {
var _a;
if (i < M)
return v;
if (ADX[i - M] === null) {
return null;
}
else {
return ((v !== null && v !== void 0 ? v : 0) + ((_a = ADX[i - M]) !== null && _a !== void 0 ? _a : 0)) / 2;
}
});
return {
PDI: PDI.map((v) => (0, common_1.funRound)(v, 3)),
MDI: MDI.map((v) => (0, common_1.funRound)(v, 3)),
ADX: ADX.map((v) => (0, common_1.funRound)(v, 3)),
ADXR: ADXR.map((v) => (0, common_1.funRound)(v, 3)),
};
}
//# sourceMappingURL=dmi.js.map