UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

56 lines 2.02 kB
import { TradingSignal, TrendIndicatorSeries } from '../../base/Indicator.js'; import { EMA } from '../../trend/EMA/EMA.js'; export class TRIX extends TrendIndicatorSeries { #single; #double; #triple; #previousTripleEma; #penultimateTripleEma; interval; constructor(interval) { super(); this.interval = interval; this.#single = new EMA(interval); this.#double = new EMA(interval); this.#triple = new EMA(interval); } getRequiredInputs() { return 3 * (this.interval - 1) + 2; } update(price, replace) { const single = this.#single.update(price, replace); if (this.#single.isStable) { const double = this.#double.update(single, replace); if (this.#double.isStable) { const triple = this.#triple.update(double, replace); if (this.#triple.isStable) { const previous = replace ? this.#penultimateTripleEma : this.#previousTripleEma; if (!replace) { this.#penultimateTripleEma = this.#previousTripleEma; } this.#previousTripleEma = triple; if (previous !== undefined && triple !== 0) { return this.setResult((100 * (triple - previous)) / triple, replace); } } } } return null; } calculateSignalState(result) { const hasResult = result !== null && result !== undefined; const isBullish = hasResult && result > 0; const isBearish = hasResult && result < 0; switch (true) { case !hasResult: return TradingSignal.UNKNOWN; case isBullish: return TradingSignal.BULLISH; case isBearish: return TradingSignal.BEARISH; default: return TradingSignal.SIDEWAYS; } } } //# sourceMappingURL=TRIX.js.map