[#151] fill gaps in orderbook data

This commit is contained in:
Bartłomiej Głownia 2022-04-27 14:59:01 +02:00
parent 279acca6dc
commit 90ea4e4ab3
2 changed files with 34 additions and 18 deletions

View File

@ -217,14 +217,14 @@ describe('updateCompactedData', () => {
resolution
);
expect(updatedData[0].ask).toEqual(20);
expect(updatedData[0].askByLevel?.[120]).toEqual(10);
expect(updatedData[0].askByLevel?.['120']).toEqual(10);
expect(updatedData[0].cumulativeVol.ask).toEqual(60);
expect(updatedData[2].bid).toEqual(20);
expect(updatedData[2].bidByLevel?.[80]).toEqual(10);
expect(updatedData[2].cumulativeVol.bid).toEqual(60);
expect(updatedData[4].bid).toEqual(20);
expect(updatedData[4].bidByLevel?.['80']).toEqual(10);
expect(updatedData[4].cumulativeVol.bid).toEqual(60);
});
it('remove row', () => {
it('update with zero value volume', () => {
const sell: MarketDepthSubscription_marketDepthUpdate_sell = {
__typename: 'PriceLevel',
price: '121',
@ -243,7 +243,7 @@ describe('updateCompactedData', () => {
[buy],
resolution
);
expect(updatedData.length).toEqual(1);
expect(updatedData.length).toEqual(5);
});
it('add new row at the end', () => {
@ -265,11 +265,11 @@ describe('updateCompactedData', () => {
[buy],
resolution
);
expect(updatedData.length).toEqual(5);
expect(updatedData.length).toEqual(8);
expect(updatedData[0].price).toEqual('130');
expect(updatedData[0].cumulativeVol.ask).toEqual(55);
expect(updatedData[4].price).toEqual('60');
expect(updatedData[4].cumulativeVol.bid).toEqual(55);
expect(updatedData[7].price).toEqual('60');
expect(updatedData[7].cumulativeVol.bid).toEqual(55);
});
it('add new row in the middle', () => {

View File

@ -95,6 +95,27 @@ const mapRawData =
): OrderbookData =>
createData(data.price, Number(data.volume), dataType);
const fillGaps = (orderbookData: OrderbookData[], resolution: number) => {
if (orderbookData.length < 2) {
return;
}
let index = 0;
while (index < orderbookData.length - 1) {
if (
BigInt(orderbookData[index].price) -
BigInt(orderbookData[index + 1].price) !==
BigInt(resolution)
) {
const data = createData(
(BigInt(orderbookData[index].price) - BigInt(resolution)).toString()
);
data.cumulativeVol = { ...orderbookData[index].cumulativeVol };
orderbookData.splice(index + 1, 0, data);
}
index += 1;
}
};
/**
* @summary merges sell amd buy data, orders by price desc, group by price level, counts cumulative and relative values
*/
@ -163,6 +184,8 @@ export const compactData = (
}
}
}
// fill gaps between price levels
fillGaps(orderbookData, resolution);
// count relative volumes
updateRelativeData(orderbookData);
return orderbookData;
@ -274,15 +297,8 @@ export const updateCompactedData = (
draft[i - 1].cumulativeVol.bid + draft[i].bid;
}
}
let index = 0;
// remove levels that do not have any volume
while (index < draft.length) {
if (!draft[index].ask && !draft[index].bid) {
draft.splice(index, 1);
} else {
index += 1;
}
}
// fill gaps between price levels
fillGaps(draft, resolution);
// count relative volumes
updateRelativeData(draft);
});