stock-indicator
Version:
Stock Indicator Library
29 lines • 1.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MACD = MACD;
const common_1 = require("../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
*/
function MACD(CLOSE, SHORT = 12, LONG = 26, MID = 9) {
// - check input data
(0, common_1.checkArrayOfNumbers)(CLOSE, "CLOSE");
// - check n and m parameters
(0, common_1.checkNumber)(SHORT, 2, LONG, "SHORT", "LONG");
(0, common_1.checkNumber)(LONG, SHORT, CLOSE, "LONG", "CLOSE");
(0, common_1.checkNumber)(MID, 2, CLOSE, "MID", "CLOSE");
const fastMA = (0, common_1.funEMA)(CLOSE, SHORT);
const slowMA = (0, common_1.funEMA)(CLOSE, LONG);
const DIF = fastMA.map((item, index) => item !== null && slowMA[index] !== null
? (0, common_1.funRound)(item - slowMA[index], 3)
: null);
const DEA = (0, common_1.funEMA)(DIF, MID).map((item) => (0, common_1.funRound)(item, 3));
const MACD = DIF.map((item, index) => item && DEA[index] ? (0, common_1.funRound)(2 * (item - DEA[index]), 3) : null);
return { DIF, DEA, MACD };
}
//# sourceMappingURL=macd.js.map