UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

45 lines 1.49 kB
import { TradingSignal, TrendIndicatorSeries } from '../../base/Indicator.js'; import { pushUpdate } from '../../util/pushUpdate.js'; export class CMF extends TrendIndicatorSeries { #candles = []; interval; constructor(interval) { super(); this.interval = interval; } getRequiredInputs() { return this.interval; } update(candle, replace) { pushUpdate(this.#candles, replace, candle, this.interval); if (this.#candles.length < this.interval) { return null; } let sumMoneyFlowVolume = 0; let sumVolume = 0; for (const c of this.#candles) { const highLowDiff = c.high - c.low; let moneyFlowMultiplier = 0; if (highLowDiff !== 0) { moneyFlowMultiplier = (c.close - c.low - (c.high - c.close)) / highLowDiff; } sumMoneyFlowVolume += moneyFlowMultiplier * c.volume; sumVolume += c.volume; } const cmf = sumVolume === 0 ? 0 : sumMoneyFlowVolume / sumVolume; return this.setResult(cmf, replace); } calculateSignalState(result) { if (result === null || result === undefined) { return TradingSignal.UNKNOWN; } if (result > 0) { return TradingSignal.BULLISH; } if (result < 0) { return TradingSignal.BEARISH; } return TradingSignal.SIDEWAYS; } } //# sourceMappingURL=CMF.js.map