diff --git a/src/ABANDS/AccelerationBands.test.ts b/src/ABANDS/AccelerationBands.test.ts index fbac9157..82dbf455 100644 --- a/src/ABANDS/AccelerationBands.test.ts +++ b/src/ABANDS/AccelerationBands.test.ts @@ -108,11 +108,13 @@ describe('AccelerationBands', () => { describe('update', () => { it("doesn't crash when supplying zeroes", () => { const accBands = new AccelerationBands(20, 2); - return accBands.update({ - close: 0, - high: 0, - low: 0, - }); + return accBands.updates([ + { + close: 0, + high: 0, + low: 0, + }, + ]); }); }); }); @@ -120,10 +122,12 @@ describe('AccelerationBands', () => { describe('FaserAccelerationBands', () => { it("doesn't crash when supplying zeroes", () => { const accBands = new FasterAccelerationBands(20, 2); - return accBands.update({ - close: 0, - high: 0, - low: 0, - }); + return accBands.updates([ + { + close: 0, + high: 0, + low: 0, + }, + ]); }); }); diff --git a/src/BBANDS/BollingerBands.test.ts b/src/BBANDS/BollingerBands.test.ts index 3136fb14..c76ff025 100644 --- a/src/BBANDS/BollingerBands.test.ts +++ b/src/BBANDS/BollingerBands.test.ts @@ -7,8 +7,7 @@ describe('BollingerBands', () => { describe('prices', () => { it('does not cache more prices than necessary to fill the interval', () => { const bb = new BollingerBands(3); - bb.update(1); - bb.update(2); + bb.updates([1, 2]); expect(bb.prices.length).toBe(2); bb.update(3); expect(bb.prices.length).toBe(3); @@ -150,9 +149,7 @@ describe('FasterBollingerBands', () => { 81.59, 81.06, 82.87, 83.0, 83.61, 83.15, 82.84, 83.99, 84.55, 84.36, 85.53, 86.54, 86.89, 87.77, 87.29, ]; const fasterBB = new FasterBollingerBands(5, 2); - for (const price of prices) { - fasterBB.update(price); - } + fasterBB.updates(prices); expect(fasterBB.isStable).toBe(true); const actual = fasterBB.getResult(); expect(actual.lower.toFixed(2)).toBe('85.29'); diff --git a/src/DMA/DMA.test.ts b/src/DMA/DMA.test.ts index 49087531..c1e458f9 100644 --- a/src/DMA/DMA.test.ts +++ b/src/DMA/DMA.test.ts @@ -7,24 +7,14 @@ describe('DMA', () => { it('can replace recently added values', () => { const dma = new DMA(3, 6, SMA); const fasterDMA = new FasterDMA(3, 6, FasterSMA); - dma.update(41); - dma.update(37); - dma.update(20.9); - dma.update(100); - dma.update(30.71); - dma.update(40); + dma.updates([41, 37, 20.9, 100, 30.71, 40]); 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'); - fasterDMA.update(41); - fasterDMA.update(37); - fasterDMA.update(20.9); - fasterDMA.update(100); - fasterDMA.update(30.71); - fasterDMA.update(40); + fasterDMA.updates([41, 37, 20.9, 100, 30.71, 40]); fasterDMA.update(30, true); expect(fasterDMA.isStable).toBe(true); diff --git a/src/DX/DX.test.ts b/src/DX/DX.test.ts index 54628d9e..9d8b5ba9 100644 --- a/src/DX/DX.test.ts +++ b/src/DX/DX.test.ts @@ -75,10 +75,8 @@ describe('DX', () => { const dx = new DX(5); const fasterDX = new FasterDX(5); - for (const candle of candles) { - dx.update(candle); - fasterDX.update(candle); - } + dx.updates(candles); + fasterDX.updates(candles); expect(dx.isStable).toBe(true); expect(fasterDX.isStable).toBe(true); diff --git a/src/EMA/EMA.test.ts b/src/EMA/EMA.test.ts index 6ac3c3d8..20e78f5c 100644 --- a/src/EMA/EMA.test.ts +++ b/src/EMA/EMA.test.ts @@ -27,9 +27,11 @@ describe('EMA', () => { const ema = new EMA(interval); const emaWithReplace = new EMA(interval); - ema.updates([prices[0], prices[1], prices[2], prices[3], prices[4]]); + const subset = [prices[0], prices[1], prices[2]]; - emaWithReplace.updates([prices[0], prices[1], prices[2], '8239239']); + ema.updates([...subset, prices[3], prices[4]]); + + emaWithReplace.updates([...subset, '8239239']); emaWithReplace.replace(prices[3]); emaWithReplace.update(prices[4]); diff --git a/src/Indicator.test.ts b/src/Indicator.test.ts index e29b7325..7194ea9e 100644 --- a/src/Indicator.test.ts +++ b/src/Indicator.test.ts @@ -38,8 +38,7 @@ describe('Indicator', () => { it('returns the cross sum', () => { const itc = new IndicatorTestClass(); - itc.update(20); - itc.update(40); + itc.updates([20, 40]); expect(itc.getResult().toString()).toBe('30'); }); }); diff --git a/src/MACD/MACD.test.ts b/src/MACD/MACD.test.ts index cba2f6c5..8f52d1f2 100644 --- a/src/MACD/MACD.test.ts +++ b/src/MACD/MACD.test.ts @@ -17,15 +17,19 @@ describe('MACD', () => { signalInterval: 9, }); - macd.updates(['81.59', '81.06', '82.87', '83.0', '90', '83.61']); + const subset = ['10', '20', '80', '81.59', '81.06', '82.87', '83.0']; - macdWithReplace.updates(['81.59', '81.06', '82.87', '83.0', '100']); + macd.updates([...subset, '90', '83.61']); + + macdWithReplace.updates([...subset, '100']); macdWithReplace.replace(90); macdWithReplace.update('83.61'); - expect(macdWithReplace.getResult().histogram.toFixed()).toBe(macd.getResult().histogram.toFixed()); - expect(macdWithReplace.getResult().macd.toFixed()).toBe(macd.getResult().macd.toFixed()); - expect(macdWithReplace.getResult().signal.toFixed()).toBe(macd.getResult().signal.toFixed()); + 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()); }); }); @@ -39,14 +43,10 @@ describe('MACD', () => { }); const fasterMACD = new FasterMACD(new FasterEMA(2), new FasterEMA(5), new FasterEMA(9)); - macd.update('81.59'); - fasterMACD.update(81.59); - macd.update('81.06'); - fasterMACD.update(81.06); - macd.update('82.87'); - fasterMACD.update(82.87); - macd.update('83.0'); - fasterMACD.update(83.0); + const subset = [81.59, 81.06, 82.87, 83.0]; + macd.updates(subset); + fasterMACD.updates(subset); + macd.update('90'); // this value gets replaced with the next call fasterMACD.update(90); // this value gets replaced with the next call macd.update('83.61', true); @@ -220,7 +220,7 @@ describe('MACD', () => { expect(mockedPrices.length).toBe(longInterval); expect(macd.isStable).toBe(false); - mockedPrices.forEach(price => macd.update(price)); + macd.updates(mockedPrices); expect(macd.isStable).toBe(true); }); diff --git a/src/STOCH/StochasticOscillator.test.ts b/src/STOCH/StochasticOscillator.test.ts index f0151be0..94cde3a2 100644 --- a/src/STOCH/StochasticOscillator.test.ts +++ b/src/STOCH/StochasticOscillator.test.ts @@ -84,21 +84,25 @@ describe('StochasticOscillator', () => { it('prevents division by zero errors when highest high and lowest low have the same value', () => { const stoch = new StochasticOscillator(5, 3, 3); - stoch.update({close: 100, high: 100, low: 100}); - stoch.update({close: 100, high: 100, low: 100}); - stoch.update({close: 100, high: 100, low: 100}); - stoch.update({close: 100, high: 100, low: 100}); - stoch.update({close: 100, high: 100, low: 100}); - stoch.update({close: 100, high: 100, low: 100}); - stoch.update({close: 100, high: 100, low: 100}); - stoch.update({close: 100, high: 100, low: 100}); - const result = stoch.update({close: 100, high: 100, low: 100})!; - expect(result.stochK.toFixed(2)).toBe('0.00'); - expect(result.stochD.toFixed(2)).toBe('0.00'); + stoch.updates([ + {close: 100, high: 100, low: 100}, + {close: 100, high: 100, low: 100}, + {close: 100, high: 100, low: 100}, + {close: 100, high: 100, low: 100}, + {close: 100, high: 100, low: 100}, + {close: 100, high: 100, low: 100}, + {close: 100, high: 100, low: 100}, + {close: 100, high: 100, low: 100}, + ]); + const result = stoch.update({close: 100, high: 100, low: 100}); + expect(result?.stochK.toFixed(2)).toBe('0.00'); + expect(result?.stochD.toFixed(2)).toBe('0.00'); const fasterStoch = new FasterStochasticOscillator(1, 2, 2); - fasterStoch.update({close: 100, high: 100, low: 100}); - fasterStoch.update({close: 100, high: 100, low: 100}); + fasterStoch.updates([ + {close: 100, high: 100, low: 100}, + {close: 100, high: 100, low: 100}, + ]); const {stochK, stochD} = fasterStoch.getResult(); expect(stochK.toFixed(2)).toBe('0.00'); expect(stochD.toFixed(2)).toBe('0.00'); diff --git a/src/WSMA/WSMA.test.ts b/src/WSMA/WSMA.test.ts index 4f8982b3..1cd6fc53 100644 --- a/src/WSMA/WSMA.test.ts +++ b/src/WSMA/WSMA.test.ts @@ -9,9 +9,11 @@ describe('WSMA', () => { const wsma = new WSMA(interval); const wsmaWithReplace = new WSMA(interval); - wsma.updates([11, 12, 13, 14, 15]); + const subset = [11, 12, 13]; - wsmaWithReplace.updates([11, 12, 13, 50]); + wsma.updates([...subset, 14, 15]); + + wsmaWithReplace.updates([...subset, 50]); wsmaWithReplace.replace(14); wsmaWithReplace.update(15); diff --git a/src/util/Period.test.ts b/src/util/Period.test.ts index 9ce3d921..b2727ff5 100644 --- a/src/util/Period.test.ts +++ b/src/util/Period.test.ts @@ -1,22 +1,32 @@ +import {NotEnoughDataError} from '../error/NotEnoughDataError.js'; import {FasterPeriod, Period} from './Period.js'; describe('Period', () => { describe('getResult', () => { it('returns the highest and lowest value of the current period', () => { + const values = [72, 1337]; const period = new Period(2); - period.update(72); - period.update(1337); + period.updates(values); const {highest, lowest} = period.getResult(); expect(lowest.valueOf()).toBe('72'); expect(highest.valueOf()).toBe('1337'); const fasterPeriod = new FasterPeriod(2); - fasterPeriod.update(72); - fasterPeriod.update(1337); + fasterPeriod.updates(values); const {highest: fastestHighest, lowest: fastestLowest} = fasterPeriod.getResult(); expect(fastestLowest).toBe(72); expect(fastestHighest).toBe(1337); }); + + it('throws an error when there is not enough input data', () => { + const period = new Period(2); + try { + period.getResult(); + throw new Error('Expected error'); + } catch (error) { + expect(error).toBeInstanceOf(NotEnoughDataError); + } + }); }); describe('isStable', () => {