UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

33 lines 1.37 kB
import { IndicatorSeries } from '../../base/Indicator.js'; import { getTypicalPrice } from '../../util/getTypicalPrice.js'; export class VWAP extends IndicatorSeries { #cumulativeTypicalPriceVolume = 0; #cumulativeVolume = 0; #lastCandle = null; constructor() { super(); } #calculateTypicalPriceVolume(candle) { return getTypicalPrice(candle) * candle.volume; } getRequiredInputs() { return 2; } update(candle, replace) { if (candle.volume === 0) { return null; } if (replace && this.#lastCandle !== null) { const lastTypicalPriceVolume = this.#calculateTypicalPriceVolume(this.#lastCandle); this.#cumulativeTypicalPriceVolume = this.#cumulativeTypicalPriceVolume - lastTypicalPriceVolume; this.#cumulativeVolume = this.#cumulativeVolume - this.#lastCandle.volume; } this.#lastCandle = candle; const typicalPriceVolume = this.#calculateTypicalPriceVolume(candle); this.#cumulativeTypicalPriceVolume = this.#cumulativeTypicalPriceVolume + typicalPriceVolume; this.#cumulativeVolume = this.#cumulativeVolume + candle.volume; const vwap = this.#cumulativeTypicalPriceVolume / this.#cumulativeVolume; return this.setResult(vwap, replace); } } //# sourceMappingURL=VWAP.js.map