UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

55 lines 2.08 kB
import { SMA } from '../../trend/SMA/SMA.js'; import { TradingSignal, TrendIndicatorSeries } from '../../base/Indicator.js'; import { getTypicalPrice, pushUpdate } from '../../util/index.js'; import { MAD } from '../../volatility/MAD/MAD.js'; export class CCI extends TrendIndicatorSeries { #sma; #typicalPrices; #overbought; #oversold; interval; constructor(interval, { overbought = 100, oversold = -100 } = {}) { super(); this.interval = interval; this.#sma = new SMA(this.interval); this.#typicalPrices = []; this.#overbought = overbought; this.#oversold = oversold; } getRequiredInputs() { return this.#sma.getRequiredInputs(); } update(candle, replace) { const typicalPrice = this.#cacheTypicalPrice(candle, replace); this.#sma.update(typicalPrice, replace); if (this.#sma.isStable) { const mean = this.#sma.getResultOrThrow(); const meanDeviation = MAD.getResultFromBatch(this.#typicalPrices, mean); const numerator = typicalPrice - mean; const denominator = 0.015 * meanDeviation; return this.setResult(numerator / denominator, replace); } return null; } #cacheTypicalPrice(candle, replace) { const typicalPrice = getTypicalPrice(candle); pushUpdate(this.#typicalPrices, replace, typicalPrice, this.interval); return typicalPrice; } 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=CCI.js.map