UNPKG

@4ex/indicators

Version:

Technical indicators for ohlc charts written in TypeScript

96 lines (95 loc) 3.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MACD = void 0; const fp_1 = require("lodash/fp"); const technicalindicators_1 = require("technicalindicators"); const types_1 = require("../types"); const defaultInput = { source: types_1.OHLCEnum.CLOSE, signalPeriod: 9, fastPeriod: 12, slowPeriod: 26, simpleMAOscillator: false, simpleMASignal: false, }; /** * Moving average convergence divergence (MACD) is a trend-following momentum * indicator that shows the relationship between two moving averages of a * security’s price. The MACD is calculated by subtracting the 26-period * exponential moving average (EMA) from the 12-period EMA. The result of that * calculation is the MACD line. A nine-day EMA of the MACD called the * "signal line," is then plotted on top of the MACD line, which can function * as a trigger for buy and sell signals. Traders may buy the security when the * MACD crosses above its signal line and sell—or short—the security when the * MACD crosses below the signal line. Moving average convergence divergence * (MACD) indicators can be interpreted in several ways, but the more common * methods are crossovers, divergences, and rapid rises/falls. */ class MACD { /** * * @param {OHLC[]} series candle series * @param {OHLCEnum} source source values from candle * @param {number} fastPeriod * @param {number} slowPeriod * @param {number} signalPeriod signal line period * @param {boolean} simpleMAOscillator use simple MA for Oscillator * @param {boolean} simpleMASignal use simple MA for Signal */ constructor(series, source = defaultInput.source, fastPeriod = defaultInput.fastPeriod, slowPeriod = defaultInput.slowPeriod, signalPeriod = defaultInput.signalPeriod, simpleMAOscillator = defaultInput.simpleMAOscillator, simpleMASignal = defaultInput.simpleMASignal) { // super(); const values = fp_1.map(source)(series); this.indicator = new technicalindicators_1.MACD({ values, fastPeriod, slowPeriod, signalPeriod, SimpleMAOscillator: simpleMAOscillator, SimpleMASignal: simpleMASignal, }); this.source = source; } /** * Retrieve MACD values for instance * @return {MovingAverageConvergenceDivergenceOutput[]} values for instance * data */ getResults() { return this.indicator.getResult(); } /** * Calculate MACD to next tick * @param {OHLC} candle new candle to add to series * @return {MovingAverageConvergenceDivergenceOutput | undefined} next MACD * value or undefined if period is greater than actual series length */ next(candle) { return this.indicator.nextValue(candle[this.source]); } /** * Create instance from data * @param {MovingAverageConvergenceDivergenceInput} input input data * @return {MACD} MACD instance */ static generator({ series, source, fastPeriod, slowPeriod, signalPeriod, simpleMAOscillator, simpleMASignal, }) { return new MACD(series, source, fastPeriod, slowPeriod, signalPeriod, simpleMAOscillator, simpleMASignal); } /** * Get MACD values from input * @param {MovingAverageConvergenceDivergenceInput} input input data * @return {MovingAverageConvergenceDivergenceOutput[]} MACD values */ static calculate(input) { const { series, source, fastPeriod, slowPeriod, signalPeriod, simpleMAOscillator, simpleMASignal, } = Object.assign(Object.assign({}, defaultInput), input); const values = fp_1.map(source)(series); return technicalindicators_1.MACD.calculate({ values, fastPeriod, slowPeriod, signalPeriod, SimpleMAOscillator: simpleMAOscillator, SimpleMASignal: simpleMASignal, }); } } exports.MACD = MACD;