From 3ba0507a795fb3521a03f16f4cb13f376b75d542 Mon Sep 17 00:00:00 2001 From: Jan Korf Date: Mon, 21 Oct 2019 21:44:51 +0200 Subject: [PATCH] Cleaned up order book --- CryptoExchange.Net/CryptoExchange.Net.xml | 66 +++++-------------- CryptoExchange.Net/ExtensionMethods.cs | 11 ++++ .../Interfaces/ISymbolOrderBook.cs | 2 +- .../OrderBook/OrderBookEntry.cs | 30 --------- .../OrderBook/ProcessBufferEntry.cs | 15 ++--- CryptoExchange.Net/OrderBook/ProcessEntry.cs | 31 --------- .../OrderBook/SymbolOrderBook.cs | 37 ++++++----- 7 files changed, 55 insertions(+), 137 deletions(-) delete mode 100644 CryptoExchange.Net/OrderBook/OrderBookEntry.cs delete mode 100644 CryptoExchange.Net/OrderBook/ProcessEntry.cs diff --git a/CryptoExchange.Net/CryptoExchange.Net.xml b/CryptoExchange.Net/CryptoExchange.Net.xml index e8cdf81..ca429f4 100644 --- a/CryptoExchange.Net/CryptoExchange.Net.xml +++ b/CryptoExchange.Net/CryptoExchange.Net.xml @@ -538,6 +538,13 @@ The value of the object Name of the parameter + + + Validates a list is not null or empty + + The value of the object + Name of the parameter + Rate limiter interface @@ -1637,28 +1644,6 @@ - - - Order book entry - - - - - Quantity of the entry - - - - - Price of the entry - - - - - ctor - - - - Buffer entry for order book @@ -1674,38 +1659,16 @@ The last sequence number of the entries - + - List of entries + List of asks - + - ctor + List of bids - - - Process entry for order book - - - - - The entry - - - - - The type - - - - - ctor - - - - Base for order book implementations @@ -1758,7 +1721,7 @@ - Event when order book was updated. Be careful! It can generate a lot of events at high-liquidity markets + Event when order book was updated, containing the changed bids and asks. Be careful! It can generate a lot of events at high-liquidity markets @@ -1852,13 +1815,14 @@ List of asks List of bids - + Update the order book with entries First sequence number Last sequence number - List of entries + List of bids + List of asks diff --git a/CryptoExchange.Net/ExtensionMethods.cs b/CryptoExchange.Net/ExtensionMethods.cs index 9fd7e1c..1090cf9 100644 --- a/CryptoExchange.Net/ExtensionMethods.cs +++ b/CryptoExchange.Net/ExtensionMethods.cs @@ -262,6 +262,17 @@ namespace CryptoExchange.Net if (value == null) throw new ArgumentException($"No value provided for parameter {argumentName}"); } + + /// + /// Validates a list is not null or empty + /// + /// The value of the object + /// Name of the parameter + public static void ValidateNotNull(this IEnumerable value, string argumentName) + { + if (value == null || !value.Any()) + throw new ArgumentException($"No values provided for parameter {argumentName}"); + } } } diff --git a/CryptoExchange.Net/Interfaces/ISymbolOrderBook.cs b/CryptoExchange.Net/Interfaces/ISymbolOrderBook.cs index ad09904..1571feb 100644 --- a/CryptoExchange.Net/Interfaces/ISymbolOrderBook.cs +++ b/CryptoExchange.Net/Interfaces/ISymbolOrderBook.cs @@ -31,7 +31,7 @@ namespace CryptoExchange.Net.Interfaces /// /// Event when order book was updated. Be careful! It can generate a lot of events at high-liquidity markets /// - event Action OnOrderBookUpdate; + event Action, IEnumerable> OnOrderBookUpdate; /// /// Timestamp of the last update /// diff --git a/CryptoExchange.Net/OrderBook/OrderBookEntry.cs b/CryptoExchange.Net/OrderBook/OrderBookEntry.cs deleted file mode 100644 index 4bf9c39..0000000 --- a/CryptoExchange.Net/OrderBook/OrderBookEntry.cs +++ /dev/null @@ -1,30 +0,0 @@ -using CryptoExchange.Net.Interfaces; - -namespace CryptoExchange.Net.OrderBook -{ - /// - /// Order book entry - /// - public class OrderBookEntry : ISymbolOrderBookEntry - { - /// - /// Quantity of the entry - /// - public decimal Quantity { get; set; } - /// - /// Price of the entry - /// - public decimal Price { get; set; } - - /// - /// ctor - /// - /// - /// - public OrderBookEntry(decimal price, decimal quantity) - { - Quantity = quantity; - Price = price; - } - } -} diff --git a/CryptoExchange.Net/OrderBook/ProcessBufferEntry.cs b/CryptoExchange.Net/OrderBook/ProcessBufferEntry.cs index 8a48794..397f53a 100644 --- a/CryptoExchange.Net/OrderBook/ProcessBufferEntry.cs +++ b/CryptoExchange.Net/OrderBook/ProcessBufferEntry.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using CryptoExchange.Net.Interfaces; +using System.Collections.Generic; namespace CryptoExchange.Net.OrderBook { @@ -16,16 +17,12 @@ namespace CryptoExchange.Net.OrderBook /// public long LastSequence { get; set; } /// - /// List of entries + /// List of asks /// - public List Entries { get; set; } - + public IEnumerable Asks { get; set; } = new List(); /// - /// ctor + /// List of bids /// - public ProcessBufferEntry() - { - Entries = new List(); - } + public IEnumerable Bids { get; set; } = new List(); } } diff --git a/CryptoExchange.Net/OrderBook/ProcessEntry.cs b/CryptoExchange.Net/OrderBook/ProcessEntry.cs deleted file mode 100644 index 5ebff10..0000000 --- a/CryptoExchange.Net/OrderBook/ProcessEntry.cs +++ /dev/null @@ -1,31 +0,0 @@ -using CryptoExchange.Net.Interfaces; -using CryptoExchange.Net.Objects; - -namespace CryptoExchange.Net.OrderBook -{ - /// - /// Process entry for order book - /// - public class ProcessEntry - { - /// - /// The entry - /// - public ISymbolOrderBookEntry Entry { get; set; } - /// - /// The type - /// - public OrderBookEntryType Type { get; set; } - - /// - /// ctor - /// - /// - /// - public ProcessEntry(OrderBookEntryType type, ISymbolOrderBookEntry entry) - { - Type = type; - Entry = entry; - } - } -} diff --git a/CryptoExchange.Net/OrderBook/SymbolOrderBook.cs b/CryptoExchange.Net/OrderBook/SymbolOrderBook.cs index 675e8e8..4597e7d 100644 --- a/CryptoExchange.Net/OrderBook/SymbolOrderBook.cs +++ b/CryptoExchange.Net/OrderBook/SymbolOrderBook.cs @@ -77,9 +77,9 @@ namespace CryptoExchange.Net.OrderBook /// public event Action? OnStatusChange; /// - /// Event when order book was updated. Be careful! It can generate a lot of events at high-liquidity markets + /// Event when order book was updated, containing the changed bids and asks. Be careful! It can generate a lot of events at high-liquidity markets /// - public event Action? OnOrderBookUpdate; + public event Action, IEnumerable>? OnOrderBookUpdate; /// /// Timestamp of the last update /// @@ -268,10 +268,10 @@ namespace CryptoExchange.Net.OrderBook asks.Clear(); foreach (var ask in askList) - asks.Add(ask.Price, new OrderBookEntry(ask.Price, ask.Quantity)); + asks.Add(ask.Price, ask); bids.Clear(); foreach (var bid in bidList) - bids.Add(bid.Price, new OrderBookEntry(bid.Price, bid.Quantity)); + bids.Add(bid.Price, bid); LastSequenceNumber = orderBookSequenceNumber; @@ -281,7 +281,7 @@ namespace CryptoExchange.Net.OrderBook CheckProcessBuffer(); bookSet = true; LastOrderBookUpdate = DateTime.UtcNow; - OnOrderBookUpdate?.Invoke(); + OnOrderBookUpdate?.Invoke(bidList, askList); log.Write(LogVerbosity.Debug, $"{Id} order book {Symbol} data set: {BidCount} bids, {AskCount} asks"); } } @@ -291,8 +291,9 @@ namespace CryptoExchange.Net.OrderBook /// /// First sequence number /// Last sequence number - /// List of entries - protected void UpdateOrderBook(long firstSequenceNumber, long lastSequenceNumber, List entries) + /// List of bids + /// List of asks + protected void UpdateOrderBook(long firstSequenceNumber, long lastSequenceNumber, IEnumerable bids, IEnumerable asks) { lock (bookLock) { @@ -305,7 +306,8 @@ namespace CryptoExchange.Net.OrderBook { FirstSequence = firstSequenceNumber, LastSequence = lastSequenceNumber, - Entries = entries + Asks = asks, + Bids = bids }; processBuffer.Add(entry); log.Write(LogVerbosity.Debug, $"{Id} order book {Symbol} update before synced; buffering"); @@ -318,13 +320,15 @@ namespace CryptoExchange.Net.OrderBook } else { - foreach (var entry in entries) - ProcessUpdate(entry.Type, entry.Entry); + foreach (var entry in asks) + ProcessUpdate(OrderBookEntryType.Ask, entry); + foreach (var entry in bids) + ProcessUpdate(OrderBookEntryType.Bid, entry); LastSequenceNumber = lastSequenceNumber; CheckProcessBuffer(); LastOrderBookUpdate = DateTime.UtcNow; - OnOrderBookUpdate?.Invoke(); - log.Write(LogVerbosity.Debug, $"{Id} order book {Symbol} update: {entries.Count} entries processed"); + OnOrderBookUpdate?.Invoke(bids, asks); + log.Write(LogVerbosity.Debug, $"{Id} order book {Symbol} update: {asks.Count()} asks, {bids.Count()} bids processed"); } } } @@ -345,8 +349,11 @@ namespace CryptoExchange.Net.OrderBook if (bufferEntry.FirstSequence > LastSequenceNumber + 1) break; - foreach (var entry in bufferEntry.Entries) - ProcessUpdate(entry.Type, entry.Entry); + foreach (var entry in bufferEntry.Asks) + ProcessUpdate(OrderBookEntryType.Ask, entry); + foreach (var entry in bufferEntry.Bids) + ProcessUpdate(OrderBookEntryType.Bid, entry); + processBuffer.Remove(bufferEntry); LastSequenceNumber = bufferEntry.LastSequence; } @@ -373,7 +380,7 @@ namespace CryptoExchange.Net.OrderBook { if (!listToChange.ContainsKey(entry.Price)) { - listToChange.Add(entry.Price, new OrderBookEntry(entry.Price, entry.Quantity)); + listToChange.Add(entry.Price, entry); if (type == OrderBookEntryType.Ask) AskCount++; else BidCount++; }