Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(WMA): Fix caching of highest and lowest result #684

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
1 change: 0 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
72 changes: 53 additions & 19 deletions src/WMA/WMA.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/WMA/WMA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -72,7 +72,7 @@ export class FasterWMA extends FasterMovingAverage {

const weightedMa = weightedPricesSum / weightBase;

return this.setResult(weightedMa);
return this.setResult(weightedMa, replace);
}
}
}
1 change: 0 additions & 1 deletion src/WSMA/WSMA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down