UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

48 lines 1.64 kB
import { IndicatorSeries } from '../../base/Indicator.js'; export class RVOL extends IndicatorSeries { #period; #priorVolumes = []; #snapshot = null; constructor(period) { super(); if (period < 1) { throw new Error(`period must be >= 1, got "${period}"`); } this.#period = period; } getRequiredInputs() { return this.#period + 1; } update(volume, replace) { if (replace) { if (!this.#snapshot) { throw new Error('Cannot replace before any input has been added.'); } this.#priorVolumes.length = 0; this.#priorVolumes.push(...this.#snapshot.priorVolumes); this.previousResult = this.#snapshot.resultBeforeUpdate; } else { this.#snapshot = { priorVolumes: [...this.#priorVolumes], resultBeforeUpdate: this.result, }; } if (this.#priorVolumes.length < this.#period) { this.#priorVolumes.push(volume); return null; } const window = this.#priorVolumes.slice(-this.#period); const baseline = window.reduce((a, b) => a + b, 0) / this.#period; this.#priorVolumes.push(volume); if (this.#priorVolumes.length > this.#period * 2) { this.#priorVolumes.splice(0, this.#priorVolumes.length - this.#period); } if (baseline === 0) { this.result = undefined; return null; } return this.setResult(volume / baseline, replace); } } //# sourceMappingURL=RVOL.js.map