Skip to content

Commit

Permalink
fix: handle bitmex empty snapshots when mapping data
Browse files Browse the repository at this point in the history
  • Loading branch information
thaaddeus committed Nov 12, 2021
1 parent 89a76f4 commit 1edee16
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/mappers/bitmex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ export class BitmexBookChangeMapper implements Mapper<'bitmex', BookChange> {
[key: string]: typeof bitmexOrderBookL2Message.data
}
)

if (bitmexOrderBookL2Message.data.length === 0 && bitmexOrderBookL2Message.filter?.symbol !== undefined) {
const emptySnapshot: BookChange = {
type: 'book_change',
symbol: bitmexOrderBookL2Message.filter?.symbol!,
exchange: 'bitmex',
isSnapshot: true,
bids: [],
asks: [],
timestamp: localTimestamp,
localTimestamp: localTimestamp
}

yield emptySnapshot
}
} else {
// in case of other messages types BitMEX always returns data for single symbol
bitmexBookMessagesGrouppedBySymbol = {
Expand Down Expand Up @@ -107,12 +122,14 @@ export class BitmexBookChangeMapper implements Mapper<'bitmex', BookChange> {
}
}

if (bids.length > 0 || asks.length > 0) {
const isSnapshot = bitmexOrderBookL2Message.action === 'partial'

if (bids.length > 0 || asks.length > 0 || isSnapshot) {
const bookChange: BookChange = {
type: 'book_change',
symbol,
exchange: 'bitmex',
isSnapshot: bitmexOrderBookL2Message.action === 'partial',
isSnapshot,
bids,
asks,
timestamp: localTimestamp,
Expand Down Expand Up @@ -280,6 +297,7 @@ type BitmexInstrumentsMessage = BitmexDataMessage & {

type BitmexOrderBookL2Message = BitmexDataMessage & {
table: 'orderBookL2'
filter?: { symbol?: string }
data: {
symbol: string
id: number
Expand Down
15 changes: 15 additions & 0 deletions test/__snapshots__/mappers.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,21 @@ Array [
]
`;

exports[`mappers map bitmex messages 15`] = `
Array [
Object {
"asks": Array [],
"bids": Array [],
"exchange": "bitmex",
"isSnapshot": true,
"localTimestamp": 2019-06-01T00:00:28.619Z,
"symbol": "XBTZ21",
"timestamp": 2019-06-01T00:00:28.619Z,
"type": "book_change",
},
]
`;

exports[`mappers map bitstamp messages 1`] = `Array []`;

exports[`mappers map bitstamp messages 2`] = `
Expand Down
11 changes: 11 additions & 0 deletions test/mappers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,10 +739,21 @@ describe('mappers', () => {
{ timestamp: '2021-10-13T07:07:00.106Z', symbol: 'XBTUSD', bidSize: 2700, bidPrice: 55411, askPrice: 55411.5, askSize: 205100 },
{ timestamp: '2021-10-13T07:07:01.010Z', symbol: 'XBTUSD', bidSize: 700, bidPrice: 55400, askPrice: 55400.5, askSize: 241500 }
]
},
{
table: 'orderBookL2',
action: 'partial',
keys: ['symbol', 'id', 'side'],
types: { symbol: 'symbol', id: 'long', side: 'symbol', size: 'long', price: 'float' },
foreignKeys: { symbol: 'instrument', side: 'side' },
attributes: { symbol: 'parted', id: 'sorted' },
filter: { symbol: 'XBTZ21' },
data: []
}
]

const bitmexMapper = createMapper('bitmex')

for (const message of messages) {
const mappedMessages = bitmexMapper.map(message, new Date('2019-06-01T00:00:28.6199940Z'))
expect(mappedMessages).toMatchSnapshot()
Expand Down

0 comments on commit 1edee16

Please sign in to comment.