bennycode / trading-signals

Technical indicators to run technical analysis with JavaScript & TypeScript. 📈
https://bennycode.com/trading-signals
MIT License
596 stars 90 forks source link

bug of update function if 'replace' parameter is true when use FasterEMA #680

Closed nicewookyes closed 4 months ago

nicewookyes commented 4 months ago

The function setResult In indicator.ts will export wrong value while the 'replace' parameter is true, beacuse 'this.previousResult' always replaced by 'this.result'. it should not be replaced if 'replace' parameter is true

protected setResult(value: number, replace: boolean = false): number {
    if (replace && this.previousHighest !== undefined) {
      this.highest = value > this.previousHighest ? value : this.previousHighest;
    } else if (this.highest === undefined || value > this.highest) {
      this.previousHighest = this.highest;
      this.highest = value;
    }

    if (replace && this.previousLowest) {
      this.lowest = value < this.previousLowest ? value : this.previousLowest;
    } else if (this.lowest === undefined || value < this.lowest) {
      this.previousLowest = this.lowest;
      this.lowest = value;
    }

    this.previousResult = this.result;
    return (this.result = value);
  }

Fixed it by

 if(!replace){
      this.previousResult = this.result;
    }