Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed Dec 12, 2024
1 parent 3821f08 commit a6884aa
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 68 deletions.
24 changes: 14 additions & 10 deletions src/ABANDS/AccelerationBands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,26 @@ 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,
},
]);
});
});
});

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,
},
]);
});
});
7 changes: 2 additions & 5 deletions src/BBANDS/BollingerBands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down
14 changes: 2 additions & 12 deletions src/DMA/DMA.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions src/DX/DX.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/EMA/EMA.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
3 changes: 1 addition & 2 deletions src/Indicator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
Expand Down
28 changes: 14 additions & 14 deletions src/MACD/MACD.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
});

Expand All @@ -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);
Expand Down Expand Up @@ -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);
});
Expand Down
30 changes: 17 additions & 13 deletions src/STOCH/StochasticOscillator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 4 additions & 2 deletions src/WSMA/WSMA.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 14 additions & 4 deletions src/util/Period.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down

0 comments on commit a6884aa

Please sign in to comment.