UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

70 lines 2.02 kB
import { NotEnoughDataError } from '../error/NotEnoughDataError.js'; export const TradingSignal = { BEARISH: 'BEARISH', BULLISH: 'BULLISH', SIDEWAYS: 'SIDEWAYS', UNKNOWN: 'UNKNOWN', }; export class TechnicalIndicator { result; getResult() { try { return this.getResultOrThrow(); } catch { return null; } } getResultOrThrow() { if (this.result === undefined) { throw new NotEnoughDataError(this.getRequiredInputs()); } return this.result; } get isStable() { return this.result !== undefined; } add(input) { return this.update(input, false); } replace(input) { return this.update(input, true); } updates(inputs, replace = false) { return inputs.map(input => this.update(input, replace)); } } export class IndicatorSeries extends TechnicalIndicator { previousResult; setResult(value, replace) { if (replace) { this.result = this.previousResult; } this.previousResult = this.result; return (this.result = value); } rollbackLastResult() { this.result = this.previousResult; } } export class TrendIndicatorSeries extends IndicatorSeries { #previousSignalState; setResult(value, replace) { if (replace && this.previousResult !== undefined) { this.#previousSignalState = this.calculateSignalState(this.previousResult); } else if (!replace) { this.#previousSignalState = this.calculateSignalState(this.result); } return super.setResult(value, replace); } getSignal() { const currentState = this.calculateSignalState(this.getResult()); const hasChanged = this.#previousSignalState !== undefined && this.#previousSignalState !== currentState; return { hasChanged, state: currentState, }; } } //# sourceMappingURL=Indicator.js.map