Skip to content

Commit

Permalink
feat: support bitmex orderBookL2 timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
thaaddeus committed Feb 14, 2022
1 parent be9b85b commit 5f736a4
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/mappers/bitmex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -136,7 +148,7 @@ export class BitmexBookChangeMapper implements Mapper<'bitmex', BookChange> {
isSnapshot,
bids,
asks,
timestamp: localTimestamp,
timestamp: latestBitmexTimestamp !== undefined ? latestBitmexTimestamp : localTimestamp,
localTimestamp: localTimestamp
}

Expand Down Expand Up @@ -314,6 +326,7 @@ type BitmexOrderBookL2Message = BitmexDataMessage & {
side: 'Buy' | 'Sell'
size?: number
price?: number
timestamp?: string
}[]
}

Expand Down
73 changes: 73 additions & 0 deletions test/__snapshots__/mappers.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `
Expand Down
29 changes: 29 additions & 0 deletions test/mappers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
]
}
]

Expand Down

0 comments on commit 5f736a4

Please sign in to comment.