1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-08 00:16:27 +00:00

added timeout for triggering order book updating event

This commit is contained in:
msg_kurt 2019-09-04 16:49:32 +03:00
parent c01bcc87b1
commit 43c17bae64
2 changed files with 43 additions and 18 deletions

View File

@ -26,8 +26,12 @@ namespace CryptoExchange.Net.Objects
/// <summary>
/// Base for order book options
/// </summary>
public class OrderBookOptions: BaseOptions
public class OrderBookOptions : BaseOptions
{
/// <summary>
/// Update event raising timeout in milliseconds (to limit it at high-liquidity order books)
/// </summary>
public int UpdateEventTimeout { get; }
/// <summary>
/// The name of the order book implementation
/// </summary>
@ -42,8 +46,10 @@ namespace CryptoExchange.Net.Objects
/// </summary>
/// <param name="name">The name of the order book implementation</param>
/// <param name="sequencesAreConsecutive">Whether each update should have a consecutive id number. Used to identify and reconnect when numbers are skipped.</param>
public OrderBookOptions(string name, bool sequencesAreConsecutive)
/// <param name="updateInterval">Update event raising timeout in milliseconds (to limit it at high-liquidity order books)</param>
public OrderBookOptions(string name, bool sequencesAreConsecutive, int? updateInterval)
{
UpdateEventTimeout = updateInterval ?? 1000;
OrderBookName = name;
SequenceNumbersAreConsecutive = sequencesAreConsecutive;
}
@ -52,7 +58,7 @@ namespace CryptoExchange.Net.Objects
/// <summary>
/// Base client options
/// </summary>
public class ClientOptions: BaseOptions
public class ClientOptions : BaseOptions
{
/// <summary>
@ -74,7 +80,7 @@ namespace CryptoExchange.Net.Objects
/// <summary>
/// Base for rest client options
/// </summary>
public class RestClientOptions: ClientOptions
public class RestClientOptions : ClientOptions
{
/// <summary>
/// List of rate limiters to use
@ -96,7 +102,7 @@ namespace CryptoExchange.Net.Objects
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T Copy<T>() where T:RestClientOptions, new()
public T Copy<T>() where T : RestClientOptions, new()
{
var copy = new T
{
@ -119,7 +125,7 @@ namespace CryptoExchange.Net.Objects
/// <summary>
/// Base for socket client options
/// </summary>
public class SocketClientOptions: ClientOptions
public class SocketClientOptions : ClientOptions
{
/// <summary>
/// Whether or not the socket should automatically reconnect when losing connection

View File

@ -13,7 +13,7 @@ namespace CryptoExchange.Net.OrderBook
/// <summary>
/// Base for order book implementations
/// </summary>
public abstract class SymbolOrderBook: IDisposable
public abstract class SymbolOrderBook : IDisposable
{
/// <summary>
/// The process buffer, used while syncing
@ -27,6 +27,7 @@ namespace CryptoExchange.Net.OrderBook
/// <summary>
/// The bid list
/// </summary>
protected SortedList<decimal, OrderBookEntry> bids;
private OrderBookStatus status;
private UpdateSubscription subscription;
@ -71,10 +72,17 @@ namespace CryptoExchange.Net.OrderBook
/// </summary>
public event Action<OrderBookStatus, OrderBookStatus> OnStatusChange;
/// <summary>
/// Event when orderbook was updated
/// Event when orderbook was updated, but not more often then timeout setted in orderbook options (1000ms by default). Be careful! with small timeout it can generate a lot of events at high-liquidity order books
/// </summary>
public event Action OnOrderBookUpdate;
/// <summary>
/// Should be useful for low-liquidity order-books to monitor market activity
/// </summary>
public DateTime LastOrderBookUpdate;
private DateTime LastOrderBookUpdateEventTrigger;
private readonly int updateEventInterval;
/// <summary>
/// The number of asks in the book
/// </summary>
public int AskCount { get; private set; }
@ -141,6 +149,7 @@ namespace CryptoExchange.Net.OrderBook
id = options.OrderBookName;
processBuffer = new List<ProcessBufferEntry>();
sequencesAreConsecutive = options.SequenceNumbersAreConsecutive;
updateEventInterval = options.UpdateEventTimeout;
Symbol = symbol;
Status = OrderBookStatus.Disconnected;
@ -166,7 +175,7 @@ namespace CryptoExchange.Net.OrderBook
{
Status = OrderBookStatus.Connecting;
var startResult = await DoStart().ConfigureAwait(false);
if(!startResult.Success)
if (!startResult.Success)
return new CallResult<bool>(false, startResult.Error);
subscription = startResult.Data;
@ -249,7 +258,7 @@ namespace CryptoExchange.Net.OrderBook
return;
asks.Clear();
foreach(var ask in askList)
foreach (var ask in askList)
asks.Add(ask.Price, new OrderBookEntry(ask.Price, ask.Quantity));
bids.Clear();
foreach (var bid in bidList)
@ -262,7 +271,12 @@ namespace CryptoExchange.Net.OrderBook
CheckProcessBuffer();
bookSet = true;
LastOrderBookUpdate = DateTime.UtcNow;
if ((LastOrderBookUpdate - LastOrderBookUpdateEventTrigger).TotalMilliseconds >= updateEventInterval)
{
OnOrderBookUpdate?.Invoke();
LastOrderBookUpdateEventTrigger = DateTime.UtcNow;
}
log.Write(LogVerbosity.Debug, $"{id} order book {Symbol} data set: {BidCount} bids, {AskCount} asks");
}
}
@ -299,11 +313,16 @@ namespace CryptoExchange.Net.OrderBook
}
else
{
foreach(var entry in entries)
foreach (var entry in entries)
ProcessUpdate(entry.Type, entry.Entry);
LastSequenceNumber = lastSequenceNumber;
CheckProcessBuffer();
LastOrderBookUpdate = DateTime.UtcNow;
if ((LastOrderBookUpdate - LastOrderBookUpdateEventTrigger).TotalMilliseconds >= updateEventInterval)
{
OnOrderBookUpdate?.Invoke();
LastOrderBookUpdateEventTrigger = DateTime.UtcNow;
}
log.Write(LogVerbosity.Debug, $"{id} order book {Symbol} update: {entries.Count} entries processed");
}
}
@ -316,7 +335,7 @@ namespace CryptoExchange.Net.OrderBook
{
foreach (var bufferEntry in processBuffer.OrderBy(b => b.FirstSequence).ToList())
{
if(bufferEntry.LastSequence < LastSequenceNumber)
if (bufferEntry.LastSequence < LastSequenceNumber)
{
processBuffer.Remove(bufferEntry);
continue;
@ -325,7 +344,7 @@ namespace CryptoExchange.Net.OrderBook
if (bufferEntry.FirstSequence > LastSequenceNumber + 1)
break;
foreach(var entry in bufferEntry.Entries)
foreach (var entry in bufferEntry.Entries)
ProcessUpdate(entry.Type, entry.Entry);
processBuffer.Remove(bufferEntry);
LastSequenceNumber = bufferEntry.LastSequence;