trading-signals
Version:
Technical indicators to run technical analysis with JavaScript / TypeScript.
66 lines • 2.51 kB
JavaScript
import { ATR } from '../../volatility/ATR/ATR.js';
import { IndicatorSeries } from '../../base/Indicator.js';
import { WSMA } from '../WSMA/WSMA.js';
export class DX extends IndicatorSeries {
#movesUp;
#movesDown;
#previousCandle;
#secondLastCandle;
#atr;
mdi;
pdi;
interval;
constructor(interval, SmoothingIndicator = WSMA) {
super();
this.interval = interval;
this.#atr = new ATR(this.interval, SmoothingIndicator);
this.#movesDown = new SmoothingIndicator(this.interval);
this.#movesUp = new SmoothingIndicator(this.interval);
}
#updateState(candle, pdm, mdm, replace) {
this.#atr.update(candle, replace);
this.#movesUp.update(pdm, replace);
this.#movesDown.update(mdm, replace);
if (this.#previousCandle) {
this.#secondLastCandle = this.#previousCandle;
}
this.#previousCandle = candle;
}
getRequiredInputs() {
return this.#movesUp.getRequiredInputs();
}
update(candle, replace) {
if (!this.#previousCandle) {
this.#updateState(candle, 0, 0, replace);
return null;
}
if (this.#secondLastCandle && replace) {
this.#previousCandle = this.#secondLastCandle;
}
const currentHigh = candle.high;
const previousHigh = this.#previousCandle.high;
const currentLow = candle.low;
const previousLow = this.#previousCandle.low;
const higherHigh = currentHigh - previousHigh;
const lowerLow = previousLow - currentLow;
const noHigherHighs = higherHigh < 0;
const lowsRise = higherHigh < lowerLow;
const pdm = noHigherHighs || lowsRise ? 0 : higherHigh;
const noLowerLows = lowerLow < 0;
const highsRise = lowerLow < higherHigh;
const mdm = noLowerLows || highsRise ? 0 : lowerLow;
this.#updateState(candle, pdm, mdm, replace);
if (this.#movesUp.isStable) {
this.pdi = this.#movesUp.getResultOrThrow() / this.#atr.getResultOrThrow();
this.mdi = this.#movesDown.getResultOrThrow() / this.#atr.getResultOrThrow();
const dmDiff = Math.abs(this.pdi - this.mdi);
const dmSum = this.pdi + this.mdi;
if (dmSum === 0) {
return this.setResult(0, replace);
}
return this.setResult((dmDiff / dmSum) * 100, replace);
}
return null;
}
}
//# sourceMappingURL=DX.js.map