Skip to content

Commit

Permalink
fix atr perfomance
Browse files Browse the repository at this point in the history
  • Loading branch information
BusinessDuck committed Dec 4, 2021
1 parent 688f7d1 commit b0edb65
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
18 changes: 17 additions & 1 deletion src/atr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 1 addition & 5 deletions src/wema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit b0edb65

Please sign in to comment.