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

Merge pull request #17 from ridicoulous/orderBookUpdateEvent

added orderbook update event
This commit is contained in:
Jan Korf 2019-09-04 16:27:02 +02:00 committed by GitHub
commit de0916920f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 14 deletions

View File

@ -1574,6 +1574,16 @@
Event when the state changes Event when the state changes
</summary> </summary>
</member> </member>
<member name="E:CryptoExchange.Net.OrderBook.SymbolOrderBook.OnOrderBookUpdate">
<summary>
Event when orderbook was updated. Be careful! It can generate a lot of events at high-liquidity markets
</summary>
</member>
<member name="F:CryptoExchange.Net.OrderBook.SymbolOrderBook.LastOrderBookUpdate">
<summary>
Should be useful for low-liquidity order-books to monitor market activity
</summary>
</member>
<member name="P:CryptoExchange.Net.OrderBook.SymbolOrderBook.AskCount"> <member name="P:CryptoExchange.Net.OrderBook.SymbolOrderBook.AskCount">
<summary> <summary>
The number of asks in the book The number of asks in the book

View File

@ -26,7 +26,7 @@ namespace CryptoExchange.Net.Objects
/// <summary> /// <summary>
/// Base for order book options /// Base for order book options
/// </summary> /// </summary>
public class OrderBookOptions: BaseOptions public class OrderBookOptions : BaseOptions
{ {
/// <summary> /// <summary>
/// The name of the order book implementation /// The name of the order book implementation
@ -52,7 +52,7 @@ namespace CryptoExchange.Net.Objects
/// <summary> /// <summary>
/// Base client options /// Base client options
/// </summary> /// </summary>
public class ClientOptions: BaseOptions public class ClientOptions : BaseOptions
{ {
/// <summary> /// <summary>
@ -74,7 +74,7 @@ namespace CryptoExchange.Net.Objects
/// <summary> /// <summary>
/// Base for rest client options /// Base for rest client options
/// </summary> /// </summary>
public class RestClientOptions: ClientOptions public class RestClientOptions : ClientOptions
{ {
/// <summary> /// <summary>
/// List of rate limiters to use /// List of rate limiters to use
@ -96,7 +96,7 @@ namespace CryptoExchange.Net.Objects
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <returns></returns> /// <returns></returns>
public T Copy<T>() where T:RestClientOptions, new() public T Copy<T>() where T : RestClientOptions, new()
{ {
var copy = new T var copy = new T
{ {
@ -119,7 +119,7 @@ namespace CryptoExchange.Net.Objects
/// <summary> /// <summary>
/// Base for socket client options /// Base for socket client options
/// </summary> /// </summary>
public class SocketClientOptions: ClientOptions public class SocketClientOptions : ClientOptions
{ {
/// <summary> /// <summary>
/// Whether or not the socket should automatically reconnect when losing connection /// Whether or not the socket should automatically reconnect when losing connection

View File

@ -13,7 +13,7 @@ namespace CryptoExchange.Net.OrderBook
/// <summary> /// <summary>
/// Base for order book implementations /// Base for order book implementations
/// </summary> /// </summary>
public abstract class SymbolOrderBook: IDisposable public abstract class SymbolOrderBook : IDisposable
{ {
/// <summary> /// <summary>
/// The process buffer, used while syncing /// The process buffer, used while syncing
@ -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;
@ -70,6 +71,14 @@ namespace CryptoExchange.Net.OrderBook
/// Event when the state changes /// Event when the state changes
/// </summary> /// </summary>
public event Action<OrderBookStatus, OrderBookStatus> OnStatusChange; public event Action<OrderBookStatus, OrderBookStatus> OnStatusChange;
/// <summary>
/// Event when orderbook was updated. Be careful! It can generate a lot of events at high-liquidity markets
/// </summary>
public event Action OnOrderBookUpdate;
/// <summary>
/// Should be useful for low-liquidity order-books to monitor market activity
/// </summary>
public DateTime LastOrderBookUpdate;
/// <summary> /// <summary>
/// The number of asks in the book /// The number of asks in the book
@ -163,7 +172,7 @@ namespace CryptoExchange.Net.OrderBook
{ {
Status = OrderBookStatus.Connecting; Status = OrderBookStatus.Connecting;
var startResult = await DoStart().ConfigureAwait(false); var startResult = await DoStart().ConfigureAwait(false);
if(!startResult.Success) if (!startResult.Success)
return new CallResult<bool>(false, startResult.Error); return new CallResult<bool>(false, startResult.Error);
subscription = startResult.Data; subscription = startResult.Data;
@ -246,7 +255,7 @@ namespace CryptoExchange.Net.OrderBook
return; return;
asks.Clear(); asks.Clear();
foreach(var ask in askList) foreach (var ask in askList)
asks.Add(ask.Price, new OrderBookEntry(ask.Price, ask.Quantity)); asks.Add(ask.Price, new OrderBookEntry(ask.Price, ask.Quantity));
bids.Clear(); bids.Clear();
foreach (var bid in bidList) foreach (var bid in bidList)
@ -259,6 +268,8 @@ namespace CryptoExchange.Net.OrderBook
CheckProcessBuffer(); CheckProcessBuffer();
bookSet = true; bookSet = true;
LastOrderBookUpdate = DateTime.UtcNow;
OnOrderBookUpdate?.Invoke();
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");
} }
} }
@ -295,10 +306,12 @@ namespace CryptoExchange.Net.OrderBook
} }
else else
{ {
foreach(var entry in entries) foreach (var entry in entries)
ProcessUpdate(entry.Type, entry.Entry); ProcessUpdate(entry.Type, entry.Entry);
LastSequenceNumber = lastSequenceNumber; LastSequenceNumber = lastSequenceNumber;
CheckProcessBuffer(); CheckProcessBuffer();
LastOrderBookUpdate = DateTime.UtcNow;
OnOrderBookUpdate?.Invoke();
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");
} }
} }
@ -311,7 +324,7 @@ namespace CryptoExchange.Net.OrderBook
{ {
foreach (var bufferEntry in processBuffer.OrderBy(b => b.FirstSequence).ToList()) foreach (var bufferEntry in processBuffer.OrderBy(b => b.FirstSequence).ToList())
{ {
if(bufferEntry.LastSequence < LastSequenceNumber) if (bufferEntry.LastSequence < LastSequenceNumber)
{ {
processBuffer.Remove(bufferEntry); processBuffer.Remove(bufferEntry);
continue; continue;
@ -320,7 +333,7 @@ namespace CryptoExchange.Net.OrderBook
if (bufferEntry.FirstSequence > LastSequenceNumber + 1) if (bufferEntry.FirstSequence > LastSequenceNumber + 1)
break; break;
foreach(var entry in bufferEntry.Entries) foreach (var entry in bufferEntry.Entries)
ProcessUpdate(entry.Type, entry.Entry); ProcessUpdate(entry.Type, entry.Entry);
processBuffer.Remove(bufferEntry); processBuffer.Remove(bufferEntry);
LastSequenceNumber = bufferEntry.LastSequence; LastSequenceNumber = bufferEntry.LastSequence;