Skip to content

Commit

Permalink
refactor!: Rename "getResult" to "getResultOrThrow"
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed Jan 13, 2025
1 parent c4698d1 commit cfedcd8
Show file tree
Hide file tree
Showing 40 changed files with 300 additions and 296 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ sma.replace('40');
sma.update(new Big(30.0009));

// You can get the result in various formats:
console.log(sma.getResult().toFixed(2)); // "50.00"
console.log(sma.getResult().toFixed(4)); // "50.0003"
console.log(sma.getResultOrThrow().toFixed(2)); // "50.00"
console.log(sma.getResultOrThrow().toFixed(4)); // "50.0003"
```

### When to use `update(...)`?

You have to call an indicator's `update` method to enter input data. The update method may or may not return a result from the indicator depending on whether the minimum amount of input data has been reached.

### When to use `getResult()`?
### When to use `getResultOrThrow()`?

You can call `getResult()` at any point in time, but it throws errors unless an indicator has received the minimum amount of data. If you call `getResult()`, before an indicator has received the required amount of input values, a `NotEnoughDataError` will be thrown.
You can call `getResultOrThrow()` at any point in time, but it throws errors unless an indicator has received the minimum amount of data. If you call `getResultOrThrow()`, before an indicator has received the required amount of input values, a `NotEnoughDataError` will be thrown.

**Example:**

Expand All @@ -119,7 +119,7 @@ sma.update(40);

try {
// We will get an error, because the minimum amount of inputs is 3
sma.getResult();
sma.getResultOrThrow();
} catch (error) {
console.log(error.constructor.name); // "NotEnoughDataError"
}
Expand All @@ -128,7 +128,7 @@ try {
sma.update(70);

// Now, we will receive a proper result
console.log(sma.getResult().valueOf()); // "40"
console.log(sma.getResultOrThrow().valueOf()); // "40"
```

Most of the time, the minimum amount of data depends on the interval / time period used.
Expand Down Expand Up @@ -165,7 +165,7 @@ fasterStochRSI.update(4);
fasterStochRSI.update(5);
fasterStochRSI.update(6);

console.log(fasterStochRSI.getResult());
console.log(fasterStochRSI.getResultOrThrow());
```

### Benchmarks
Expand Down
14 changes: 7 additions & 7 deletions src/ABANDS/AccelerationBands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('AccelerationBands', () => {
});
});

describe('getResult', () => {
describe('getResultOrThrow', () => {
it('returns upper, middle and lower bands', () => {
const accBands = new AccelerationBands(20, 4);
expect(accBands.isStable).toBe(false);
Expand Down Expand Up @@ -52,8 +52,8 @@ describe('AccelerationBands', () => {
fasterAccBands.add({close, high, low});
}

let result = accBands.getResult();
let fasterResult = fasterAccBands.getResult();
let result = accBands.getResultOrThrow();
let fasterResult = fasterAccBands.getResultOrThrow();

// See: https://github.com/QuantConnect/Lean/blob/master/Tests/TestData/spy_acceleration_bands_20_4.txt#L21
expect(accBands.isStable).toBe(true);
Expand All @@ -73,8 +73,8 @@ describe('AccelerationBands', () => {
accBands.add(candle);
fasterAccBands.add(candle);

result = accBands.getResult();
fasterResult = fasterAccBands.getResult();
result = accBands.getResultOrThrow();
fasterResult = fasterAccBands.getResultOrThrow();

expect(result.lower.toFixed(4)).toBe('187.1217');
expect(fasterResult.lower.toFixed(4)).toBe('187.1217');
Expand All @@ -89,15 +89,15 @@ describe('AccelerationBands', () => {
it('throws an error when there is not enough input data', () => {
const accBands = new AccelerationBands(20, 2);
try {
accBands.getResult();
accBands.getResultOrThrow();
throw new Error('Expected error');
} catch (error) {
expect(error).toBeInstanceOf(NotEnoughDataError);
}

const fasterAccBands = new FasterAccelerationBands(20, 2);
try {
fasterAccBands.getResult();
fasterAccBands.getResultOrThrow();
throw new Error('Expected error');
} catch (error) {
expect(error).toBeInstanceOf(NotEnoughDataError);
Expand Down
12 changes: 6 additions & 6 deletions src/ABANDS/AccelerationBands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ export class AccelerationBands extends TechnicalIndicator<BandsResult, HighLowCl

if (this.isStable) {
return (this.result = {
lower: this.lowerBand.getResult(),
middle: this.middleBand.getResult(),
upper: this.upperBand.getResult(),
lower: this.lowerBand.getResultOrThrow(),
middle: this.middleBand.getResultOrThrow(),
upper: this.upperBand.getResultOrThrow(),
});
}

Expand Down Expand Up @@ -95,9 +95,9 @@ export class FasterAccelerationBands extends TechnicalIndicator<FasterBandsResul

if (this.isStable) {
return (this.result = {
lower: this.lowerBand.getResult(),
middle: this.middleBand.getResult(),
upper: this.upperBand.getResult(),
lower: this.lowerBand.getResultOrThrow(),
middle: this.middleBand.getResultOrThrow(),
upper: this.upperBand.getResultOrThrow(),
});
}

Expand Down
14 changes: 7 additions & 7 deletions src/AC/AC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ describe('AC', () => {
acWithReplace.add(wrong);
acWithReplace.replace(correct);

expect(acWithReplace.getResult().toFixed()).toBe(ac.getResult().toFixed());
expect(acWithReplace.getResultOrThrow().toFixed()).toBe(ac.getResultOrThrow().toFixed());
});
});

describe('getResult', () => {
describe('getResultOrThrow', () => {
it('works with a signal line of SMA(5)', () => {
const ac = new AC(5, 34, 5);
const fasterAC = new FasterAC(5, 34, 5);
Expand All @@ -291,11 +291,11 @@ describe('AC', () => {
expect(ac.isStable).toBe(true);
expect(fasterAC.isStable).toBe(true);

expect(ac.getResult().toFixed(2)).toBe('-21.97');
expect(fasterAC.getResult().toFixed(2)).toBe('-21.97');
expect(ac.getResultOrThrow().toFixed(2)).toBe('-21.97');
expect(fasterAC.getResultOrThrow().toFixed(2)).toBe('-21.97');

expect(ac.momentum.getResult().toFixed(2)).toBe('-9.22');
expect(fasterAC.momentum.getResult().toFixed(2)).toBe('-9.22');
expect(ac.momentum.getResultOrThrow().toFixed(2)).toBe('-9.22');
expect(fasterAC.momentum.getResultOrThrow().toFixed(2)).toBe('-9.22');

expect(ac.lowest?.toFixed(2)).toBe('-21.97');
expect(fasterAC.lowest?.toFixed(2)).toBe('-21.97');
Expand All @@ -308,7 +308,7 @@ describe('AC', () => {
const ac = new AC(5, 34, 5);

try {
ac.getResult();
ac.getResultOrThrow();
throw new Error('Expected error');
} catch (error) {
expect(error).toBeInstanceOf(NotEnoughDataError);
Expand Down
4 changes: 2 additions & 2 deletions src/AC/AC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class AC extends BigIndicatorSeries<HighLow> {
if (ao) {
this.signal.update(ao, replace);
if (this.signal.isStable) {
const result = this.setResult(ao.sub(this.signal.getResult()), replace);
const result = this.setResult(ao.sub(this.signal.getResultOrThrow()), replace);
this.momentum.update(result, replace);
return result;
}
Expand Down Expand Up @@ -67,7 +67,7 @@ export class FasterAC extends NumberIndicatorSeries<HighLowNumber> {
if (ao) {
this.signal.update(ao, replace);
if (this.signal.isStable) {
const result = this.setResult(ao - this.signal.getResult(), replace);
const result = this.setResult(ao - this.signal.getResultOrThrow(), replace);
this.momentum.update(result, replace);
return result;
}
Expand Down
14 changes: 7 additions & 7 deletions src/ADX/ADX.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ describe('ADX', () => {
adx.add(correct);
adxWithReplace.add(wrong);

expect(adx.getResult().toFixed()).not.toBe(adxWithReplace.getResult().toFixed());
expect(adx.getResultOrThrow().toFixed()).not.toBe(adxWithReplace.getResultOrThrow().toFixed());

adxWithReplace.replace(correct);

expect(adx.getResult().toFixed()).toBe(adxWithReplace.getResult().toFixed());
expect(adx.getResultOrThrow().toFixed()).toBe(adxWithReplace.getResultOrThrow().toFixed());
});
});

describe('getResult', () => {
describe('getResultOrThrow', () => {
it('calculates the Average Directional Index (ADX)', () => {
const interval = 5;
const adx = new ADX(interval);
Expand All @@ -59,16 +59,16 @@ describe('ADX', () => {
fasterADX.add(candle);
if (adx.isStable && fasterADX.isStable) {
const expected = expectations.shift();
expect(adx.getResult().toFixed(2)).toBe(`${expected}`);
expect(fasterADX.getResult().toFixed(2)).toBe(`${expected}`);
expect(adx.getResultOrThrow().toFixed(2)).toBe(`${expected}`);
expect(fasterADX.getResultOrThrow().toFixed(2)).toBe(`${expected}`);
}
}

expect(adx.isStable).toBe(true);
expect(fasterADX.isStable).toBe(true);

expect(adx.getResult().toFixed(2)).toBe('67.36');
expect(fasterADX.getResult().toFixed(2)).toBe('67.36');
expect(adx.getResultOrThrow().toFixed(2)).toBe('67.36');
expect(fasterADX.getResultOrThrow().toFixed(2)).toBe('67.36');

expect(adx.lowest?.toFixed(2)).toBe('41.38');
expect(fasterADX.lowest?.toFixed(2)).toBe('41.38');
Expand Down
4 changes: 2 additions & 2 deletions src/ADX/ADX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class ADX extends BigIndicatorSeries<HighLowClose> {
}

if (this.smoothed.isStable) {
return this.setResult(this.smoothed.getResult(), replace);
return this.setResult(this.smoothed.getResultOrThrow(), replace);
}

return null;
Expand Down Expand Up @@ -101,7 +101,7 @@ export class FasterADX extends NumberIndicatorSeries<HighLowCloseNumber> {
}

if (this.smoothed.isStable) {
return this.setResult(this.smoothed.getResult(), replace);
return this.setResult(this.smoothed.getResultOrThrow(), replace);
}

return null;
Expand Down
18 changes: 9 additions & 9 deletions src/AO/AO.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('AO', () => {
5.3673, 4.5294, 4.764, 4.1044, 1.6913, -1.3769, -4.2062, -7.7196, -10.6241, -11.4972, -9.6358, -7.9344,
] as const;

describe('getResult', () => {
describe('getResultOrThrow', () => {
it('works with an interval setting of 5/34', () => {
const shortInterval = 5;
const longInterval = 34;
Expand All @@ -48,7 +48,7 @@ describe('AO', () => {
if (ao.isStable && fasterAO.isStable) {
expect(result).not.toBeUndefined();
expect(fasterResult).not.toBeUndefined();
const actual = ao.getResult().toFixed(4);
const actual = ao.getResultOrThrow().toFixed(4);
const expected = aos[i - (longInterval - 1)];
expect(parseFloat(actual)).toBe(expected);
expect(fasterResult?.toFixed(4)).toBe(expected.toFixed(4));
Expand All @@ -66,7 +66,7 @@ describe('AO', () => {
const ao = new AO(5, 34);

try {
ao.getResult();
ao.getResultOrThrow();
throw new Error('Expected error');
} catch (error) {
expect(error).toBeInstanceOf(NotEnoughDataError);
Expand Down Expand Up @@ -109,12 +109,12 @@ describe('AO', () => {
const latestHigh = '749.69';

ao.add(latestValue);
expect(ao.getResult()?.toFixed(2)).toBe(latestResult);
expect(ao.getResultOrThrow()?.toFixed(2)).toBe(latestResult);
expect(ao.lowest?.toFixed(2)).toBe(latestLow);
expect(ao.highest?.toFixed(2), 'new record high').toBe(latestHigh);

fasterAO.add(latestValue);
expect(fasterAO.getResult()?.toFixed(2)).toBe(latestResult);
expect(fasterAO.getResultOrThrow()?.toFixed(2)).toBe(latestResult);
expect(fasterAO.lowest?.toFixed(2)).toBe(latestLow);
expect(fasterAO.highest?.toFixed(2), 'new record high').toBe(latestHigh);

Expand All @@ -128,23 +128,23 @@ describe('AO', () => {
const otherHigh = '33.35';

ao.replace(someOtherValue);
expect(ao.getResult()?.toFixed(2)).toBe(otherResult);
expect(ao.getResultOrThrow()?.toFixed(2)).toBe(otherResult);
expect(ao.lowest?.toFixed(2), 'new record low').toBe(otherLow);
expect(ao.highest?.toFixed(2)).toBe(otherHigh);

fasterAO.replace(someOtherValue);
expect(fasterAO.getResult()?.toFixed(2)).toBe(otherResult);
expect(fasterAO.getResultOrThrow()?.toFixed(2)).toBe(otherResult);
expect(fasterAO.lowest?.toFixed(2), 'new record low').toBe(otherLow);
expect(fasterAO.highest?.toFixed(2)).toBe(otherHigh);

// Replace the other value with the latest value
ao.replace(latestValue);
expect(ao.getResult()?.toFixed(2)).toBe(latestResult);
expect(ao.getResultOrThrow()?.toFixed(2)).toBe(latestResult);
expect(ao.lowest?.toFixed(2), 'lowest reset').toBe(latestLow);
expect(ao.highest?.toFixed(2), 'highest reset').toBe(latestHigh);

fasterAO.replace(latestValue);
expect(fasterAO.getResult()?.toFixed(2)).toBe(latestResult);
expect(fasterAO.getResultOrThrow()?.toFixed(2)).toBe(latestResult);
expect(fasterAO.lowest?.toFixed(2), 'lowest reset').toBe(latestLow);
expect(fasterAO.highest?.toFixed(2), 'highest reset').toBe(latestHigh);
});
Expand Down
4 changes: 2 additions & 2 deletions src/AO/AO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class AO extends BigIndicatorSeries<HighLow> {
this.long.update(medianPrice, replace);

if (this.long.isStable) {
return this.setResult(this.short.getResult().sub(this.long.getResult()), replace);
return this.setResult(this.short.getResultOrThrow().sub(this.long.getResultOrThrow()), replace);
}

return null;
Expand Down Expand Up @@ -69,7 +69,7 @@ export class FasterAO extends NumberIndicatorSeries<HighLowNumber> {
this.long.update(medianPrice, replace);

if (this.long.isStable) {
return this.setResult(this.short.getResult() - this.long.getResult(), replace);
return this.setResult(this.short.getResultOrThrow() - this.long.getResultOrThrow(), replace);
}

return null;
Expand Down
Loading

0 comments on commit cfedcd8

Please sign in to comment.