trading-signals icon indicating copy to clipboard operation
trading-signals copied to clipboard

replace in WSMA

Open ozum opened this issue 1 year ago • 1 comments

I'm trying to add replace to ATR, but replace in WSMA and TR seems wrong. I created PR #666 for TR, but I cannot be sure about WSMA.

I created a test case. Could you please try the test below for WSMA in WSMA.test.ts?

    it('replace recently added values - 2', () => {
      const wsma = new WSMA(3);
      const fasterWSMA = new FasterWSMA(3);
      wsma.update('11');
      fasterWSMA.update(11);
      wsma.update('12');
      fasterWSMA.update(12);
      wsma.update('13');
      fasterWSMA.update(13);
      wsma.update('14');
      fasterWSMA.update(14);
      wsma.update('15');
      fasterWSMA.update(15);

      expect(wsma.getResult().toFixed(2)).toBe('13.44');
      expect(fasterWSMA.getResult().toFixed(2)).toBe('13.44');

      wsma.update('1000', true);
      fasterWSMA.update(1000, true);

      expect(wsma.getResult().toFixed(2)).toBe('341.78');
      expect(fasterWSMA.getResult().toFixed(2)).toBe('341.78');

      wsma.update('15', true);
      fasterWSMA.update(15, true);

      expect(wsma.getResult().toFixed(2)).toBe('13.44');
      expect(fasterWSMA.getResult().toFixed(2)).toBe('13.44');
    });

ozum avatar Apr 15 '24 09:04 ozum

I added some console.log statements to track the cause, and it seems that this.previousResult is not correct.

  update(price: BigSource, replace: boolean = false): Big | void {
    const sma = this.indicator.update(price, replace);
    if (replace && this.previousResult) {
      const smoothed = new Big(price).minus(this.previousResult).mul(this.smoothingFactor);
      console.log('W:', price, smoothed, this.previousResult);
      return this.setResult(smoothed.plus(this.previousResult), replace);
    } else if (!replace && this.result) {
      const smoothed = new Big(price).minus(this.result).mul(this.smoothingFactor);
      console.log('W2:', price, smoothed, this.previousResult);
      return this.setResult(smoothed.plus(this.result), replace);
    } else if (this.result === undefined && sma) {
      return this.setResult(sma, replace);
    }
  }

It's output for my above test case is as follows:

W2: 14 0.66666666666666666666 undefined
W2: 15 0.7777777777777777777722222222222222222222 12
W: 1000 329.1111111111111111078222222222222222222222 12.66666666666666666666
W: 15 0.518518518518518518517407407407407407407374074074074074074074 13.4444444444444444444322222222222222222222

W2: 15 and W: 15 should have the same previous result value.

ozum avatar Apr 15 '24 09:04 ozum

Hi @ozum, thank you for bringing the issue to my attention!

I discovered a bug in how the setResult function in the Indicator base class caches previous values. I have addressed this in the following fix: https://github.com/bennycode/trading-signals/pull/679

Tomorrow, I will review the specific WSMA test cases to determine if they will be resolved by PR #679 or if additional fixes are required.

Best, Benny

bennycode avatar May 07 '24 22:05 bennycode

Hi @ozum, I fixed the issue: https://github.com/bennycode/trading-signals/pull/679

I also added a test case for the "replace" behaviour in WSMA: https://github.com/bennycode/trading-signals/pull/679/commits/f515f04905a092a38de54c90063e219bb3017ba9

Please install [email protected] and let me know if it works. 😀

Best, Benny

bennycode avatar May 08 '24 13:05 bennycode