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