From 1e7bc7b0c09b881e1e270e6875f6485beadae45a Mon Sep 17 00:00:00 2001 From: Lucas Turpin Date: Thu, 5 Mar 2020 00:16:39 -0500 Subject: [PATCH] [#190] Fixed incorrect data in Top of Book graph - Removed assumption that orders are sorted by id within a price level - Added test case to document --- core/graphelier-service/db/mongo.go | 2 +- core/graphelier-service/models/orderbooks.go | 2 +- .../models/orderbooks_test.go | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/core/graphelier-service/db/mongo.go b/core/graphelier-service/db/mongo.go index 21852e68..deb961f1 100644 --- a/core/graphelier-service/db/mongo.go +++ b/core/graphelier-service/db/mongo.go @@ -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) } diff --git a/core/graphelier-service/models/orderbooks.go b/core/graphelier-service/models/orderbooks.go index a16d61d3..f1d65365 100644 --- a/core/graphelier-service/models/orderbooks.go +++ b/core/graphelier-service/models/orderbooks.go @@ -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 } } diff --git a/core/graphelier-service/models/orderbooks_test.go b/core/graphelier-service/models/orderbooks_test.go index f67c20a1..da30c53c 100644 --- a/core/graphelier-service/models/orderbooks_test.go +++ b/core/graphelier-service/models/orderbooks_test.go @@ -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) +}