trading-signals
Version:
Technical indicators to run technical analysis with JavaScript / TypeScript.
32 lines • 1.15 kB
JavaScript
import { IndicatorSeries } from '../../base/Indicator.js';
import { SMA } from '../SMA/SMA.js';
export class WSMA extends IndicatorSeries {
#indicator;
#smoothingFactor;
interval;
constructor(interval) {
super();
this.interval = interval;
this.#indicator = new SMA(interval);
this.#smoothingFactor = 1 / this.interval;
}
getRequiredInputs() {
return this.interval;
}
update(price, replace) {
const sma = this.#indicator.update(price, replace);
if (replace && this.previousResult !== undefined) {
const smoothed = (price - this.previousResult) * this.#smoothingFactor;
return this.setResult(smoothed + this.previousResult, replace);
}
else if (!replace && this.result !== undefined) {
const smoothed = (price - this.result) * this.#smoothingFactor;
return this.setResult(smoothed + this.result, replace);
}
else if (this.result === undefined && sma !== null) {
return this.setResult(sma, replace);
}
return null;
}
}
//# sourceMappingURL=WSMA.js.map