UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

27 lines 950 B
import { MovingAverage } from '../MA/MovingAverage.js'; import { pushUpdate } from '../../util/pushUpdate.js'; export class WMA extends MovingAverage { prices = []; interval; constructor(interval) { super(interval); this.interval = interval; } getRequiredInputs() { return this.interval; } update(price, replace) { pushUpdate(this.prices, replace, price, this.interval); if (this.prices.length === this.interval) { const weightedPricesSum = this.prices.reduce((acc, price, index) => { const weightedPrice = price * (index + 1); return acc + weightedPrice; }, 0); const weightBase = (this.interval * (this.interval + 1)) / 2; const weightedMa = weightedPricesSum / weightBase; return this.setResult(weightedMa, replace); } return null; } } //# sourceMappingURL=WMA.js.map