diff --git a/src/mappers/bitmex.ts b/src/mappers/bitmex.ts index 4d508c2..feca5f7 100644 --- a/src/mappers/bitmex.ts +++ b/src/mappers/bitmex.ts @@ -100,8 +100,20 @@ export class BitmexBookChangeMapper implements Mapper<'bitmex', BookChange> { for (let symbol in bitmexBookMessagesGrouppedBySymbol) { const bids: BookPriceLevel[] = [] const asks: BookPriceLevel[] = [] + let latestBitmexTimestamp: Date | undefined = undefined for (const item of bitmexBookMessagesGrouppedBySymbol[symbol]) { + if (item.timestamp !== undefined) { + const priceLevelTimestamp = new Date(item.timestamp) + if (latestBitmexTimestamp === undefined) { + latestBitmexTimestamp = priceLevelTimestamp + } else { + if (priceLevelTimestamp.valueOf() > latestBitmexTimestamp.valueOf()) { + latestBitmexTimestamp = priceLevelTimestamp + } + } + } + // https://www.bitmex.com/app/restAPI#OrderBookL2 if (item.price !== undefined) { // store the mapping from id to price level if price is specified @@ -136,7 +148,7 @@ export class BitmexBookChangeMapper implements Mapper<'bitmex', BookChange> { isSnapshot, bids, asks, - timestamp: localTimestamp, + timestamp: latestBitmexTimestamp !== undefined ? latestBitmexTimestamp : localTimestamp, localTimestamp: localTimestamp } @@ -314,6 +326,7 @@ type BitmexOrderBookL2Message = BitmexDataMessage & { side: 'Buy' | 'Sell' size?: number price?: number + timestamp?: string }[] } diff --git a/test/__snapshots__/mappers.test.ts.snap b/test/__snapshots__/mappers.test.ts.snap index ebb12b7..367583e 100644 --- a/test/__snapshots__/mappers.test.ts.snap +++ b/test/__snapshots__/mappers.test.ts.snap @@ -1772,6 +1772,79 @@ Array [ ] `; +exports[`mappers map bitmex messages 16`] = ` +Array [ + Object { + "asks": Array [ + Object { + "amount": 1000000, + "price": 42362, + }, + Object { + "amount": 10000, + "price": 662520, + }, + Object { + "amount": 20000, + "price": 658900, + }, + ], + "bids": Array [], + "exchange": "bitmex", + "isSnapshot": true, + "localTimestamp": 2019-06-01T00:00:28.619Z, + "symbol": "XBTUSD", + "timestamp": 2022-02-14T07:59:54.829Z, + "type": "book_change", + }, +] +`; + +exports[`mappers map bitmex messages 17`] = ` +Array [ + Object { + "asks": Array [], + "bids": Array [ + Object { + "amount": 20000, + "price": 42362, + }, + ], + "exchange": "bitmex", + "isSnapshot": false, + "localTimestamp": 2019-06-01T00:00:28.619Z, + "symbol": "XBTUSD", + "timestamp": 2022-02-14T08:06:54.742Z, + "type": "book_change", + }, +] +`; + +exports[`mappers map bitmex messages 18`] = ` +Array [ + Object { + "asks": Array [ + Object { + "amount": 0, + "price": 662520, + }, + ], + "bids": Array [ + Object { + "amount": 0, + "price": 658900, + }, + ], + "exchange": "bitmex", + "isSnapshot": false, + "localTimestamp": 2019-06-01T00:00:28.619Z, + "symbol": "XBTUSD", + "timestamp": 2022-02-14T08:34:56.913Z, + "type": "book_change", + }, +] +`; + exports[`mappers map bitstamp messages 1`] = `Array []`; exports[`mappers map bitstamp messages 2`] = ` diff --git a/test/mappers.test.ts b/test/mappers.test.ts index abb6e34..628b2d4 100644 --- a/test/mappers.test.ts +++ b/test/mappers.test.ts @@ -749,6 +749,35 @@ describe('mappers', () => { attributes: { symbol: 'parted', id: 'sorted' }, filter: { symbol: 'XBTZ21' }, data: [] + }, + + { + table: 'orderBookL2', + action: 'partial', + keys: ['symbol', 'id', 'side'], + types: { symbol: 'symbol', id: 'long', side: 'symbol', size: 'long', price: 'float', timestamp: 'timestamp' }, + foreignKeys: { symbol: 'instrument', side: 'side' }, + attributes: { symbol: 'grouped' }, + filter: { symbol: 'XBTUSD' }, + data: [ + { symbol: 'XBTUSD', id: 8791115350, side: 'Sell', size: 1000000, price: 42362, timestamp: '2022-02-14T07:59:54.829Z' }, + { symbol: 'XBTUSD', id: 8733748000, side: 'Sell', size: 10000, price: 662520, timestamp: '2022-02-14T07:59:54.829Z' }, + { symbol: 'XBTUSD', id: 8734110000, side: 'Sell', size: 20000, price: 658900, timestamp: '2022-02-14T07:59:54.829Z' } + ] + }, + { + table: 'orderBookL2', + action: 'insert', + data: [{ symbol: 'XBTUSD', id: 8791115350, side: 'Buy', size: 20000, price: 42362, timestamp: '2022-02-14T08:06:54.742Z' }] + }, + + { + table: 'orderBookL2', + action: 'delete', + data: [ + { symbol: 'XBTUSD', id: 8733748000, side: 'Sell', timestamp: '2022-02-14T08:34:56.852Z' }, + { symbol: 'XBTUSD', id: 8734110000, side: 'Buy', timestamp: '2022-02-14T08:34:56.913Z' } + ] } ]