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

feat(MA,SMA,TR): Expose "replace" function #678

Merged
merged 1 commit into from
May 7, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ sma.updates([20, 40, 80]);
sma.update('10');

// You can replace a previous value (useful for live charts):
sma.update('40', true);
sma.replace('40');

// You can add arbitrary-precision decimals:
sma.update(new Big(30));
Expand Down
12 changes: 10 additions & 2 deletions src/Indicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export abstract class BigIndicatorSeries<Input = BigSource> implements Indicator
return (this.result = value);
}

abstract update(input: Input): void | Big;
abstract update(input: Input, replace?: boolean): void | Big;

replace(input: Input) {
return this.update(input, true);
}
}

export abstract class NumberIndicatorSeries<Input = number> implements IndicatorSeries<number, Input> {
Expand Down Expand Up @@ -104,5 +108,9 @@ export abstract class NumberIndicatorSeries<Input = number> implements Indicator
return (this.result = value);
}

abstract update(input: Input): void | number;
abstract update(input: Input, replace?: boolean): void | number;

replace(input: Input) {
return this.update(input, true);
}
}
8 changes: 8 additions & 0 deletions src/MA/MovingAverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export abstract class MovingAverage extends BigIndicatorSeries {
}

abstract update(price: BigSource, replace?: boolean): Big | void;

replace(price: BigSource) {
return this.update(price, true);
}
}

export abstract class FasterMovingAverage extends NumberIndicatorSeries {
Expand All @@ -33,4 +37,8 @@ export abstract class FasterMovingAverage extends NumberIndicatorSeries {
}

abstract update(price: number, replace?: boolean): number | void;

replace(price: number) {
return this.update(price, true);
}
}
15 changes: 12 additions & 3 deletions src/SMA/SMA.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('SMA', () => {
});

describe('update', () => {
it('can replace recently added values', () => {
it('replaces recently added values', () => {
const interval = 3;

const sma = new SMA(interval);
Expand All @@ -32,17 +32,26 @@ describe('SMA', () => {
sma.update(30);
fasterSMA.update(30);

// Add the last candle
sma.update(40);
fasterSMA.update(40);

expect(sma.getResult().toFixed()).toBe('30');
expect(fasterSMA.getResult().toFixed()).toBe('30');

sma.update(10, true);
fasterSMA.update(10, true);
// Replace the last candle with some other candle
sma.replace(10);
fasterSMA.replace(10);

expect(sma.getResult().toFixed()).toBe('20');
expect(fasterSMA.getResult().toFixed()).toBe('20');

// Replace other candle again with the last candle
sma.replace(40);
fasterSMA.replace(40);

expect(sma.getResult().toFixed()).toBe('30');
expect(fasterSMA.getResult().toFixed()).toBe('30');
});
});

Expand Down
16 changes: 8 additions & 8 deletions src/TR/TR.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('TR', () => {
{close: 86.89, high: 86.98, low: 85.76},
{close: 87.77, high: 88.0, low: 87.17},
{close: 87.29, high: 87.87, low: 87.01},
];
] as const;
const expectations = [
'0.86',
'1.25',
Expand All @@ -36,7 +36,7 @@ describe('TR', () => {
'1.22',
'1.11',
'0.86',
];
] as const;

describe('getResult', () => {
it('calculates the True Range (TR)', () => {
Expand Down Expand Up @@ -89,17 +89,17 @@ describe('TR', () => {
expect(tr.getResult().toFixed(2)).toBe(expected);
expect(fasterTR.getResult().toFixed(2)).toBe(expected);

// Replace the last candle with some other candle.
tr.update(otherCandle, true);
fasterTR.update(otherCandle, true);
// Replace the last candle with some other candle
tr.replace(otherCandle);
fasterTR.replace(otherCandle);

expect(tr.getResult().toFixed(2)).not.toBe(expected);
expect(fasterTR.getResult().toFixed(2)).not.toBe(expected);

// Replace other candle again with the last candle.
// Replace other candle again with the last candle
if (lastCandle) {
tr.update(lastCandle, true);
fasterTR.update(lastCandle, true);
tr.replace(lastCandle);
fasterTR.replace(lastCandle);
}

expect(tr.getResult().toFixed(2)).toBe(expected);
Expand Down
Loading