diff --git a/README.md b/README.md index 69b4d1fb8..4f72f4eaf 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,8 @@ All indicators can be updated over time by streaming data (prices or candles) to 1. Stochastic Oscillator (STOCH) 1. Stochastic RSI (STOCHRSI) 1. True Range (TR) -1. Wilder's Smoothed Moving Average (WSMA / WMA / WWS / SMMA / MEMA) +1. Weighted Moving Average (WMA) +1. Wilder's Smoothed Moving Average (WSMA / WWS / SMMA / MEMA) Utility Methods: diff --git a/docs/README.md b/docs/README.md index 5ddef3d97..c89304c50 100644 --- a/docs/README.md +++ b/docs/README.md @@ -60,7 +60,6 @@ All indicators can be updated over time by streaming data (prices or candles) to 1. Stochastic RSI (STOCHRSI) 1. True Range (TR) 1. Wilder's Smoothed Moving Average (WSMA / WMA / WWS / SMMA / MEMA) -1. Weighted Moving Average (WMA) Utility Methods: diff --git a/src/WMA/WMA.test.ts b/src/WMA/WMA.test.ts index f7e1e1bec..a532d42db 100644 --- a/src/WMA/WMA.test.ts +++ b/src/WMA/WMA.test.ts @@ -31,30 +31,64 @@ describe('WMA', () => { }); }); - describe('update', () => { - it('can replace recently added values', () => { + describe('replace', () => { + it('replaces recently added values', () => { const interval = 3; const wma = new WMA(interval); const fasterWMA = new FasterWMA(interval); - wma.update(30); - fasterWMA.update(30); - - wma.update(60); - fasterWMA.update(60); - - wma.update(90); - fasterWMA.update(90); - - expect(wma.getResult().toFixed()).toBe('70'); - expect(fasterWMA.getResult().toFixed()).toBe('70'); - - wma.update(60, true); - fasterWMA.update(60, true); - - expect(wma.getResult().toFixed()).toBe('55'); - expect(fasterWMA.getResult().toFixed()).toBe('55'); + wma.update(11); + fasterWMA.update(11); + wma.update(12); + fasterWMA.update(12); + wma.update(13); + fasterWMA.update(13); + wma.update(14); + fasterWMA.update(14); + + // Add the latest value + const latestValue = 15; + const latestResult = '14.33'; + const latestLow = '12.33'; + const latestHigh = '14.33'; + + wma.update(latestValue); + expect(wma.getResult().toFixed(2)).toBe(latestResult); + expect(wma.lowest?.toFixed(2)).toBe(latestLow); + expect(wma.highest?.toFixed(2)).toBe(latestHigh); + + fasterWMA.update(latestValue); + expect(fasterWMA.getResult().toFixed(2)).toBe(latestResult); + expect(fasterWMA.lowest?.toFixed(2)).toBe(latestLow); + expect(fasterWMA.highest?.toFixed(2)).toBe(latestHigh); + + // Replace the latest value with some other value + const someOtherValue = 1000; + const otherResult = '506.83'; + const otherLow = '12.33'; + const otherHigh = '506.83'; + + wma.replace(someOtherValue); + expect(wma.getResult().toFixed(2)).toBe(otherResult); + expect(wma.lowest?.toFixed(2)).toBe(otherLow); + expect(wma.highest?.toFixed(2), 'new record high').toBe(otherHigh); + + fasterWMA.replace(someOtherValue); + expect(fasterWMA.getResult().toFixed(2)).toBe(otherResult); + expect(fasterWMA.lowest?.toFixed(2)).toBe(otherLow); + expect(fasterWMA.highest?.toFixed(2), 'new record high').toBe(otherHigh); + + // Replace the other value with the latest value + wma.replace(latestValue); + expect(wma.getResult().toFixed(2)).toBe(latestResult); + expect(wma.lowest?.toFixed(2), 'lowest reset').toBe(latestLow); + expect(wma.highest?.toFixed(2), 'highest reset').toBe(latestHigh); + + fasterWMA.replace(latestValue); + expect(fasterWMA.getResult().toFixed(2)).toBe(latestResult); + expect(fasterWMA.lowest?.toFixed(2), 'lowest reset').toBe(latestLow); + expect(fasterWMA.highest?.toFixed(2), 'highest reset').toBe(latestHigh); }); }); diff --git a/src/WMA/WMA.ts b/src/WMA/WMA.ts index 5a6969ed3..bdcc2624f 100644 --- a/src/WMA/WMA.ts +++ b/src/WMA/WMA.ts @@ -38,7 +38,7 @@ export class WMA extends MovingAverage { const weightBase = (this.interval * (this.interval + 1)) / 2; // the numerator will always be even and the value will be an int. const weightedMa = weightedPricesSum.div(weightBase); - return this.setResult(weightedMa); + return this.setResult(weightedMa, replace); } } } @@ -72,7 +72,7 @@ export class FasterWMA extends FasterMovingAverage { const weightedMa = weightedPricesSum / weightBase; - return this.setResult(weightedMa); + return this.setResult(weightedMa, replace); } } } diff --git a/src/WSMA/WSMA.ts b/src/WSMA/WSMA.ts index 04a159c34..f01e45c4e 100644 --- a/src/WSMA/WSMA.ts +++ b/src/WSMA/WSMA.ts @@ -15,7 +15,6 @@ import {NumberIndicatorSeries} from '../Indicator.js'; * - Modified Exponential Moving Average (MEMA) * - Smoothed Moving Average (SMMA) * - Welles Wilder's Smoothing (WWS) - * - Wilder's Moving Average (WMA) * * @see https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/V-Z/WildersSmoothing */