diff --git a/src/atr.ts b/src/atr.ts index dd88b25..e408a10 100644 --- a/src/atr.ts +++ b/src/atr.ts @@ -61,7 +61,23 @@ export class ATR { private getTrueRange(high: number, low: number) { if (this.prevClose) { - return Math.max(high - low, Math.abs(high - this.prevClose), Math.abs(low - this.prevClose)); + // Linear conditions without max min and abs + // Perormance reason + const hl = high - low; + const hc = high > this.prevClose ? high - this.prevClose : this.prevClose - high; + const lc = low > this.prevClose ? low - this.prevClose : this.prevClose - low; + + if (hl > hc && hl > lc) { + return hl; + } + + if (hc > hl && hc > lc) { + return hc; + } + + return lc; + + // return Math.max(high - low, Math.abs(high - this.prevClose), Math.abs(low - this.prevClose)); } return null; diff --git a/src/wema.ts b/src/wema.ts index 59a9135..792b029 100644 --- a/src/wema.ts +++ b/src/wema.ts @@ -23,11 +23,7 @@ export class WEMA { return (this.wema = this.sma.nextValue(value)); } - if (this.wema) { - this.wema = (value - this.wema) * this.smooth + this.wema; - } - - return this.wema; + return (this.wema = (value - this.wema) * this.smooth + this.wema); } /**