Skip to content

Commit

Permalink
refactor ExchangeOrderBook.MergeOrderBookDelta() (#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
vslee authored Jan 3, 2022
1 parent 76e30d6 commit e96811c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 20 deletions.
19 changes: 1 addition & 18 deletions src/ExchangeSharp/API/Exchanges/_Base/ExchangeAPIExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,14 @@ public static async Task<IWebSocket> GetFullOrderBookWebSocketAsync(this IOrderB
ConcurrentDictionary<string, ExchangeOrderBook> fullBooks = new ConcurrentDictionary<string, ExchangeOrderBook>();
Dictionary<string, Queue<ExchangeOrderBook>> partialOrderBookQueues = new Dictionary<string, Queue<ExchangeOrderBook>>();

static void applyDelta(SortedDictionary<decimal, ExchangeOrderPrice> deltaValues, SortedDictionary<decimal, ExchangeOrderPrice> bookToEdit)
{
foreach (ExchangeOrderPrice record in deltaValues.Values)
{
if (record.Amount <= 0 || record.Price <= 0)
{
bookToEdit.Remove(record.Price);
}
else
{
bookToEdit[record.Price] = record;
}
}
}

static void updateOrderBook(ExchangeOrderBook fullOrderBook, ExchangeOrderBook freshBook)
{
lock (fullOrderBook)
{
// update deltas as long as the full book is at or before the delta timestamp
if (fullOrderBook.SequenceId <= freshBook.SequenceId)
{
applyDelta(freshBook.Asks, fullOrderBook.Asks);
applyDelta(freshBook.Bids, fullOrderBook.Bids);
fullOrderBook.SequenceId = freshBook.SequenceId;
fullOrderBook.ApplyUpdates(freshBook);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/ExchangeSharp/Model/ExchangeOrderBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,15 @@ public void ApplyUpdates(ExchangeOrderBook partialUpdate)
{
MergeOrderBookDelta(partialUpdate.Asks, this.Asks);
MergeOrderBookDelta(partialUpdate.Bids, this.Bids);
SequenceId = partialUpdate.SequenceId;

static void MergeOrderBookDelta(
static void MergeOrderBookDelta(
SortedDictionary<decimal, ExchangeOrderPrice> newData,
SortedDictionary<decimal, ExchangeOrderPrice> bookData)
{
newData.ToList().ForEach(x =>
{
if (x.Value.Amount == 0m)
if (x.Value.Amount <= 0m || x.Value.Price <= 0m)
bookData.Remove(x.Key);
else
bookData[x.Key] = x.Value;
Expand Down

0 comments on commit e96811c

Please sign in to comment.