trading-signals
Version:
Technical indicators to run technical analysis with JavaScript / TypeScript.
48 lines • 1.61 kB
JavaScript
import { IndicatorSeries } from '../../base/Indicator.js';
export class ZigZag extends IndicatorSeries {
#deviation;
#isUp = false;
#highestExtreme = null;
#lowestExtreme = null;
constructor(config) {
super();
this.#deviation = config.deviation;
}
getRequiredInputs() {
return 1;
}
update(candle, replace) {
const low = candle.low;
const high = candle.high;
if (this.#lowestExtreme === null) {
this.#lowestExtreme = low;
}
if (this.#highestExtreme === null) {
this.#highestExtreme = high;
}
if (this.#isUp) {
const uptrendReversal = this.#lowestExtreme + ((this.#highestExtreme - this.#lowestExtreme) * (100 - this.#deviation)) / 100;
if (high > this.#highestExtreme) {
this.#highestExtreme = high;
}
else if (low < uptrendReversal) {
this.#isUp = false;
this.#lowestExtreme = low;
return this.setResult(this.#highestExtreme, replace);
}
}
else {
const downtrendReversal = low + ((this.#highestExtreme - low) * this.#deviation) / 100;
if (low < this.#lowestExtreme) {
this.#lowestExtreme = low;
}
else if (high > downtrendReversal) {
this.#isUp = true;
this.#highestExtreme = high;
return this.setResult(this.#lowestExtreme, replace);
}
}
return null;
}
}
//# sourceMappingURL=ZigZag.js.map