Skip to content

Commit e96811c

Browse files
authored
refactor ExchangeOrderBook.MergeOrderBookDelta() (#709)
1 parent 76e30d6 commit e96811c

File tree

2 files changed

+4
-20
lines changed

2 files changed

+4
-20
lines changed

src/ExchangeSharp/API/Exchanges/_Base/ExchangeAPIExtensions.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,14 @@ public static async Task<IWebSocket> GetFullOrderBookWebSocketAsync(this IOrderB
5252
ConcurrentDictionary<string, ExchangeOrderBook> fullBooks = new ConcurrentDictionary<string, ExchangeOrderBook>();
5353
Dictionary<string, Queue<ExchangeOrderBook>> partialOrderBookQueues = new Dictionary<string, Queue<ExchangeOrderBook>>();
5454

55-
static void applyDelta(SortedDictionary<decimal, ExchangeOrderPrice> deltaValues, SortedDictionary<decimal, ExchangeOrderPrice> bookToEdit)
56-
{
57-
foreach (ExchangeOrderPrice record in deltaValues.Values)
58-
{
59-
if (record.Amount <= 0 || record.Price <= 0)
60-
{
61-
bookToEdit.Remove(record.Price);
62-
}
63-
else
64-
{
65-
bookToEdit[record.Price] = record;
66-
}
67-
}
68-
}
69-
7055
static void updateOrderBook(ExchangeOrderBook fullOrderBook, ExchangeOrderBook freshBook)
7156
{
7257
lock (fullOrderBook)
7358
{
7459
// update deltas as long as the full book is at or before the delta timestamp
7560
if (fullOrderBook.SequenceId <= freshBook.SequenceId)
7661
{
77-
applyDelta(freshBook.Asks, fullOrderBook.Asks);
78-
applyDelta(freshBook.Bids, fullOrderBook.Bids);
79-
fullOrderBook.SequenceId = freshBook.SequenceId;
62+
fullOrderBook.ApplyUpdates(freshBook);
8063
}
8164
}
8265
}

src/ExchangeSharp/Model/ExchangeOrderBook.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,15 @@ public void ApplyUpdates(ExchangeOrderBook partialUpdate)
205205
{
206206
MergeOrderBookDelta(partialUpdate.Asks, this.Asks);
207207
MergeOrderBookDelta(partialUpdate.Bids, this.Bids);
208+
SequenceId = partialUpdate.SequenceId;
208209

209-
static void MergeOrderBookDelta(
210+
static void MergeOrderBookDelta(
210211
SortedDictionary<decimal, ExchangeOrderPrice> newData,
211212
SortedDictionary<decimal, ExchangeOrderPrice> bookData)
212213
{
213214
newData.ToList().ForEach(x =>
214215
{
215-
if (x.Value.Amount == 0m)
216+
if (x.Value.Amount <= 0m || x.Value.Price <= 0m)
216217
bookData.Remove(x.Key);
217218
else
218219
bookData[x.Key] = x.Value;

0 commit comments

Comments
 (0)