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:
parent
c01bcc87b1
commit
43c17bae64
@ -28,6 +28,10 @@ namespace CryptoExchange.Net.Objects
|
|||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// The name of the order book implementation
|
/// The name of the order book implementation
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -42,8 +46,10 @@ namespace CryptoExchange.Net.Objects
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="name">The name of the order book implementation</param>
|
/// <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>
|
/// <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;
|
OrderBookName = name;
|
||||||
SequenceNumbersAreConsecutive = sequencesAreConsecutive;
|
SequenceNumbersAreConsecutive = sequencesAreConsecutive;
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ namespace CryptoExchange.Net.OrderBook
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The bid list
|
/// The bid list
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
||||||
protected SortedList<decimal, OrderBookEntry> bids;
|
protected SortedList<decimal, OrderBookEntry> bids;
|
||||||
private OrderBookStatus status;
|
private OrderBookStatus status;
|
||||||
private UpdateSubscription subscription;
|
private UpdateSubscription subscription;
|
||||||
@ -71,10 +72,17 @@ namespace CryptoExchange.Net.OrderBook
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public event Action<OrderBookStatus, OrderBookStatus> OnStatusChange;
|
public event Action<OrderBookStatus, OrderBookStatus> OnStatusChange;
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
public event Action OnOrderBookUpdate;
|
public event Action OnOrderBookUpdate;
|
||||||
/// <summary>
|
/// <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
|
/// The number of asks in the book
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int AskCount { get; private set; }
|
public int AskCount { get; private set; }
|
||||||
@ -141,6 +149,7 @@ namespace CryptoExchange.Net.OrderBook
|
|||||||
id = options.OrderBookName;
|
id = options.OrderBookName;
|
||||||
processBuffer = new List<ProcessBufferEntry>();
|
processBuffer = new List<ProcessBufferEntry>();
|
||||||
sequencesAreConsecutive = options.SequenceNumbersAreConsecutive;
|
sequencesAreConsecutive = options.SequenceNumbersAreConsecutive;
|
||||||
|
updateEventInterval = options.UpdateEventTimeout;
|
||||||
Symbol = symbol;
|
Symbol = symbol;
|
||||||
Status = OrderBookStatus.Disconnected;
|
Status = OrderBookStatus.Disconnected;
|
||||||
|
|
||||||
@ -262,7 +271,12 @@ namespace CryptoExchange.Net.OrderBook
|
|||||||
|
|
||||||
CheckProcessBuffer();
|
CheckProcessBuffer();
|
||||||
bookSet = true;
|
bookSet = true;
|
||||||
|
LastOrderBookUpdate = DateTime.UtcNow;
|
||||||
|
if ((LastOrderBookUpdate - LastOrderBookUpdateEventTrigger).TotalMilliseconds >= updateEventInterval)
|
||||||
|
{
|
||||||
OnOrderBookUpdate?.Invoke();
|
OnOrderBookUpdate?.Invoke();
|
||||||
|
LastOrderBookUpdateEventTrigger = DateTime.UtcNow;
|
||||||
|
}
|
||||||
log.Write(LogVerbosity.Debug, $"{id} order book {Symbol} data set: {BidCount} bids, {AskCount} asks");
|
log.Write(LogVerbosity.Debug, $"{id} order book {Symbol} data set: {BidCount} bids, {AskCount} asks");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -303,7 +317,12 @@ namespace CryptoExchange.Net.OrderBook
|
|||||||
ProcessUpdate(entry.Type, entry.Entry);
|
ProcessUpdate(entry.Type, entry.Entry);
|
||||||
LastSequenceNumber = lastSequenceNumber;
|
LastSequenceNumber = lastSequenceNumber;
|
||||||
CheckProcessBuffer();
|
CheckProcessBuffer();
|
||||||
|
LastOrderBookUpdate = DateTime.UtcNow;
|
||||||
|
if ((LastOrderBookUpdate - LastOrderBookUpdateEventTrigger).TotalMilliseconds >= updateEventInterval)
|
||||||
|
{
|
||||||
OnOrderBookUpdate?.Invoke();
|
OnOrderBookUpdate?.Invoke();
|
||||||
|
LastOrderBookUpdateEventTrigger = DateTime.UtcNow;
|
||||||
|
}
|
||||||
log.Write(LogVerbosity.Debug, $"{id} order book {Symbol} update: {entries.Count} entries processed");
|
log.Write(LogVerbosity.Debug, $"{id} order book {Symbol} update: {entries.Count} entries processed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user