UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

70 lines 2.27 kB
import { TradingSignal, TrendIndicatorSeries } from '../../base/Indicator.js'; export class TDS extends TrendIndicatorSeries { state = { closes: [], lastBarCompletedSetup: false, setupCount: 0, setupDirection: null, }; getRequiredInputs() { return 9; } update(close, replace) { if (replace && this.state.lastBarCompletedSetup) { this.rollbackLastResult(); } this.trackState(replace); const state = this.state; state.closes.push(close); state.lastBarCompletedSetup = false; if (state.closes.length < 5) { return null; } if (state.closes.length > 13) { state.closes.shift(); } const index = state.closes.length - 1; const prev4 = state.closes[index - 4]; if (close > prev4) { if (state.setupDirection === 'bearish') { state.setupCount = 1; state.setupDirection = 'bullish'; } else { state.setupCount++; state.setupDirection = 'bullish'; } } else if (close < prev4) { if (state.setupDirection === 'bullish') { state.setupCount = 1; state.setupDirection = 'bearish'; } else { state.setupCount++; state.setupDirection = 'bearish'; } } if (state.setupCount >= this.getRequiredInputs()) { const result = state.setupDirection === 'bullish' ? 1 : -1; state.setupCount = 0; state.setupDirection = null; state.lastBarCompletedSetup = true; return this.setResult(result, replace); } return null; } calculateSignalState(result) { const hasResult = result !== null && result !== undefined; const isOverbought = hasResult && result === 1; switch (true) { case !hasResult: return TradingSignal.UNKNOWN; case isOverbought: return TradingSignal.BULLISH; default: return TradingSignal.BEARISH; } } } //# sourceMappingURL=TDS.js.map