UNPKG

trading-signals

Version:

Technical indicators to run technical analysis with JavaScript / TypeScript.

59 lines 2.01 kB
import { IndicatorSeries } from '../../base/Indicator.js'; export class ZigZag extends IndicatorSeries { #deviation; state = { highestExtreme: null, isUp: false, lastCandleReversedTrend: false, lowestExtreme: null, }; constructor(config) { super(); this.#deviation = config.deviation; } getRequiredInputs() { return 1; } update(candle, replace) { const low = candle.low; const high = candle.high; if (replace && this.state.lastCandleReversedTrend) { this.rollbackLastResult(); } this.trackState(replace); const state = this.state; state.lastCandleReversedTrend = false; if (state.lowestExtreme === null) { state.lowestExtreme = low; } if (state.highestExtreme === null) { state.highestExtreme = high; } if (state.isUp) { const uptrendReversal = state.lowestExtreme + ((state.highestExtreme - state.lowestExtreme) * (100 - this.#deviation)) / 100; if (high > state.highestExtreme) { state.highestExtreme = high; } else if (low < uptrendReversal) { state.isUp = false; state.lowestExtreme = low; state.lastCandleReversedTrend = true; return this.setResult(state.highestExtreme, replace); } } else { const downtrendReversal = low + ((state.highestExtreme - low) * this.#deviation) / 100; if (low < state.lowestExtreme) { state.lowestExtreme = low; } else if (high > downtrendReversal) { state.isUp = true; state.highestExtreme = high; state.lastCandleReversedTrend = true; return this.setResult(state.lowestExtreme, replace); } } return null; } } //# sourceMappingURL=ZigZag.js.map