UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

29 lines 1.19 kB
import { MovingAverage } from '../MA/MovingAverage.js'; import { pushUpdate } from '../../util/pushUpdate.js'; export class KAMA extends MovingAverage { static #FAST = 2 / (2 + 1); static #SLOW = 2 / (30 + 1); #prices = []; getRequiredInputs() { return this.interval; } update(price, replace) { pushUpdate(this.#prices, replace, price, this.interval + 1); if (this.#prices.length < this.interval) { return null; } if (this.#prices.length === this.interval) { return this.setResult(price, replace); } let volatility = 0; for (let i = 1; i < this.#prices.length; i++) { volatility += Math.abs(this.#prices[i] - this.#prices[i - 1]); } const netChange = Math.abs(price - this.#prices[0]); const efficiencyRatio = volatility === 0 ? 1 : netChange / volatility; const smoothing = (efficiencyRatio * (KAMA.#FAST - KAMA.#SLOW) + KAMA.#SLOW) ** 2; const previous = (replace ? this.previousResult : this.result); return this.setResult(previous + smoothing * (price - previous), replace); } } //# sourceMappingURL=KAMA.js.map