UNPKG

@4ex/indicators

Version:

Technical indicators for ohlc charts written in TypeScript

76 lines (75 loc) 2.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TRIX = void 0; const fp_1 = require("lodash/fp"); const technicalindicators_1 = require("technicalindicators"); const types_1 = require("../types"); const defaultInput = { period: 18, }; /** * The triple exponential average (TRIX) indicator is an oscillator used * to identify oversold and overbought markets, and it can also be used as * a momentum indicator. Like many oscillators, TRIX oscillates around a * zero line. When it is used as an oscillator, a positive value indicates an * overbought market while a negative value indicates an oversold market. * When TRIX is used as a momentum indicator, a positive value suggests * momentum is increasing while a negative value suggests momentum is * decreasing. Many analysts believe that when the TRIX crosses above the zero * line it gives a buy signal, and when it closes below the zero line, it gives * a sell signal. Also, divergences between price and TRIX can indicate * significant turning points in the market. TRIX calculates a triple * exponential moving average of the log of the price input over the period of * time specified by the length input for the current bar. The current bar's * value is subtracted by the previous bar's value. This prevents cycles that * are shorter than the period defined by length input from being considered by * the indicator. */ class TRIX { /** * @param {OHLC[]} series candles series * @param {number} period period for indicator */ constructor(series, period = defaultInput.period) { // super(); const values = fp_1.map(types_1.OHLCEnum.CLOSE)(series); this.indicator = new technicalindicators_1.TRIX({ values, period }); } /** * Retrieve TRIX values for instance * @return {number[]} values for instance data */ getResults() { return this.indicator.getResult(); } /** * Calculate TRIX to next tick * @param {OHLC} candle new candle to add to series * @return {number | undefined} next TRIX value or undefined if period is * greater than actual series length */ next(candle) { return this.indicator.nextValue(candle[types_1.OHLCEnum.CLOSE]); } /** * Create instance from data * @param {TripleMovingAverageInput} input input data * @return {TRIX} TRIX instance */ static generator({ series, period }) { return new TRIX(series, period); } /** * Get TRIX values from input * @param {TripleMovingAverageInput} input input data * @return {number[]} TRIX values */ static calculate({ series, period, }) { const values = fp_1.map(types_1.OHLCEnum.CLOSE)(series); return technicalindicators_1.TRIX.calculate({ values, period: period !== null && period !== void 0 ? period : defaultInput.period, }); } } exports.TRIX = TRIX;