Skip to content

Commit

Permalink
[#190] Fixed incorrect data in Top of Book graph
Browse files Browse the repository at this point in the history
- Removed assumption that orders are sorted by id within a price level
- Added test case to document
  • Loading branch information
Lercerss committed Mar 5, 2020
1 parent d654bb5 commit 1e7bc7b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/graphelier-service/db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func (c *Connector) GetTopOfBookByInterval(instrument string, startTimestamp uin
if pointDistance == 0 {
pointDistance = 1
}
log.Tracef("Applying messages to keep points at every %d", pointDistance)
log.Tracef("Applying messages to keep points at every %d\n", pointDistance)
results = orderbook.TopBookPerXNano(messagesInterval, uint64(pointDistance), startTimestamp, endTimestamp)
}

Expand Down
2 changes: 1 addition & 1 deletion core/graphelier-service/models/orderbooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (orderbook *Orderbook) getOrderIndex(index int, message *Message) (int, boo

for ; i < len(orders); i++ {
lastID = orders[i].ID
if lastID >= message.OrderID {
if lastID == message.OrderID {
break
}
}
Expand Down
18 changes: 18 additions & 0 deletions core/graphelier-service/models/orderbooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,21 @@ func TestMessagesZero(t *testing.T) {
assert.Equal(t, uint64(102), topbook[1].Timestamp)
assert.Equal(t, uint64(104), topbook[2].Timestamp)
}

func TestOrderIdsNotSorted(t *testing.T) {
// Orders were incorrectly assumed to be sorted by ID
// This stopped `Orderbook.getOrderIndex` from finding some orders
// As a result, some valid messages were being ignored which invalidated the order book state
setupExistingOrders()
orderbook.Asks[0].Orders[0].ID = 3
orderbook.Asks[0].Orders[1].ID = 2
orderbook.Asks[0].Orders[2].ID = 1

orderbook.ApplyMessagesToOrderbook([]*Message{
MakeMsg(DirectionAsk, OrderID(1), TypeDelete),
MakeMsg(DirectionAsk, OrderID(2), TypeDelete),
})

assert.Equal(t, 1, len(orderbook.Asks[0].Orders))
assert.Equal(t, uint64(3), orderbook.Asks[0].Orders[0].ID)
}

0 comments on commit 1e7bc7b

Please sign in to comment.