trading-signals
Version:
Technical indicators to run technical analysis with JavaScript / TypeScript.
36 lines • 1.13 kB
JavaScript
import { IndicatorSeries } from '../../base/Indicator.js';
import { pushUpdate } from '../../util/pushUpdate.js';
export class SwingHigh extends IndicatorSeries {
#lookback;
#window = [];
#lastEmitted = false;
constructor(config) {
super();
this.#lookback = config.lookback;
}
getRequiredInputs() {
return 2 * this.#lookback + 1;
}
update(candle, replace) {
if (replace && this.#lastEmitted) {
this.rollbackLastResult();
}
this.#lastEmitted = false;
pushUpdate(this.#window, replace, candle.high, this.getRequiredInputs());
if (this.#window.length < this.getRequiredInputs()) {
return null;
}
const pivot = this.#window[this.#lookback];
for (let i = 0; i < this.#window.length; i++) {
if (i === this.#lookback) {
continue;
}
if (this.#window[i] >= pivot) {
return null;
}
}
this.#lastEmitted = true;
return this.setResult(pivot, false);
}
}
//# sourceMappingURL=SwingHigh.js.map