UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

55 lines 2 kB
import { TradingSignal, TrendIndicatorSeries } from '../../base/Indicator.js'; import { pushUpdate } from '../../util/pushUpdate.js'; export class WilliamsR extends TrendIndicatorSeries { candles = []; interval; #overbought; #oversold; constructor(interval, { overbought = -20, oversold = -80 } = {}) { super(); this.interval = interval; this.#overbought = overbought; this.#oversold = oversold; } getRequiredInputs() { return this.interval; } update(candle, replace) { pushUpdate(this.candles, replace, candle, this.interval); if (this.candles.length === this.interval) { let highest = this.candles[0].high; let lowest = this.candles[0].low; for (let i = 1; i < this.candles.length; i++) { if (this.candles[i].high > highest) { highest = this.candles[i].high; } if (this.candles[i].low < lowest) { lowest = this.candles[i].low; } } const divisor = highest - lowest; if (divisor === 0) { return this.setResult(-100, replace); } const willR = ((highest - candle.close) / divisor) * -100; return this.setResult(willR, replace); } return null; } calculateSignalState(result) { const hasResult = result !== null && result !== undefined; const isOverbought = hasResult && result >= this.#overbought; const isOversold = hasResult && result <= this.#oversold; switch (true) { case !hasResult: return TradingSignal.UNKNOWN; case isOverbought: return TradingSignal.BULLISH; case isOversold: return TradingSignal.BEARISH; default: return TradingSignal.SIDEWAYS; } } } //# sourceMappingURL=WilliamsR.js.map