UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

64 lines 2.43 kB
import { TradingSignal, TrendIndicatorSeries } from '../../base/Indicator.js'; import { pushUpdate } from '../../util/pushUpdate.js'; import { WSMA } from '../../trend/WSMA/WSMA.js'; export class RSI extends TrendIndicatorSeries { #previousPrices = []; #avgGain; #avgLoss; #maxValue = 100; #overbought; #oversold; interval; constructor(interval, SmoothingIndicator = WSMA, { overbought = 70, oversold = 30 } = {}) { super(); this.interval = interval; this.#avgGain = new SmoothingIndicator(this.interval); this.#avgLoss = new SmoothingIndicator(this.interval); this.#overbought = overbought; this.#oversold = oversold; } getRequiredInputs() { return this.#avgGain.getRequiredInputs(); } update(price, replace) { pushUpdate(this.#previousPrices, replace, price, this.interval); if (this.#previousPrices.length < 2) { return null; } const currentPrice = price; const previousPrice = this.#previousPrices[this.#previousPrices.length - 2]; if (currentPrice > previousPrice) { this.#avgLoss.update(0, replace); this.#avgGain.update(price - previousPrice, replace); } else { this.#avgLoss.update(previousPrice - currentPrice, replace); this.#avgGain.update(0, replace); } if (this.#avgGain.isStable) { const avgLoss = this.#avgLoss.getResultOrThrow(); if (avgLoss === 0) { return this.setResult(100, replace); } const relativeStrength = this.#avgGain.getResultOrThrow() / avgLoss; return this.setResult(this.#maxValue - this.#maxValue / (relativeStrength + 1), replace); } return null; } calculateSignalState(result) { const hasResult = result !== null && result !== undefined; const isOversold = hasResult && result <= this.#oversold; const isOverbought = hasResult && result >= this.#overbought; switch (true) { case !hasResult: return TradingSignal.UNKNOWN; case isOversold: return TradingSignal.BEARISH; case isOverbought: return TradingSignal.BULLISH; default: return TradingSignal.SIDEWAYS; } } } //# sourceMappingURL=RSI.js.map