From cfedcd847b2fd6980011f16c61fb085a5be9511a Mon Sep 17 00:00:00 2001 From: Benny Neugebauer Date: Mon, 13 Jan 2025 12:07:43 +0100 Subject: [PATCH] refactor!: Rename "getResult" to "getResultOrThrow" --- README.md | 14 +++++----- src/ABANDS/AccelerationBands.test.ts | 14 +++++----- src/ABANDS/AccelerationBands.ts | 12 ++++---- src/AC/AC.test.ts | 14 +++++----- src/AC/AC.ts | 4 +-- src/ADX/ADX.test.ts | 14 +++++----- src/ADX/ADX.ts | 4 +-- src/AO/AO.test.ts | 18 ++++++------ src/AO/AO.ts | 4 +-- src/ATR/ATR.test.ts | 32 +++++++++++----------- src/ATR/ATR.ts | 4 +-- src/BBANDS/BollingerBands.test.ts | 14 +++++----- src/BBW/BollingerBandsWidth.test.ts | 6 ++-- src/CCI/CCI.test.ts | 16 +++++------ src/CCI/CCI.ts | 4 +-- src/CG/CG.test.ts | 30 ++++++++++---------- src/DEMA/DEMA.test.ts | 12 ++++---- src/DMA/DMA.test.ts | 22 +++++++-------- src/DMA/DMA.ts | 8 +++--- src/DX/DX.test.ts | 22 +++++++-------- src/DX/DX.ts | 8 +++--- src/EMA/EMA.test.ts | 32 +++++++++++----------- src/EMA/EMA.ts | 8 +++--- src/Indicator.test.ts | 20 +++++++------- src/Indicator.ts | 4 +-- src/MA/MovingAverage.test.ts | 4 +-- src/MACD/MACD.test.ts | 28 +++++++++++-------- src/MAD/MAD.test.ts | 22 +++++++-------- src/MOM/MOM.test.ts | 12 ++++---- src/OBV/OBV.test.ts | 12 ++++---- src/ROC/ROC.test.ts | 6 ++-- src/RSI/RSI.test.ts | 20 +++++++------- src/RSI/RSI.ts | 8 +++--- src/SMA/SMA.test.ts | 28 +++++++++---------- src/STOCH/StochasticOscillator.test.ts | 12 ++++---- src/STOCH/StochasticRSI.test.ts | 12 ++++---- src/TR/TR.test.ts | 22 +++++++-------- src/WMA/WMA.test.ts | 24 ++++++++-------- src/WSMA/WSMA.test.ts | 38 +++++++++++++------------- src/util/Period.test.ts | 8 +++--- 40 files changed, 300 insertions(+), 296 deletions(-) diff --git a/README.md b/README.md index ef5aa999..f3975aec 100644 --- a/README.md +++ b/README.md @@ -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:** @@ -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" } @@ -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. @@ -165,7 +165,7 @@ fasterStochRSI.update(4); fasterStochRSI.update(5); fasterStochRSI.update(6); -console.log(fasterStochRSI.getResult()); +console.log(fasterStochRSI.getResultOrThrow()); ``` ### Benchmarks diff --git a/src/ABANDS/AccelerationBands.test.ts b/src/ABANDS/AccelerationBands.test.ts index 3a459142..6a1eab0a 100644 --- a/src/ABANDS/AccelerationBands.test.ts +++ b/src/ABANDS/AccelerationBands.test.ts @@ -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); @@ -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); @@ -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'); @@ -89,7 +89,7 @@ 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); @@ -97,7 +97,7 @@ describe('AccelerationBands', () => { const fasterAccBands = new FasterAccelerationBands(20, 2); try { - fasterAccBands.getResult(); + fasterAccBands.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); diff --git a/src/ABANDS/AccelerationBands.ts b/src/ABANDS/AccelerationBands.ts index f9b96f45..962e6e8d 100644 --- a/src/ABANDS/AccelerationBands.ts +++ b/src/ABANDS/AccelerationBands.ts @@ -59,9 +59,9 @@ export class AccelerationBands extends TechnicalIndicator { 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); @@ -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'); @@ -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); diff --git a/src/AC/AC.ts b/src/AC/AC.ts index 0de6a1ad..77887684 100644 --- a/src/AC/AC.ts +++ b/src/AC/AC.ts @@ -37,7 +37,7 @@ export class AC extends BigIndicatorSeries { 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; } @@ -67,7 +67,7 @@ export class FasterAC extends NumberIndicatorSeries { 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; } diff --git a/src/ADX/ADX.test.ts b/src/ADX/ADX.test.ts index 89b79eb0..f6452ccf 100644 --- a/src/ADX/ADX.test.ts +++ b/src/ADX/ADX.test.ts @@ -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); @@ -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'); diff --git a/src/ADX/ADX.ts b/src/ADX/ADX.ts index fcc67c64..ed2c98e7 100644 --- a/src/ADX/ADX.ts +++ b/src/ADX/ADX.ts @@ -65,7 +65,7 @@ export class ADX extends BigIndicatorSeries { } if (this.smoothed.isStable) { - return this.setResult(this.smoothed.getResult(), replace); + return this.setResult(this.smoothed.getResultOrThrow(), replace); } return null; @@ -101,7 +101,7 @@ export class FasterADX extends NumberIndicatorSeries { } if (this.smoothed.isStable) { - return this.setResult(this.smoothed.getResult(), replace); + return this.setResult(this.smoothed.getResultOrThrow(), replace); } return null; diff --git a/src/AO/AO.test.ts b/src/AO/AO.test.ts index 59b0ea3d..b2b572e7 100644 --- a/src/AO/AO.test.ts +++ b/src/AO/AO.test.ts @@ -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; @@ -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)); @@ -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); @@ -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); @@ -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); }); diff --git a/src/AO/AO.ts b/src/AO/AO.ts index 9fb1f800..018d9203 100644 --- a/src/AO/AO.ts +++ b/src/AO/AO.ts @@ -41,7 +41,7 @@ export class AO extends BigIndicatorSeries { 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; @@ -69,7 +69,7 @@ export class FasterAO extends NumberIndicatorSeries { 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; diff --git a/src/ATR/ATR.test.ts b/src/ATR/ATR.test.ts index 0d166fb6..f2267d5d 100644 --- a/src/ATR/ATR.test.ts +++ b/src/ATR/ATR.test.ts @@ -50,15 +50,15 @@ describe('ATR', () => { atr.add(correct); atrWithReplace.add(wrong); - expect(atr.getResult().toFixed()).not.toBe(atrWithReplace.getResult().toFixed()); + expect(atr.getResultOrThrow().toFixed()).not.toBe(atrWithReplace.getResultOrThrow().toFixed()); atrWithReplace.replace(correct); - expect(atr.getResult().toFixed()).toBe(atrWithReplace.getResult().toFixed()); + expect(atr.getResultOrThrow().toFixed()).toBe(atrWithReplace.getResultOrThrow().toFixed()); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the Average True Range (ATR)', () => { const interval = 5; const atr = new ATR(interval); @@ -70,19 +70,19 @@ describe('ATR', () => { fasterATR.add(candle); if (atr.isStable && fasterATR.isStable) { const expected = expectations[i - (interval - 1)]; - expect(atr.getResult().toFixed(2)).toBe(expected!); - expect(fasterATR.getResult().toFixed(2)).toBe(expected!); + expect(atr.getResultOrThrow().toFixed(2)).toBe(expected!); + expect(fasterATR.getResultOrThrow().toFixed(2)).toBe(expected!); } } expect(atr.isStable).toBe(true); expect(fasterATR.isStable).toBe(true); - expect(atr.getResult().toFixed(2)).toBe('1.14'); + expect(atr.getResultOrThrow().toFixed(2)).toBe('1.14'); expect(atr.lowest?.toFixed(2)).toBe('1.01'); expect(atr.highest?.toFixed(2)).toBe('1.24'); - expect(fasterATR.getResult().toFixed(2)).toBe('1.14'); + expect(fasterATR.getResultOrThrow().toFixed(2)).toBe('1.14'); expect(fasterATR.lowest?.toFixed(2)).toBe('1.01'); expect(fasterATR.highest?.toFixed(2)).toBe('1.24'); }); @@ -91,7 +91,7 @@ describe('ATR', () => { const atr = new ATR(14); try { - atr.getResult(); + atr.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -110,11 +110,11 @@ describe('ATR', () => { fasterATR.add(candle); } - expect(atr.getResult().toFixed(2)).toBe('1.14'); + expect(atr.getResultOrThrow().toFixed(2)).toBe('1.14'); expect(atr.lowest?.toFixed(2)).toBe('1.01'); expect(atr.highest?.toFixed(2)).toBe('1.24'); - expect(fasterATR.getResult().toFixed(2)).toBe('1.14'); + expect(fasterATR.getResultOrThrow().toFixed(2)).toBe('1.14'); expect(fasterATR.lowest?.toFixed(2)).toBe('1.01'); expect(fasterATR.highest?.toFixed(2)).toBe('1.24'); @@ -125,12 +125,12 @@ describe('ATR', () => { const latestHigh = '250.85'; atr.add(latestValue); - expect(atr.getResult().toFixed(2)).toBe(latestResult); + expect(atr.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(atr.lowest?.toFixed(2)).toBe(latestLow); expect(atr.highest?.toFixed(2), 'new record high').toBe(latestHigh); fasterATR.add(latestValue); - expect(fasterATR.getResult().toFixed(2)).toBe(latestResult); + expect(fasterATR.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(fasterATR.lowest?.toFixed(2)).toBe(latestLow); expect(fasterATR.highest?.toFixed(2), 'new record high').toBe(latestHigh); @@ -145,23 +145,23 @@ describe('ATR', () => { const otherHigh = '18.17'; atr.replace(someOtherValue); - expect(atr.getResult().toFixed(2)).toBe(otherResult); + expect(atr.getResultOrThrow().toFixed(2)).toBe(otherResult); expect(atr.lowest?.toFixed(2)).toBe(otherLow); expect(atr.highest?.toFixed(2)).toBe(otherHigh); fasterATR.replace(someOtherValue); - expect(fasterATR.getResult().toFixed(2)).toBe(otherResult); + expect(fasterATR.getResultOrThrow().toFixed(2)).toBe(otherResult); expect(fasterATR.lowest?.toFixed(2)).toBe(otherLow); expect(fasterATR.highest?.toFixed(2)).toBe(otherHigh); // Replace the other value with the latest value atr.replace(latestValue); - expect(atr.getResult().toFixed(2)).toBe(latestResult); + expect(atr.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(atr.lowest?.toFixed(2), 'lowest reset').toBe(latestLow); expect(atr.highest?.toFixed(2), 'highest reset').toBe(latestHigh); fasterATR.replace(latestValue); - expect(fasterATR.getResult().toFixed(2)).toBe(latestResult); + expect(fasterATR.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(fasterATR.lowest?.toFixed(2), 'lowest reset').toBe(latestLow); expect(fasterATR.highest?.toFixed(2), 'highest reset').toBe(latestHigh); }); diff --git a/src/ATR/ATR.ts b/src/ATR/ATR.ts index 14f27609..831ffafb 100644 --- a/src/ATR/ATR.ts +++ b/src/ATR/ATR.ts @@ -44,7 +44,7 @@ export class ATR extends BigIndicatorSeries { const trueRange = this.tr.update(candle, replace); this.smoothing.update(trueRange, replace); if (this.smoothing.isStable) { - return this.setResult(this.smoothing.getResult(), replace); + return this.setResult(this.smoothing.getResultOrThrow(), replace); } return null; } @@ -67,7 +67,7 @@ export class FasterATR extends NumberIndicatorSeries { const trueRange = this.tr.update(candle, replace); this.smoothing.update(trueRange, replace); if (this.smoothing.isStable) { - return this.setResult(this.smoothing.getResult(), replace); + return this.setResult(this.smoothing.getResultOrThrow(), replace); } return null; diff --git a/src/BBANDS/BollingerBands.test.ts b/src/BBANDS/BollingerBands.test.ts index 4a622c71..01f292b6 100644 --- a/src/BBANDS/BollingerBands.test.ts +++ b/src/BBANDS/BollingerBands.test.ts @@ -20,7 +20,7 @@ describe('BollingerBands', () => { }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates Bollinger Bands with interval 20', () => { const bb = new BollingerBands(20); @@ -35,7 +35,7 @@ describe('BollingerBands', () => { const resLower = new Big(Number(data.lower[index])); const resUpper = new Big(Number(data.upper[index])); - const {middle, upper, lower} = bb.getResult(); + const {middle, upper, lower} = bb.getResultOrThrow(); expect(middle.toPrecision(12)).toEqual(resMiddle.toPrecision(12)); expect(lower.toPrecision(12)).toEqual(resLower.toPrecision(12)); @@ -53,7 +53,7 @@ describe('BollingerBands', () => { const bb = new BollingerBands(20); try { - bb.getResult(); + bb.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -127,7 +127,7 @@ describe('BollingerBands', () => { const price = inputs[i]; bb.add(price); if (bb.isStable) { - const {lower, middle, upper} = bb.getResult(); + const {lower, middle, upper} = bb.getResultOrThrow(); const expectedLow = expectedLows[i]; const expectedMid = expectedMids[i]; const expectedUp = expectedUps[i]; @@ -141,7 +141,7 @@ describe('BollingerBands', () => { }); describe('FasterBollingerBands', () => { - describe('getResult', () => { + describe('getResultOrThrow', () => { it('only works with plain numbers', () => { // Test data verified with: // https://tulipindicators.org/bbands @@ -151,7 +151,7 @@ describe('FasterBollingerBands', () => { const fasterBB = new FasterBollingerBands(5, 2); fasterBB.updates(prices, false); expect(fasterBB.isStable).toBe(true); - const actual = fasterBB.getResult(); + const actual = fasterBB.getResultOrThrow(); expect(actual.lower.toFixed(2)).toBe('85.29'); expect(actual.middle.toFixed(2)).toBe('86.80'); expect(actual.upper.toFixed(2)).toBe('88.32'); @@ -161,7 +161,7 @@ describe('FasterBollingerBands', () => { const fasterBB = new FasterBollingerBands(5); try { - fasterBB.getResult(); + fasterBB.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); diff --git a/src/BBW/BollingerBandsWidth.test.ts b/src/BBW/BollingerBandsWidth.test.ts index 4e76fbae..d583e0ec 100644 --- a/src/BBW/BollingerBandsWidth.test.ts +++ b/src/BBW/BollingerBandsWidth.test.ts @@ -2,7 +2,7 @@ import {BollingerBands, FasterBollingerBands} from '../BBANDS/BollingerBands.js' import {BollingerBandsWidth, FasterBollingerBandsWidth} from './BollingerBandsWidth.js'; describe('BollingerBandsWidth', () => { - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the Bollinger Bands Width (BBW)', () => { // eBay Inc. (EBAY) daily stock prices in USD on NASDAQ with CBOE BZX exchange const candles = [ @@ -51,8 +51,8 @@ describe('BollingerBandsWidth', () => { fasterBBW.add(close); if (bbw.isStable) { const expected = expectations.shift()!; - expect(bbw.getResult().toFixed(2)).toBe(`${expected}`); - expect(fasterBBW.getResult().toFixed(2)).toBe(expected); + expect(bbw.getResultOrThrow().toFixed(2)).toBe(`${expected}`); + expect(fasterBBW.getResultOrThrow().toFixed(2)).toBe(expected); } } }); diff --git a/src/CCI/CCI.test.ts b/src/CCI/CCI.test.ts index 2deb9ecc..5e4de2be 100644 --- a/src/CCI/CCI.test.ts +++ b/src/CCI/CCI.test.ts @@ -52,15 +52,15 @@ describe('CCI', () => { cci.add(correct); cciWithReplace.add(wrong); - expect(cci.getResult().toFixed()).not.toBe(cciWithReplace.getResult().toFixed()); + expect(cci.getResultOrThrow().toFixed()).not.toBe(cciWithReplace.getResultOrThrow().toFixed()); cciWithReplace.replace(correct); - expect(cci.getResult().toFixed()).toBe(cciWithReplace.getResult().toFixed()); + expect(cci.getResultOrThrow().toFixed()).toBe(cciWithReplace.getResultOrThrow().toFixed()); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the Commodity Channel Index (CCI)', () => { const interval = 5; const cci = new CCI(interval); @@ -70,11 +70,11 @@ describe('CCI', () => { fasterCCI.add(candle); if (cci.isStable && fasterCCI.isStable) { const expected = expectations.shift(); - expect(cci.getResult().toFixed(2)).toBe(expected); - expect(fasterCCI.getResult().toFixed(2)).toBe(expected); + expect(cci.getResultOrThrow().toFixed(2)).toBe(expected); + expect(fasterCCI.getResultOrThrow().toFixed(2)).toBe(expected); } } - const actual = cci.getResult().toFixed(2); + const actual = cci.getResultOrThrow().toFixed(2); expect(actual).toBe('71.93'); }); @@ -94,7 +94,7 @@ describe('CCI', () => { it('throws an error when there is not enough input data', () => { const cci = new CCI(5); try { - cci.getResult(); + cci.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -102,7 +102,7 @@ describe('CCI', () => { const fasterCCI = new FasterCCI(5); try { - fasterCCI.getResult(); + fasterCCI.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); diff --git a/src/CCI/CCI.ts b/src/CCI/CCI.ts index 5e436b38..69153e12 100644 --- a/src/CCI/CCI.ts +++ b/src/CCI/CCI.ts @@ -45,7 +45,7 @@ export class CCI extends BigIndicatorSeries { this.sma.update(typicalPrice, replace); if (this.sma.isStable) { - const mean = this.sma.getResult(); + const mean = this.sma.getResultOrThrow(); const meanDeviation = MAD.getResultFromBatch(this.typicalPrices, mean); const numerator = typicalPrice.minus(mean); const denominator = new Big(0.015).mul(meanDeviation); @@ -78,7 +78,7 @@ export class FasterCCI extends NumberIndicatorSeries { this.sma.update(typicalPrice, replace); if (this.sma.isStable) { - const mean = this.sma.getResult(); + const mean = this.sma.getResultOrThrow(); const meanDeviation = FasterMAD.getResultFromBatch(this.typicalPrices, mean); const numerator = typicalPrice - mean; const denominator = 0.015 * meanDeviation; diff --git a/src/CG/CG.test.ts b/src/CG/CG.test.ts index a18f0321..f9cbce66 100644 --- a/src/CG/CG.test.ts +++ b/src/CG/CG.test.ts @@ -53,13 +53,13 @@ describe('CG', () => { cg.add(latestValue); expect(cg.prices.length).toBe(5); - expect(cg.getResult().toFixed(4)).toBe(latestResult); + expect(cg.getResultOrThrow().toFixed(4)).toBe(latestResult); expect(cg.lowest?.toFixed(4)).toBe(latestLow); expect(cg.highest?.toFixed(4)).toBe(latestHigh); fasterCG.add(latestValue); expect(fasterCG.prices.length).toBe(5); - expect(fasterCG.getResult().toFixed(4)).toBe(latestResult); + expect(fasterCG.getResultOrThrow().toFixed(4)).toBe(latestResult); expect(fasterCG.lowest?.toFixed(4)).toBe(latestLow); expect(fasterCG.highest?.toFixed(4)).toBe(latestHigh); @@ -71,32 +71,32 @@ describe('CG', () => { cg.replace(someOtherValue); expect(cg.prices.length).toBe(5); - expect(cg.getResult().toFixed(4)).toBe(otherResult); + expect(cg.getResultOrThrow().toFixed(4)).toBe(otherResult); expect(cg.lowest?.toFixed(4)).toBe(otherLow); expect(cg.highest?.toFixed(4)).toBe(otherHigh); fasterCG.replace(someOtherValue); expect(fasterCG.prices.length).toBe(5); - expect(fasterCG.getResult().toFixed(4)).toBe(otherResult); + expect(fasterCG.getResultOrThrow().toFixed(4)).toBe(otherResult); expect(fasterCG.lowest?.toFixed(4)).toBe(otherLow); expect(fasterCG.highest?.toFixed(4)).toBe(otherHigh); // Replace the other value with the latest value cg.replace(latestValue); expect(cg.prices.length).toBe(5); - expect(cg.getResult().toFixed(4)).toBe(latestResult); + expect(cg.getResultOrThrow().toFixed(4)).toBe(latestResult); expect(cg.lowest?.toFixed(4)).toBe(latestLow); expect(cg.highest?.toFixed(4)).toBe(latestHigh); fasterCG.replace(latestValue); expect(fasterCG.prices.length).toBe(5); - expect(fasterCG.getResult().toFixed(4)).toBe(latestResult); + expect(fasterCG.getResultOrThrow().toFixed(4)).toBe(latestResult); expect(fasterCG.lowest?.toFixed(4)).toBe(latestLow); expect(fasterCG.highest?.toFixed(4)).toBe(latestHigh); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('indicates a downtrend when the center of gravity falls below the signal line', () => { const cg = new CG(5, 10); const fasterCG = new FasterCG(5, 10); @@ -105,8 +105,8 @@ describe('CG', () => { cg.add(value); fasterCG.add(value); } - let cgResult = cg.getResult(); - let signalResult = cg.signal.getResult(); + let cgResult = cg.getResultOrThrow(); + let signalResult = cg.signal.getResultOrThrow(); expect(cgResult.gt(signalResult)).toBe(true); [150, 110, 90, 130].forEach(price => { cg.add(price); @@ -116,12 +116,12 @@ describe('CG', () => { expect(cg.isStable).toBe(true); expect(fasterCG.isStable).toBe(true); - cgResult = cg.getResult(); - signalResult = cg.signal.getResult(); + cgResult = cg.getResultOrThrow(); + signalResult = cg.signal.getResultOrThrow(); expect(cgResult.gt(signalResult)).toBe(false); expect(cgResult.toFixed(4)).toBe('2.7059'); - expect(fasterCG.getResult().toFixed(4)).toBe('2.7059'); + expect(fasterCG.getResultOrThrow().toFixed(4)).toBe('2.7059'); expect(cg.lowest?.toFixed(4)).toBe('2.6081'); expect(fasterCG.lowest?.toFixed(4)).toBe('2.6081'); @@ -134,7 +134,7 @@ describe('CG', () => { const cg = new CG(10, 20); try { - cg.getResult(); + cg.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -144,11 +144,11 @@ describe('CG', () => { it('is protected against division by zero errors', () => { const cg = new CG(1, 1); cg.add(0); - expect(cg.getResult().valueOf()).toBe('0'); + expect(cg.getResultOrThrow().valueOf()).toBe('0'); const fasterCG = new FasterCG(1, 1); fasterCG.add(0); - expect(fasterCG.getResult().valueOf()).toBe(0); + expect(fasterCG.getResultOrThrow().valueOf()).toBe(0); }); }); }); diff --git a/src/DEMA/DEMA.test.ts b/src/DEMA/DEMA.test.ts index 044af316..0fa4e70e 100644 --- a/src/DEMA/DEMA.test.ts +++ b/src/DEMA/DEMA.test.ts @@ -43,12 +43,12 @@ describe('DEMA', () => { expect(dema.isStable).toBe(true); expect(fasterDEMA.isStable).toBe(true); - expect(dema.getResult().toFixed(2)).toBe('48.96'); - expect(fasterDEMA.getResult().toFixed(2)).toBe('48.96'); + expect(dema.getResultOrThrow().toFixed(2)).toBe('48.96'); + expect(fasterDEMA.getResultOrThrow().toFixed(2)).toBe('48.96'); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the DEMA with interval 10', () => { const prices = [ 81, 24, 75, 21, 34, 25, 72, 92, 99, 2, 86, 80, 76, 8, 87, 75, 32, 65, 41, 9, 13, 26, 56, 28, 65, 58, 17, 90, 87, @@ -63,8 +63,8 @@ describe('DEMA', () => { fasterDEMA.add(price); if (dema.isStable) { const result = new Big(dema10results[index]); - expect(dema.getResult().toPrecision(12)).toEqual(result.toPrecision(12)); - expect(fasterDEMA.getResult().toPrecision(4)).toEqual(result.toPrecision(4)); + expect(dema.getResultOrThrow().toPrecision(12)).toEqual(result.toPrecision(12)); + expect(fasterDEMA.getResultOrThrow().toPrecision(4)).toEqual(result.toPrecision(4)); } }); @@ -82,7 +82,7 @@ describe('DEMA', () => { const dema = new DEMA(10); try { - dema.getResult(); + dema.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); diff --git a/src/DMA/DMA.test.ts b/src/DMA/DMA.test.ts index 045689f0..ae9193db 100644 --- a/src/DMA/DMA.test.ts +++ b/src/DMA/DMA.test.ts @@ -11,15 +11,15 @@ describe('DMA', () => { dma.update(30, true); expect(dma.isStable).toBe(true); - expect(dma.getResult().short.toFixed(8)).toBe('53.57000000'); - expect(dma.getResult().long.toFixed(8)).toBe('43.26833333'); + expect(dma.getResultOrThrow().short.toFixed(8)).toBe('53.57000000'); + expect(dma.getResultOrThrow().long.toFixed(8)).toBe('43.26833333'); fasterDMA.updates([41, 37, 20.9, 100, 30.71, 40], false); fasterDMA.update(30, true); expect(fasterDMA.isStable).toBe(true); - expect(fasterDMA.getResult().short.toFixed(8)).toBe('53.57000000'); - expect(fasterDMA.getResult().long.toFixed(8)).toBe('43.26833333'); + expect(fasterDMA.getResultOrThrow().short.toFixed(8)).toBe('53.57000000'); + expect(fasterDMA.getResultOrThrow().long.toFixed(8)).toBe('43.26833333'); }); }); @@ -32,8 +32,8 @@ describe('DMA', () => { dma.add(100); dma.add(30.71); dma.add(30); - expect(dma.getResult().short.toFixed(8)).toBe('53.57000000'); - expect(dma.getResult().long.toFixed(8)).toBe('43.26833333'); + expect(dma.getResultOrThrow().short.toFixed(8)).toBe('53.57000000'); + expect(dma.getResultOrThrow().long.toFixed(8)).toBe('43.26833333'); }); it('can be used with exponential moving averages', () => { @@ -44,8 +44,8 @@ describe('DMA', () => { dma.add(100); dma.add(30.71); dma.add(30); - expect(dma.getResult().short.toFixed(8)).toBe('38.92125000'); - expect(dma.getResult().long.toFixed(8)).toBe('41.96735289'); + expect(dma.getResultOrThrow().short.toFixed(8)).toBe('38.92125000'); + expect(dma.getResultOrThrow().long.toFixed(8)).toBe('41.96735289'); }); }); @@ -73,7 +73,7 @@ describe('DMA', () => { }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('detects uptrends', () => { const dma = new DMA(3, 8); const fasterDMA = new FasterDMA(3, 8); @@ -85,11 +85,11 @@ describe('DMA', () => { fasterDMA.add(parseFloat(price)); } - const {short, long} = dma.getResult(); + const {short, long} = dma.getResultOrThrow(); expect(dma.isStable).toBe(true); expect(short.gt(long)).toBe(true); - const fasterResult = fasterDMA.getResult(); + const fasterResult = fasterDMA.getResultOrThrow(); expect(fasterDMA.isStable).toBe(true); expect(fasterResult.short > fasterResult.long).toBe(true); }); diff --git a/src/DMA/DMA.ts b/src/DMA/DMA.ts index b41dca86..bb484ac1 100644 --- a/src/DMA/DMA.ts +++ b/src/DMA/DMA.ts @@ -43,8 +43,8 @@ export class DMA extends TechnicalIndicator { if (this.isStable) { return (this.result = { - long: this.long.getResult(), - short: this.short.getResult(), + long: this.long.getResultOrThrow(), + short: this.short.getResultOrThrow(), }); } @@ -72,8 +72,8 @@ export class FasterDMA extends TechnicalIndicator { if (this.isStable) { return (this.result = { - long: this.long.getResult(), - short: this.short.getResult(), + long: this.long.getResultOrThrow(), + short: this.short.getResultOrThrow(), }); } diff --git a/src/DX/DX.test.ts b/src/DX/DX.test.ts index 98fe7baa..87b05cd9 100644 --- a/src/DX/DX.test.ts +++ b/src/DX/DX.test.ts @@ -63,18 +63,18 @@ describe('DX', () => { fasterDXWithReplace.add(wrong); // We need to verify the four decimal places, as the results are otherwise too similar: - expect(dx.getResult().toFixed(4)).not.toBe(dxWithReplace.getResult().toFixed(4)); - expect(fasterDX.getResult().toFixed(4)).not.toBe(fasterDXWithReplace.getResult().toFixed(4)); + expect(dx.getResultOrThrow().toFixed(4)).not.toBe(dxWithReplace.getResultOrThrow().toFixed(4)); + expect(fasterDX.getResultOrThrow().toFixed(4)).not.toBe(fasterDXWithReplace.getResultOrThrow().toFixed(4)); dxWithReplace.replace(correct); fasterDXWithReplace.replace(correct); - expect(dx.getResult().toFixed(4)).toBe(dxWithReplace.getResult().toFixed(4)); - expect(fasterDX.getResult().toFixed(4)).toBe(fasterDXWithReplace.getResult().toFixed(4)); + expect(dx.getResultOrThrow().toFixed(4)).toBe(dxWithReplace.getResultOrThrow().toFixed(4)); + expect(fasterDX.getResultOrThrow().toFixed(4)).toBe(fasterDXWithReplace.getResultOrThrow().toFixed(4)); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the Directional Movement Index (DX)', () => { const interval = 5; const dx = new DX(interval); @@ -85,16 +85,16 @@ describe('DX', () => { fasterDX.add(candle); if (dx.isStable && fasterDX.isStable) { const expected = expectations.shift(); - expect(dx.getResult().toFixed(2)).toBe(expected); - expect(fasterDX.getResult().toFixed(2)).toBe(expected); + expect(dx.getResultOrThrow().toFixed(2)).toBe(expected); + expect(fasterDX.getResultOrThrow().toFixed(2)).toBe(expected); } } expect(dx.isStable).toBe(true); expect(fasterDX.isStable).toBe(true); - expect(dx.getResult().toFixed(2)).toBe('75.61'); - expect(fasterDX.getResult().toFixed(2)).toBe('75.61'); + expect(dx.getResultOrThrow().toFixed(2)).toBe('75.61'); + expect(fasterDX.getResultOrThrow().toFixed(2)).toBe('75.61'); expect(dx.lowest?.toFixed(2)).toBe('11.09'); expect(fasterDX.lowest?.toFixed(2)).toBe('11.09'); @@ -121,8 +121,8 @@ describe('DX', () => { expect(dx.isStable).toBe(true); expect(fasterDX.isStable).toBe(true); - expect(dx.getResult().valueOf()).toBe('0'); - expect(fasterDX.getResult().valueOf()).toBe(0); + expect(dx.getResultOrThrow().valueOf()).toBe('0'); + expect(fasterDX.getResultOrThrow().valueOf()).toBe(0); }); }); }); diff --git a/src/DX/DX.ts b/src/DX/DX.ts index 84535ef3..5360ae41 100644 --- a/src/DX/DX.ts +++ b/src/DX/DX.ts @@ -85,8 +85,8 @@ export class DX extends BigIndicatorSeries { this.updateState(candle, pdm, mdm, replace); if (this.movesUp.isStable) { - this.pdi = this.movesUp.getResult().div(this.atr.getResult()); - this.mdi = this.movesDown.getResult().div(this.atr.getResult()); + this.pdi = this.movesUp.getResultOrThrow().div(this.atr.getResultOrThrow()); + this.mdi = this.movesDown.getResultOrThrow().div(this.atr.getResultOrThrow()); const dmDiff = this.pdi.minus(this.mdi).abs(); const dmSum = this.pdi.plus(this.mdi); @@ -164,8 +164,8 @@ export class FasterDX extends NumberIndicatorSeries { this.updateState(candle, pdm, mdm, replace); if (this.movesUp.isStable) { - this.pdi = this.movesUp.getResult() / this.atr.getResult(); - this.mdi = this.movesDown.getResult() / this.atr.getResult(); + this.pdi = this.movesUp.getResultOrThrow() / this.atr.getResultOrThrow(); + this.mdi = this.movesDown.getResultOrThrow() / this.atr.getResultOrThrow(); const dmDiff = Math.abs(this.pdi - this.mdi); const dmSum = this.pdi + this.mdi; diff --git a/src/EMA/EMA.test.ts b/src/EMA/EMA.test.ts index 5fa8389e..62db6798 100644 --- a/src/EMA/EMA.test.ts +++ b/src/EMA/EMA.test.ts @@ -35,8 +35,8 @@ describe('EMA', () => { emaWithReplace.replace(prices[3]); emaWithReplace.add(prices[4]); - const actual = emaWithReplace.getResult().toFixed(); - const expected = ema.getResult().toFixed(); + const actual = emaWithReplace.getResultOrThrow().toFixed(); + const expected = ema.getResultOrThrow().toFixed(); expect(actual).toBe(expected); }); @@ -61,12 +61,12 @@ describe('EMA', () => { const latestHigh = '84.84'; ema.add(latestValue); - expect(ema.getResult()?.toFixed(2)).toBe(latestResult); + expect(ema.getResultOrThrow()?.toFixed(2)).toBe(latestResult); expect(ema.lowest?.toFixed(2)).toBe(latestLow); expect(ema.highest?.toFixed(2)).toBe(latestHigh); fasterEMA.add(latestValue); - expect(fasterEMA.getResult()?.toFixed(2)).toBe(latestResult); + expect(fasterEMA.getResultOrThrow()?.toFixed(2)).toBe(latestResult); expect(fasterEMA.lowest?.toFixed(2)).toBe(latestLow); expect(fasterEMA.highest?.toFixed(2)).toBe(latestHigh); @@ -77,23 +77,23 @@ describe('EMA', () => { const otherHigh = '331.71'; ema.replace(someOtherValue); - expect(ema.getResult()?.toFixed(2)).toBe(otherResult); + expect(ema.getResultOrThrow()?.toFixed(2)).toBe(otherResult); expect(ema.lowest?.toFixed(2)).toBe(otherLow); expect(ema.highest?.toFixed(2)).toBe(otherHigh); fasterEMA.replace(someOtherValue); - expect(fasterEMA.getResult()?.toFixed(2)).toBe(otherResult); + expect(fasterEMA.getResultOrThrow()?.toFixed(2)).toBe(otherResult); expect(fasterEMA.lowest?.toFixed(2)).toBe(otherLow); expect(fasterEMA.highest?.toFixed(2)).toBe(otherHigh); // Replace the other value with the latest value ema.replace(latestValue); - expect(ema.getResult()?.toFixed(2)).toBe(latestResult); + expect(ema.getResultOrThrow()?.toFixed(2)).toBe(latestResult); expect(ema.lowest?.toFixed(2)).toBe(latestLow); expect(ema.highest?.toFixed(2)).toBe(latestHigh); fasterEMA.replace(latestValue); - expect(fasterEMA.getResult()?.toFixed(2)).toBe(latestResult); + expect(fasterEMA.getResultOrThrow()?.toFixed(2)).toBe(latestResult); expect(fasterEMA.lowest?.toFixed(2)).toBe(latestLow); expect(fasterEMA.highest?.toFixed(2)).toBe(latestHigh); }); @@ -105,7 +105,7 @@ describe('EMA', () => { ema.add(prices[2]); ema.add(prices[3]); ema.add(prices[4]); - expect(ema.getResult().toFixed(2)).toBe('82.71'); + expect(ema.getResultOrThrow().toFixed(2)).toBe('82.71'); const fasterEMA = new FasterEMA(5); fasterEMA.update(prices[0], true); @@ -116,7 +116,7 @@ describe('EMA', () => { }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the Exponential Moving Average over a period of 5', () => { const interval = 5; const ema = new EMA(interval); @@ -127,19 +127,19 @@ describe('EMA', () => { fasterEMA.add(price); if (ema.isStable && fasterEMA.isStable) { const expected = expectations[i - (interval - 1)]; - expect(ema.getResult().toFixed(2)).toBe(expected); - expect(fasterEMA.getResult().toFixed(2)).toBe(expected); + expect(ema.getResultOrThrow().toFixed(2)).toBe(expected); + expect(fasterEMA.getResultOrThrow().toFixed(2)).toBe(expected); } } - expect(ema.getResult().toFixed(2)).toBe('86.70'); - expect(fasterEMA.getResult().toFixed(2)).toBe('86.70'); + expect(ema.getResultOrThrow().toFixed(2)).toBe('86.70'); + expect(fasterEMA.getResultOrThrow().toFixed(2)).toBe('86.70'); }); it('throws an error when there is not enough input data', () => { const ema = new EMA(10); try { - ema.getResult(); + ema.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -149,7 +149,7 @@ describe('EMA', () => { const fasterEMA = new FasterEMA(10); try { - fasterEMA.getResult(); + fasterEMA.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); diff --git a/src/EMA/EMA.ts b/src/EMA/EMA.ts index 9eca55a1..1f978c1b 100644 --- a/src/EMA/EMA.ts +++ b/src/EMA/EMA.ts @@ -41,7 +41,7 @@ export class EMA extends MovingAverage { ); } - override getResult(): Big { + override getResultOrThrow(): Big { if (this.pricesCounter < this.interval) { throw new NotEnoughDataError(); } @@ -51,7 +51,7 @@ export class EMA extends MovingAverage { override get isStable(): boolean { try { - this.getResult(); + this.getResultOrThrow(); return true; } catch { return false; @@ -84,7 +84,7 @@ export class FasterEMA extends FasterMovingAverage { ); } - override getResult(): number { + override getResultOrThrow(): number { if (this.pricesCounter < this.interval) { throw new NotEnoughDataError(); } @@ -94,7 +94,7 @@ export class FasterEMA extends FasterMovingAverage { override get isStable(): boolean { try { - this.getResult(); + this.getResultOrThrow(); return true; } catch { return false; diff --git a/src/Indicator.test.ts b/src/Indicator.test.ts index 4713dbfe..0e9e3bba 100644 --- a/src/Indicator.test.ts +++ b/src/Indicator.test.ts @@ -25,11 +25,11 @@ describe('Indicator', () => { }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('throws an error when there is not enough input data', () => { const itc = new IndicatorTestClass(); try { - itc.getResult(); + itc.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -39,7 +39,7 @@ describe('Indicator', () => { it('returns the cross sum', () => { const itc = new IndicatorTestClass(); itc.updates([20, 40], false); - expect(itc.getResult().toString()).toBe('30'); + expect(itc.getResultOrThrow().toString()).toBe('30'); }); }); @@ -51,45 +51,45 @@ describe('Indicator', () => { itc.add(100); expect(itc.inputs.length).toBe(1); - expect(itc.getResult().toString()).toBe('100'); + expect(itc.getResultOrThrow().toString()).toBe('100'); expect(itc.lowest?.toString()).toBe('100'); expect(itc.highest?.toString()).toBe('100'); itc.replace(200); expect(itc.inputs.length).toBe(1); - expect(itc.getResult().toString()).toBe('200'); + expect(itc.getResultOrThrow().toString()).toBe('200'); expect(itc.lowest?.toString()).toBe('200'); expect(itc.highest?.toString()).toBe('200'); itc.replace(60); expect(itc.inputs.length).toBe(1); - expect(itc.getResult().toString()).toBe('60'); + expect(itc.getResultOrThrow().toString()).toBe('60'); expect(itc.lowest?.toString()).toBe('60'); expect(itc.highest?.toString()).toBe('60'); itc.add(20); expect(itc.inputs.length).toBe(2); - expect(itc.getResult().toString()).toBe('40'); + expect(itc.getResultOrThrow().toString()).toBe('40'); expect(itc.lowest?.toString(), 'lowest cross sum seen (60+20/2)').toBe('40'); expect(itc.highest?.toString(), 'highest cross sum seen (60/1)').toBe('60'); // Replacing an update with itself should be a "noop" itc.replace(20); expect(itc.inputs.length).toBe(2); - expect(itc.getResult().toString()).toBe('40'); + expect(itc.getResultOrThrow().toString()).toBe('40'); expect(itc.lowest?.toString(), 'lowest cross sum seen (60+20/2)').toBe('40'); expect(itc.highest?.toString(), 'highest cross sum seen (60/1)').toBe('60'); itc.add(211); expect(itc.inputs.length).toBe(3); - expect(itc.getResult().toString()).toBe('97'); + expect(itc.getResultOrThrow().toString()).toBe('97'); expect(itc.lowest?.toString()).toBe('40'); expect(itc.highest?.toString()).toBe('97'); // Replacing an update with itself should be a "noop" itc.replace(211); expect(itc.inputs.length).toBe(3); - expect(itc.getResult().toString()).toBe('97'); + expect(itc.getResultOrThrow().toString()).toBe('97'); expect(itc.lowest?.toString()).toBe('40'); expect(itc.highest?.toString()).toBe('97'); }); diff --git a/src/Indicator.ts b/src/Indicator.ts index 14c845b1..70cccc03 100644 --- a/src/Indicator.ts +++ b/src/Indicator.ts @@ -3,7 +3,7 @@ import {NotEnoughDataError} from './error/NotEnoughDataError.js'; import {getLastFromForEach} from './util/getLastFromForEach.js'; interface Indicator { - getResult(): Result; + getResultOrThrow(): Result; isStable: boolean; add(input: Input): Result | null; replace(input: Input): Result | null; @@ -14,7 +14,7 @@ interface Indicator { export abstract class TechnicalIndicator implements Indicator { protected result: Result | undefined; - getResult() { + getResultOrThrow() { if (this.result === undefined) { throw new NotEnoughDataError(); } diff --git a/src/MA/MovingAverage.test.ts b/src/MA/MovingAverage.test.ts index 786c1ef1..ea5539ae 100644 --- a/src/MA/MovingAverage.test.ts +++ b/src/MA/MovingAverage.test.ts @@ -18,10 +18,10 @@ describe('FasterMovingAverage', () => { it('can be used to implement custom average calculations based on primitive numbers', () => { const average = new MyAverage(Infinity); expect(average.isStable).toBe(false); - expect(() => average.getResult()).toThrowError(); + expect(() => average.getResultOrThrow()).toThrowError(); average.add(50); average.add(100); - const result = average.getResult(); + const result = average.getResultOrThrow(); expect(result).toBe(75); }); }); diff --git a/src/MACD/MACD.test.ts b/src/MACD/MACD.test.ts index 478e06a0..d33026d1 100644 --- a/src/MACD/MACD.test.ts +++ b/src/MACD/MACD.test.ts @@ -25,11 +25,15 @@ describe('MACD', () => { macdWithReplace.replace(90); macdWithReplace.add('83.61'); - expect(macdWithReplace.short.getResult().toFixed(), 'short').toBe(macd.short.getResult().toFixed()); - expect(macdWithReplace.long.getResult().toFixed(), 'long').toBe(macd.long.getResult().toFixed()); - expect(macdWithReplace.getResult().histogram.toFixed(), 'histogram').toBe(macd.getResult().histogram.toFixed()); - expect(macdWithReplace.getResult().macd.toFixed(), 'macd').toBe(macd.getResult().macd.toFixed()); - expect(macdWithReplace.getResult().signal.toFixed(), 'signal').toBe(macd.getResult().signal.toFixed()); + expect(macdWithReplace.short.getResultOrThrow().toFixed(), 'short').toBe(macd.short.getResultOrThrow().toFixed()); + expect(macdWithReplace.long.getResultOrThrow().toFixed(), 'long').toBe(macd.long.getResultOrThrow().toFixed()); + expect(macdWithReplace.getResultOrThrow().histogram.toFixed(), 'histogram').toBe( + macd.getResultOrThrow().histogram.toFixed() + ); + expect(macdWithReplace.getResultOrThrow().macd.toFixed(), 'macd').toBe(macd.getResultOrThrow().macd.toFixed()); + expect(macdWithReplace.getResultOrThrow().signal.toFixed(), 'signal').toBe( + macd.getResultOrThrow().signal.toFixed() + ); }); }); @@ -55,12 +59,12 @@ describe('MACD', () => { expect(macd.isStable).toBe(true); expect(fasterMACD.isStable).toBe(true); - expect(macd.getResult().macd.toFixed(2)).toBe('0.62'); - expect(fasterMACD.getResult().macd.toFixed(2)).toBe('0.62'); + expect(macd.getResultOrThrow().macd.toFixed(2)).toBe('0.62'); + expect(fasterMACD.getResultOrThrow().macd.toFixed(2)).toBe('0.62'); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('is compatible with results from Tulip Indicators (TI)', () => { // Test data verified with: // https://tulipindicators.org/macd @@ -142,8 +146,8 @@ describe('MACD', () => { const expectedMacdHistogram = expectedMacdHistograms[key]!; if (expectedMacd !== undefined) { - const result = macd.getResult(); - const fasterResult = fasterMACD.getResult(); + const result = macd.getResultOrThrow(); + const fasterResult = fasterMACD.getResultOrThrow(); expect(result.macd.toFixed(2)).toBe(expectedMacd); expect(fasterResult.macd.toFixed(2)).toBe(expectedMacd); @@ -169,7 +173,7 @@ describe('MACD', () => { }); try { - macd.getResult(); + macd.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -178,7 +182,7 @@ describe('MACD', () => { const fasterMACD = new FasterMACD(new FasterEMA(12), new FasterEMA(26), new FasterEMA(9)); try { - fasterMACD.getResult(); + fasterMACD.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); diff --git a/src/MAD/MAD.test.ts b/src/MAD/MAD.test.ts index ee190a58..f4bf4b6a 100644 --- a/src/MAD/MAD.test.ts +++ b/src/MAD/MAD.test.ts @@ -35,12 +35,12 @@ describe('MAD', () => { expect(mad.isStable).toBe(true); expect(fasterMAD.isStable).toBe(true); - expect(mad.getResult().toFixed(2)).toBe('0.39'); - expect(fasterMAD.getResult().toFixed(2)).toBe('0.39'); + expect(mad.getResultOrThrow().toFixed(2)).toBe('0.39'); + expect(fasterMAD.getResultOrThrow().toFixed(2)).toBe('0.39'); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the absolute deviation from the mean over a period', () => { // Test data verified with: // https://en.wikipedia.org/wiki/Average_absolute_deviation#Mean_absolute_deviation_around_a_central_point @@ -51,9 +51,9 @@ describe('MAD', () => { mad.add(price); fasterMAD.add(price); } - const actual = mad.getResult().valueOf(); + const actual = mad.getResultOrThrow().valueOf(); expect(actual).toBe('3.6'); - expect(fasterMAD.getResult().valueOf()).toBe(3.6); + expect(fasterMAD.getResultOrThrow().valueOf()).toBe(3.6); }); it('is compatible with results from Tulip Indicators (TI)', () => { @@ -64,12 +64,12 @@ describe('MAD', () => { fasterMAD.add(price); if (mad.isStable && fasterMAD.isStable) { const expected = expectations.shift()!; - expect(mad.getResult().toFixed(2)).toBe(expected); - expect(fasterMAD.getResult().toFixed(2)).toBe(expected); + expect(mad.getResultOrThrow().toFixed(2)).toBe(expected); + expect(fasterMAD.getResultOrThrow().toFixed(2)).toBe(expected); } } - expect(mad.getResult().toFixed(2)).toBe('0.62'); - expect(fasterMAD.getResult().toFixed(2)).toBe('0.62'); + expect(mad.getResultOrThrow().toFixed(2)).toBe('0.62'); + expect(fasterMAD.getResultOrThrow().toFixed(2)).toBe('0.62'); }); it("stores the highest and lowest result throughout the indicator's lifetime", () => { @@ -88,7 +88,7 @@ describe('MAD', () => { it('throws an error when there is not enough input data', () => { const mad = new MAD(5); try { - mad.getResult(); + mad.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -96,7 +96,7 @@ describe('MAD', () => { const fasterMAD = new FasterMAD(5); try { - fasterMAD.getResult(); + fasterMAD.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); diff --git a/src/MOM/MOM.test.ts b/src/MOM/MOM.test.ts index 9a721b3a..55ae85ea 100644 --- a/src/MOM/MOM.test.ts +++ b/src/MOM/MOM.test.ts @@ -25,12 +25,12 @@ describe('MOM', () => { expect(momentum.isStable).toBe(true); expect(fasterMomentum.isStable).toBe(true); - expect(momentum.getResult().toFixed(2)).toBe('1.56'); - expect(fasterMomentum.getResult().toFixed(2)).toBe('1.56'); + expect(momentum.getResultOrThrow().toFixed(2)).toBe('1.56'); + expect(fasterMomentum.getResultOrThrow().toFixed(2)).toBe('1.56'); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('returns the price 5 intervals ago', () => { // Test data verified with: // https://github.com/TulipCharts/tulipindicators/blob/v0.8.0/tests/untest.txt#L286-L288 @@ -45,10 +45,10 @@ describe('MOM', () => { momentum.add(input); fasterMomentum.add(input); if (momentum.isStable && fasterMomentum.isStable) { - const actual = momentum.getResult().toFixed(3); + const actual = momentum.getResultOrThrow().toFixed(3); const expected = outputs.shift()!; expect(parseFloat(actual)).toBe(expected); - expect(fasterMomentum.getResult().toFixed(2)).toBe(expected.toFixed(2)); + expect(fasterMomentum.getResultOrThrow().toFixed(2)).toBe(expected.toFixed(2)); } } @@ -66,7 +66,7 @@ describe('MOM', () => { const momentum = new MOM(5); try { - momentum.getResult(); + momentum.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); diff --git a/src/OBV/OBV.test.ts b/src/OBV/OBV.test.ts index bbdcbbe9..736341c9 100644 --- a/src/OBV/OBV.test.ts +++ b/src/OBV/OBV.test.ts @@ -2,7 +2,7 @@ import {FasterOBV, OBV} from './OBV.js'; import {NotEnoughDataError} from '../error/index.js'; describe('OBV', () => { - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the relative strength index', () => { // Test data verified with: // https://www.investopedia.com/terms/o/onbalancevolume.asp#mntl-sc-block_1-0-27 @@ -33,15 +33,15 @@ describe('OBV', () => { fasterOBV.add(candle); if (obv.isStable && fasterOBV.isStable) { const expected = expectations.shift(); - expect(obv.getResult().toFixed(3)).toBe(expected!); - expect(fasterOBV.getResult().toFixed(3)).toBe(expected!); + expect(obv.getResultOrThrow().toFixed(3)).toBe(expected!); + expect(fasterOBV.getResultOrThrow().toFixed(3)).toBe(expected!); } } expect(obv.isStable).toBe(true); expect(fasterOBV.isStable).toBe(true); - expect(obv.getResult().toFixed(2)).toBe('72100.00'); - expect(fasterOBV.getResult().toFixed(2)).toBe('72100.00'); + expect(obv.getResultOrThrow().toFixed(2)).toBe('72100.00'); + expect(fasterOBV.getResultOrThrow().toFixed(2)).toBe('72100.00'); expect(obv.lowest?.toFixed(2)).toBe('600.00'); expect(fasterOBV.lowest?.toFixed(2)).toBe('600.00'); @@ -54,7 +54,7 @@ describe('OBV', () => { const obv = new OBV(); expect(obv.isStable).toBe(false); try { - obv.getResult(); + obv.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(obv.isStable).toBe(false); diff --git a/src/ROC/ROC.test.ts b/src/ROC/ROC.test.ts index 6ba5189b..5578c5a1 100644 --- a/src/ROC/ROC.test.ts +++ b/src/ROC/ROC.test.ts @@ -3,7 +3,7 @@ import {FasterROC, ROC} from './ROC.js'; import {NotEnoughDataError} from '../error/index.js'; describe('ROC', () => { - describe('getResult', () => { + describe('getResultOrThrow', () => { it('identifies an up-trending asset by a positive ROC', () => { // Test data verified with: // https://tulipindicators.org/roc @@ -25,7 +25,7 @@ describe('ROC', () => { if (roc.isStable) { const expected = expectations.shift()!; - expect(roc.getResult().toFixed(2)).toEqual(expected.toFixed(2)); + expect(roc.getResultOrThrow().toFixed(2)).toEqual(expected.toFixed(2)); } } @@ -53,7 +53,7 @@ describe('ROC', () => { const roc = new ROC(6); try { - roc.getResult(); + roc.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); diff --git a/src/RSI/RSI.test.ts b/src/RSI/RSI.test.ts index c6dbbcee..36683c7a 100644 --- a/src/RSI/RSI.test.ts +++ b/src/RSI/RSI.test.ts @@ -28,12 +28,12 @@ describe('RSI', () => { expect(rsi.isStable).toBe(true); expect(fasterRSI.isStable).toBe(true); - expect(rsi.getResult().toFixed(2)).toBe('75.94'); - expect(fasterRSI.getResult().toFixed(2)).toBe('75.94'); + expect(rsi.getResultOrThrow().toFixed(2)).toBe('75.94'); + expect(fasterRSI.getResultOrThrow().toFixed(2)).toBe('75.94'); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the relative strength index', () => { // Test data verified with: // https://github.com/TulipCharts/tulipindicators/blob/v0.8.0/tests/untest.txt#L347-L349 @@ -59,15 +59,15 @@ describe('RSI', () => { fasterRSI.add(price); if (rsi.isStable && fasterRSI.isStable) { const expected = expectations.shift(); - expect(rsi.getResult().toFixed(3)).toBe(expected!); - expect(fasterRSI.getResult().toFixed(3)).toBe(expected!); + expect(rsi.getResultOrThrow().toFixed(3)).toBe(expected!); + expect(fasterRSI.getResultOrThrow().toFixed(3)).toBe(expected!); } } expect(rsi.isStable).toBe(true); expect(fasterRSI.isStable).toBe(true); - expect(rsi.getResult().toFixed(2)).toBe('78.50'); - expect(fasterRSI.getResult().toFixed(2)).toBe('78.50'); + expect(rsi.getResultOrThrow().toFixed(2)).toBe('78.50'); + expect(fasterRSI.getResultOrThrow().toFixed(2)).toBe('78.50'); expect(rsi.lowest?.toFixed(2)).toBe('64.93'); expect(fasterRSI.lowest?.toFixed(2)).toBe('64.93'); @@ -81,11 +81,11 @@ describe('RSI', () => { const rsi = new RSI(2); rsi.updates(updates, false); - expect(rsi.getResult().valueOf()).toBe('100'); + expect(rsi.getResultOrThrow().valueOf()).toBe('100'); const fasterRSI = new FasterRSI(2); fasterRSI.updates(updates, false); - expect(fasterRSI.getResult().valueOf()).toBe(100); + expect(fasterRSI.getResultOrThrow().valueOf()).toBe(100); }); it('throws an error when there is not enough input data', () => { @@ -93,7 +93,7 @@ describe('RSI', () => { rsi.add(0); expect(rsi.isStable).toBe(false); try { - rsi.getResult(); + rsi.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(rsi.isStable).toBe(false); diff --git a/src/RSI/RSI.ts b/src/RSI/RSI.ts index a8f25f9a..5045d906 100644 --- a/src/RSI/RSI.ts +++ b/src/RSI/RSI.ts @@ -64,12 +64,12 @@ export class RSI extends BigIndicatorSeries { } if (this.avgGain.isStable) { - const avgLoss = this.avgLoss.getResult(); + const avgLoss = this.avgLoss.getResultOrThrow(); // Prevent division by zero: https://github.com/bennycode/trading-signals/issues/378 if (avgLoss.eq(0)) { return this.setResult(new Big(100), replace); } - const relativeStrength = this.avgGain.getResult().div(avgLoss); + const relativeStrength = this.avgGain.getResultOrThrow().div(avgLoss); return this.setResult(this.maxValue.minus(this.maxValue.div(relativeStrength.add(1))), replace); } @@ -117,12 +117,12 @@ export class FasterRSI extends NumberIndicatorSeries { } if (this.avgGain.isStable) { - const avgLoss = this.avgLoss.getResult(); + const avgLoss = this.avgLoss.getResultOrThrow(); // Prevent division by zero: https://github.com/bennycode/trading-signals/issues/378 if (avgLoss === 0) { return this.setResult(100, replace); } - const relativeStrength = this.avgGain.getResult() / avgLoss; + const relativeStrength = this.avgGain.getResultOrThrow() / avgLoss; return this.setResult(this.maxValue - this.maxValue / (relativeStrength + 1), replace); } diff --git a/src/SMA/SMA.test.ts b/src/SMA/SMA.test.ts index 0a9a967a..402a8206 100644 --- a/src/SMA/SMA.test.ts +++ b/src/SMA/SMA.test.ts @@ -39,12 +39,12 @@ describe('SMA', () => { const latestHigh = '35.00'; sma.add(latestValue); - expect(sma.getResult().toFixed(2)).toBe(latestResult); + expect(sma.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(sma.lowest?.toFixed(2)).toBe(latestLow); expect(sma.highest?.toFixed(2)).toBe(latestHigh); fasterSMA.add(latestValue); - expect(fasterSMA.getResult().toFixed(2)).toBe(latestResult); + expect(fasterSMA.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(fasterSMA.lowest?.toFixed(2)).toBe(latestLow); expect(fasterSMA.highest?.toFixed(2)).toBe(latestHigh); @@ -55,23 +55,23 @@ describe('SMA', () => { const otherHigh = '56.00'; sma.replace(someOtherValue); - expect(sma.getResult().toFixed(2)).toBe(otherResult); + expect(sma.getResultOrThrow().toFixed(2)).toBe(otherResult); expect(sma.lowest?.toFixed(2)).toBe(otherLow); expect(sma.highest?.toFixed(2)).toBe(otherHigh); fasterSMA.replace(someOtherValue); - expect(fasterSMA.getResult().toFixed(2)).toBe(otherResult); + expect(fasterSMA.getResultOrThrow().toFixed(2)).toBe(otherResult); expect(fasterSMA.lowest?.toFixed(2)).toBe(otherLow); expect(fasterSMA.highest?.toFixed(2)).toBe(otherHigh); // Replace the other value with the latest value sma.replace(latestValue); - expect(sma.getResult().toFixed(2)).toBe(latestResult); + expect(sma.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(sma.lowest?.toFixed(2)).toBe(latestLow); expect(sma.highest?.toFixed(2)).toBe(latestHigh); fasterSMA.replace(latestValue); - expect(fasterSMA.getResult().toFixed(2)).toBe(latestResult); + expect(fasterSMA.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(fasterSMA.lowest?.toFixed(2)).toBe(latestLow); expect(fasterSMA.highest?.toFixed(2)).toBe(latestHigh); }); @@ -87,8 +87,8 @@ describe('SMA', () => { sma.updates(prices, false); fasterSMA.updates(prices, false); - expect(sma.getResult().toFixed()).toBe('3'); - expect(fasterSMA.getResult().toFixed()).toBe('3'); + expect(sma.getResultOrThrow().toFixed()).toBe('3'); + expect(fasterSMA.getResultOrThrow().toFixed()).toBe('3'); }); }); @@ -102,13 +102,13 @@ describe('SMA', () => { expect(sma.isStable).toBe(true); sma.add('10'); sma.add(new Big(30)); - expect(sma.getResult().valueOf()).toBe('20'); + expect(sma.getResultOrThrow().valueOf()).toBe('20'); expect(sma.lowest?.toFixed(2)).toBe('20.00'); expect(sma.highest?.toFixed(2)).toBe('30.00'); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the moving average based on the last 5 prices', () => { // Test data verified with: // https://github.com/TulipCharts/tulipindicators/blob/v0.8.0/tests/untest.txt#L359-L361 @@ -148,8 +148,8 @@ describe('SMA', () => { expect(sma.isStable).toBe(true); expect(fasterSMA.isStable).toBe(true); - expect(sma.getResult().toFixed(3)).toBe('86.804'); - expect(fasterSMA.getResult()).toBe(86.804); + expect(sma.getResultOrThrow().toFixed(3)).toBe('86.804'); + expect(fasterSMA.getResultOrThrow()).toBe(86.804); expect(sma.highest?.toFixed(2)).toBe('86.80'); expect(fasterSMA.highest?.toFixed(2)).toBe('86.80'); @@ -162,7 +162,7 @@ describe('SMA', () => { const sma = new SMA(26); try { - sma.getResult(); + sma.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -171,7 +171,7 @@ describe('SMA', () => { const fasterSMA = new FasterSMA(5); try { - fasterSMA.getResult(); + fasterSMA.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); diff --git a/src/STOCH/StochasticOscillator.test.ts b/src/STOCH/StochasticOscillator.test.ts index 1984d23c..a72bf2f1 100644 --- a/src/STOCH/StochasticOscillator.test.ts +++ b/src/STOCH/StochasticOscillator.test.ts @@ -49,12 +49,12 @@ describe('StochasticOscillator', () => { expect(stoch.isStable).toBe(true); expect(fasterStoch.isStable).toBe(true); - expect(stoch.getResult().stochK.toFixed(2)).toBe('91.09'); - expect(fasterStoch.getResult().stochK.toFixed(2)).toBe('91.09'); + expect(stoch.getResultOrThrow().stochK.toFixed(2)).toBe('91.09'); + expect(fasterStoch.getResultOrThrow().stochK.toFixed(2)).toBe('91.09'); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('throws an error when there is not enough input data', () => { const stoch = new StochasticOscillator(5, 3, 3); @@ -66,7 +66,7 @@ describe('StochasticOscillator', () => { stoch.add({close: 1, high: 6, low: 1}); // Emits 2nd of 3 required values for %d period try { - stoch.getResult(); + stoch.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -75,7 +75,7 @@ describe('StochasticOscillator', () => { const fasterStoch = new FasterStochasticOscillator(5, 3, 3); try { - fasterStoch.getResult(); + fasterStoch.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -109,7 +109,7 @@ describe('StochasticOscillator', () => { ], false ); - const {stochK, stochD} = fasterStoch.getResult(); + const {stochK, stochD} = fasterStoch.getResultOrThrow(); expect(stochK.toFixed(2)).toBe('0.00'); expect(stochD.toFixed(2)).toBe('0.00'); }); diff --git a/src/STOCH/StochasticRSI.test.ts b/src/STOCH/StochasticRSI.test.ts index ebb428b9..45abf284 100644 --- a/src/STOCH/StochasticRSI.test.ts +++ b/src/STOCH/StochasticRSI.test.ts @@ -11,11 +11,11 @@ describe('StochasticRSI', () => { stochRSIWithReplace.updates([2, 2, 2, 1], false); stochRSIWithReplace.replace(2); - expect(stochRSI.getResult().valueOf()).toBe(stochRSIWithReplace.getResult().valueOf()); + expect(stochRSI.getResultOrThrow().valueOf()).toBe(stochRSIWithReplace.getResultOrThrow().valueOf()); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the Stochastic RSI', () => { // Test data verified with: // https://github.com/TulipCharts/tulipindicators/blob/0bc8dfc46cfc89366bf8cef6dfad1fb6f81b3b7b/tests/untest.txt#L382-L384 @@ -38,8 +38,8 @@ describe('StochasticRSI', () => { expect(stochRSI.isStable).toBe(true); expect(fasterStochRSI.isStable).toBe(true); - expect(stochRSI.getResult().valueOf()).toBe('0'); - expect(fasterStochRSI.getResult()).toBe(0); + expect(stochRSI.getResultOrThrow().valueOf()).toBe('0'); + expect(fasterStochRSI.getResultOrThrow()).toBe(0); expect(stochRSI.highest!.valueOf()).toBe('1'); expect(fasterStochRSI.highest!.valueOf()).toBe(1); @@ -52,11 +52,11 @@ describe('StochasticRSI', () => { const interval = 2; const stochRSI = new StochasticRSI(interval); stochRSI.updates([2, 2, 2, 2], false); - expect(stochRSI.getResult().valueOf()).toBe('100'); + expect(stochRSI.getResultOrThrow().valueOf()).toBe('100'); const fasterStochRSI = new FasterStochasticRSI(interval); fasterStochRSI.updates([2, 2, 2, 2], false); - expect(fasterStochRSI.getResult().valueOf()).toBe(100); + expect(fasterStochRSI.getResultOrThrow().valueOf()).toBe(100); }); }); }); diff --git a/src/TR/TR.test.ts b/src/TR/TR.test.ts index f4a6bef2..cf2b0e66 100644 --- a/src/TR/TR.test.ts +++ b/src/TR/TR.test.ts @@ -38,7 +38,7 @@ describe('TR', () => { '0.86', ] as const; - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the True Range (TR)', () => { const tr = new TR(); const fasterTR = new FasterTR(); @@ -48,16 +48,16 @@ describe('TR', () => { fasterTR.add(candle); if (tr.isStable && fasterTR.isStable) { const expected = expectations[i]; - expect(tr.getResult().toFixed(2)).toBe(expected!); - expect(fasterTR.getResult().toFixed(2)).toBe(expected!); + expect(tr.getResultOrThrow().toFixed(2)).toBe(expected!); + expect(fasterTR.getResultOrThrow().toFixed(2)).toBe(expected!); } }); expect(tr.isStable).toBe(true); expect(fasterTR.isStable).toBe(true); - expect(tr.getResult().toFixed(2)).toBe('0.86'); - expect(fasterTR.getResult().toFixed(2)).toBe('0.86'); + expect(tr.getResultOrThrow().toFixed(2)).toBe('0.86'); + expect(fasterTR.getResultOrThrow().toFixed(2)).toBe('0.86'); expect(tr.lowest?.toFixed(2)).toBe('0.65'); expect(fasterTR.lowest?.toFixed(2)).toBe('0.65'); @@ -89,11 +89,11 @@ describe('TR', () => { fasterTR.add(lastCandle); } - expect(tr.getResult().toFixed(2)).toBe(latestResult); + expect(tr.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(tr.lowest?.toFixed(2)).toBe(latestLow); expect(tr.highest?.toFixed(2)).toBe(latestHigh); - expect(fasterTR.getResult().toFixed(2)).toBe(latestResult); + expect(fasterTR.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(fasterTR.lowest?.toFixed(2)).toBe(latestLow); expect(fasterTR.highest?.toFixed(2)).toBe(latestHigh); @@ -104,12 +104,12 @@ describe('TR', () => { const otherHigh = '3.14'; tr.replace(someOtherValue); - expect(tr.getResult().toFixed(2)).toBe(otherResult); + expect(tr.getResultOrThrow().toFixed(2)).toBe(otherResult); expect(tr.lowest?.toFixed(2)).toBe(otherLow); expect(tr.highest?.toFixed(2)).toBe(otherHigh); fasterTR.replace(someOtherValue); - expect(fasterTR.getResult().toFixed(2)).toBe(otherResult); + expect(fasterTR.getResultOrThrow().toFixed(2)).toBe(otherResult); expect(fasterTR.lowest?.toFixed(2)).toBe(otherLow); expect(fasterTR.highest?.toFixed(2)).toBe(otherHigh); @@ -119,11 +119,11 @@ describe('TR', () => { fasterTR.replace(lastCandle); } - expect(tr.getResult().toFixed(2)).toBe(latestResult); + expect(tr.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(tr.lowest?.toFixed(2)).toBe(latestLow); expect(tr.highest?.toFixed(2)).toBe(latestHigh); - expect(fasterTR.getResult().toFixed(2)).toBe(latestResult); + expect(fasterTR.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(fasterTR.lowest?.toFixed(2)).toBe(latestLow); expect(fasterTR.highest?.toFixed(2)).toBe(latestHigh); }); diff --git a/src/WMA/WMA.test.ts b/src/WMA/WMA.test.ts index a7849787..1090ce61 100644 --- a/src/WMA/WMA.test.ts +++ b/src/WMA/WMA.test.ts @@ -54,12 +54,12 @@ describe('WMA', () => { const latestHigh = '14.33'; wma.add(latestValue); - expect(wma.getResult().toFixed(2)).toBe(latestResult); + expect(wma.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(wma.lowest?.toFixed(2)).toBe(latestLow); expect(wma.highest?.toFixed(2)).toBe(latestHigh); fasterWMA.add(latestValue); - expect(fasterWMA.getResult().toFixed(2)).toBe(latestResult); + expect(fasterWMA.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(fasterWMA.lowest?.toFixed(2)).toBe(latestLow); expect(fasterWMA.highest?.toFixed(2)).toBe(latestHigh); @@ -70,23 +70,23 @@ describe('WMA', () => { const otherHigh = '506.83'; wma.replace(someOtherValue); - expect(wma.getResult().toFixed(2)).toBe(otherResult); + expect(wma.getResultOrThrow().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.getResultOrThrow().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.getResultOrThrow().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.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(fasterWMA.lowest?.toFixed(2), 'lowest reset').toBe(latestLow); expect(fasterWMA.highest?.toFixed(2), 'highest reset').toBe(latestHigh); }); @@ -102,8 +102,8 @@ describe('WMA', () => { wma.updates(prices, false); fasterWMA.updates(prices, false); - expect(wma.getResult().toFixed()).toBe('74'); - expect(fasterWMA.getResult().toFixed()).toBe('74'); + expect(wma.getResultOrThrow().toFixed()).toBe('74'); + expect(fasterWMA.getResultOrThrow().toFixed()).toBe('74'); }); }); @@ -117,13 +117,13 @@ describe('WMA', () => { expect(wma.isStable).toBe(true); wma.add('120'); wma.add(new Big(60)); - expect(wma.getResult().valueOf()).toBe('85'); + expect(wma.getResultOrThrow().valueOf()).toBe('85'); expect(wma.lowest?.toFixed(2)).toBe('70.00'); expect(wma.highest?.toFixed(2)).toBe('100.00'); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the moving average based on the last 5 prices', () => { const prices = [91, 90, 89, 88, 90]; const expectations = ['89.33']; @@ -149,7 +149,7 @@ describe('WMA', () => { const wma = new WMA(26); try { - wma.getResult(); + wma.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -158,7 +158,7 @@ describe('WMA', () => { const fasterWMA = new FasterWMA(5); try { - fasterWMA.getResult(); + fasterWMA.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); diff --git a/src/WSMA/WSMA.test.ts b/src/WSMA/WSMA.test.ts index 98c5c763..d4c4ba0b 100644 --- a/src/WSMA/WSMA.test.ts +++ b/src/WSMA/WSMA.test.ts @@ -17,8 +17,8 @@ describe('WSMA', () => { wsmaWithReplace.replace(14); wsmaWithReplace.add(15); - const actual = wsmaWithReplace.getResult().toFixed(); - const expected = wsma.getResult().toFixed(); + const actual = wsmaWithReplace.getResultOrThrow().toFixed(); + const expected = wsma.getResultOrThrow().toFixed(); expect(actual).toBe(expected); }); @@ -45,12 +45,12 @@ describe('WSMA', () => { const latestHigh = '13.44'; wsma.add(latestValue); - expect(wsma.getResult().toFixed(2)).toBe(latestResult); + expect(wsma.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(wsma.lowest?.toFixed(2)).toBe(latestLow); expect(wsma.highest?.toFixed(2)).toBe(latestHigh); fasterWSMA.add(latestValue); - expect(fasterWSMA.getResult().toFixed(2)).toBe(latestResult); + expect(fasterWSMA.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(fasterWSMA.lowest?.toFixed(2)).toBe(latestLow); expect(fasterWSMA.highest?.toFixed(2)).toBe(latestHigh); @@ -61,29 +61,29 @@ describe('WSMA', () => { const otherHigh = '341.78'; wsma.replace(someOtherValue); - expect(wsma.getResult().toFixed(2)).toBe(otherResult); + expect(wsma.getResultOrThrow().toFixed(2)).toBe(otherResult); expect(wsma.lowest?.toFixed(2)).toBe(otherLow); expect(wsma.highest?.toFixed(2), 'new record high').toBe(otherHigh); fasterWSMA.replace(someOtherValue); - expect(fasterWSMA.getResult().toFixed(2)).toBe(otherResult); + expect(fasterWSMA.getResultOrThrow().toFixed(2)).toBe(otherResult); expect(fasterWSMA.lowest?.toFixed(2)).toBe(otherLow); expect(fasterWSMA.highest?.toFixed(2), 'new record high').toBe(otherHigh); // Replace the other value with the latest value wsma.replace(latestValue); - expect(wsma.getResult().toFixed(2)).toBe(latestResult); + expect(wsma.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(wsma.lowest?.toFixed(2), 'lowest reset').toBe(latestLow); expect(wsma.highest?.toFixed(2), 'highest reset').toBe(latestHigh); fasterWSMA.replace(latestValue); - expect(fasterWSMA.getResult().toFixed(2)).toBe(latestResult); + expect(fasterWSMA.getResultOrThrow().toFixed(2)).toBe(latestResult); expect(fasterWSMA.lowest?.toFixed(2), 'lowest reset').toBe(latestLow); expect(fasterWSMA.highest?.toFixed(2), 'highest reset').toBe(latestHigh); }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('calculates the WSMA based on a SMA', () => { // Test data verified with: // https://runkit.com/anandaravindan/wema @@ -95,11 +95,11 @@ describe('WSMA', () => { wsma.add(price); if (wsma.isStable) { const expected = expectations.shift(); - expect(wsma.getResult().toFixed(2)).toBe(expected); + expect(wsma.getResultOrThrow().toFixed(2)).toBe(expected); } } - expect(wsma.getResult().toFixed(2)).toBe('18.97'); + expect(wsma.getResultOrThrow().toFixed(2)).toBe('18.97'); }); it('is compatible with results from Tulip Indicators (TI)', () => { @@ -133,16 +133,16 @@ describe('WSMA', () => { fasterWSMA.add(price); if (wsma.isStable && fasterWSMA.isStable) { const expected = expectations[i]; - expect(wsma.getResult().toFixed(4)).toBe(expected); - expect(fasterWSMA.getResult().toFixed(4)).toBe(expected); + expect(wsma.getResultOrThrow().toFixed(4)).toBe(expected); + expect(fasterWSMA.getResultOrThrow().toFixed(4)).toBe(expected); } } expect(wsma.isStable).toBe(true); expect(fasterWSMA.isStable).toBe(true); - expect(wsma.getResult().toFixed(4)).toBe('62.8540'); - expect(fasterWSMA.getResult().toFixed(4)).toBe('62.8540'); + expect(wsma.getResultOrThrow().toFixed(4)).toBe('62.8540'); + expect(fasterWSMA.getResultOrThrow().toFixed(4)).toBe('62.8540'); expect(wsma.highest?.toFixed(4)).toBe('63.2739'); expect(fasterWSMA.highest?.toFixed(4)).toBe('63.2739'); @@ -155,7 +155,7 @@ describe('WSMA', () => { const wsma = new WSMA(3); try { - wsma.getResult(); + wsma.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -168,7 +168,7 @@ describe('WSMA', () => { wsma.add(2); try { - wsma.getResult(); + wsma.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError); @@ -187,8 +187,8 @@ describe('WSMA', () => { wsma.updates(prices, false); fasterWSMA.updates(prices, false); - expect(wsma.getResult().toFixed(2)).toBe('18.97'); - expect(fasterWSMA.getResult().toFixed(2)).toBe('18.97'); + expect(wsma.getResultOrThrow().toFixed(2)).toBe('18.97'); + expect(fasterWSMA.getResultOrThrow().toFixed(2)).toBe('18.97'); }); }); diff --git a/src/util/Period.test.ts b/src/util/Period.test.ts index 5bc3c881..66ff2351 100644 --- a/src/util/Period.test.ts +++ b/src/util/Period.test.ts @@ -25,13 +25,13 @@ describe('Period', () => { }); }); - describe('getResult', () => { + describe('getResultOrThrow', () => { it('returns the highest and lowest value of the current period', () => { const values = [72, 1337]; const interval = 2; const period = new Period(interval); period.updates(values, false); - const {highest, lowest} = period.getResult(); + const {highest, lowest} = period.getResultOrThrow(); expect(lowest.valueOf()).toBe('72'); expect(highest.valueOf()).toBe('1337'); @@ -40,7 +40,7 @@ describe('Period', () => { const fasterPeriod = new FasterPeriod(interval); fasterPeriod.updates(values, false); - const {highest: fastestHighest, lowest: fastestLowest} = fasterPeriod.getResult(); + const {highest: fastestHighest, lowest: fastestLowest} = fasterPeriod.getResultOrThrow(); expect(fastestLowest).toBe(72); expect(fastestHighest).toBe(1337); }); @@ -48,7 +48,7 @@ describe('Period', () => { it('throws an error when there is not enough input data', () => { const period = new Period(2); try { - period.getResult(); + period.getResultOrThrow(); throw new Error('Expected error'); } catch (error) { expect(error).toBeInstanceOf(NotEnoughDataError);