UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

73 lines 2.58 kB
import { TechnicalIndicator, TradingSignal } from '../../base/Indicator.js'; import { SMA } from '../../trend/SMA/SMA.js'; export class AccelerationBands extends TechnicalIndicator { #lowerBand; #middleBand; #upperBand; #previousResult; #twoPreviousResult; #lastClose; #previousClose; interval; width; constructor(interval, width, SmoothingIndicator = SMA) { super(); this.interval = interval; this.width = width; this.#lowerBand = new SmoothingIndicator(interval); this.#middleBand = new SmoothingIndicator(interval); this.#upperBand = new SmoothingIndicator(interval); } getRequiredInputs() { return this.#middleBand.getRequiredInputs(); } update({ close, high, low }, replace) { const highPlusLow = high + low; const coefficient = highPlusLow === 0 ? 0 : ((high - low) / highPlusLow) * this.width; this.#lowerBand.update(low * (1 - coefficient), replace); this.#middleBand.update(close, replace); this.#upperBand.update(high * (1 + coefficient), replace); if (replace) { this.result = this.#previousResult; this.#previousResult = this.#twoPreviousResult; this.#lastClose = this.#previousClose; } this.#twoPreviousResult = this.#previousResult; this.#previousResult = this.result; this.#previousClose = this.#lastClose; this.#lastClose = close; if (this.isStable) { return (this.result = { lower: this.#lowerBand.getResultOrThrow(), middle: this.#middleBand.getResultOrThrow(), upper: this.#upperBand.getResultOrThrow(), }); } return null; } get isStable() { return this.#middleBand.isStable; } calculateSignal(result, close) { if (!result || close === undefined) { return TradingSignal.UNKNOWN; } if (close > result.upper) { return TradingSignal.BULLISH; } if (close < result.lower) { return TradingSignal.BEARISH; } return TradingSignal.SIDEWAYS; } getSignal() { const previousState = this.calculateSignal(this.#twoPreviousResult, this.#previousClose); const state = this.calculateSignal(this.#previousResult, this.#lastClose); const hasChanged = previousState !== state; return { hasChanged, state, }; } } //# sourceMappingURL=AccelerationBands.js.map