1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-16 19:03:01 +00:00

Added strict levels to symbol order book

This commit is contained in:
JKorf
2020-06-16 16:18:57 +02:00
parent c60131d464
commit 20bf8df4ef
3 changed files with 59 additions and 148 deletions
@@ -35,6 +35,7 @@ namespace CryptoExchange.Net.OrderBook
private OrderBookStatus status;
private UpdateSubscription? subscription;
private readonly bool sequencesAreConsecutive;
private readonly bool strictLevels;
private Task _processTask;
private AutoResetEvent _queueEvent;
@@ -54,6 +55,11 @@ namespace CryptoExchange.Net.OrderBook
/// </summary>
protected bool bookSet;
/// <summary>
/// The amount of levels for this book
/// </summary>
protected int? Levels { get; set; } = null;
/// <summary>
/// The status of the order book. Order book is up to date when the status is `Synced`
/// </summary>
@@ -194,6 +200,7 @@ namespace CryptoExchange.Net.OrderBook
_queueEvent = new AutoResetEvent(false);
sequencesAreConsecutive = options.SequenceNumbersAreConsecutive;
strictLevels = options.StrictLevels;
Symbol = symbol;
Status = OrderBookStatus.Disconnected;
@@ -236,6 +243,8 @@ namespace CryptoExchange.Net.OrderBook
log.Write(LogVerbosity.Warning, $"{Id} order book {Symbol} connection lost");
Status = OrderBookStatus.Connecting;
_queueEvent.Set();
// Clear queue
while(_processQueue.TryDequeue(out _))
processBuffer.Clear();
bookSet = false;
DoReset();
@@ -328,6 +337,14 @@ namespace CryptoExchange.Net.OrderBook
CheckProcessBuffer();
var (prevBestBid, prevBestAsk) = BestOffers;
ProcessRangeUpdates(item.StartUpdateId, item.EndUpdateId, item.Bids, item.Asks);
if (asks.First().Key < bids.First().Key)
{
log.Write(LogVerbosity.Warning, $"{Id} order book {Symbol} detected out of sync order book. Resyncing");
_ = subscription?.Reconnect();
return;
}
OnOrderBookUpdate?.Invoke(item.Bids, item.Asks);
CheckBestOffersChanged(prevBestBid, prevBestAsk);
}
@@ -429,6 +446,21 @@ namespace CryptoExchange.Net.OrderBook
foreach (var entry in asks)
ProcessUpdate(LastSequenceNumber + 1, OrderBookEntryType.Ask, entry);
if (Levels.HasValue && strictLevels)
{
while (this.bids.Count() > Levels.Value)
{
BidCount--;
this.bids.Remove(this.bids.Last().Key);
}
while (this.asks.Count() > Levels.Value)
{
AskCount--;
this.asks.Remove(this.asks.Last().Key);
}
}
LastSequenceNumber = lastUpdateId;
log.Write(LogVerbosity.Debug, $"{Id} order book {Symbol} update processed #{firstUpdateId}-{lastUpdateId}");
}