From 875107c8e564c6f45b17de80f0a5ca9d2baa6d8d Mon Sep 17 00:00:00 2001 From: Jkorf Date: Wed, 23 Oct 2024 10:30:56 +0200 Subject: [PATCH] Added support for different order book levels in OKXSymbolOrderBook --- .../SymbolOrderBooks/OKXSymbolOrderBook.cs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/OKX.Net/SymbolOrderBooks/OKXSymbolOrderBook.cs b/OKX.Net/SymbolOrderBooks/OKXSymbolOrderBook.cs index b9188e8..68f9619 100644 --- a/OKX.Net/SymbolOrderBooks/OKXSymbolOrderBook.cs +++ b/OKX.Net/SymbolOrderBooks/OKXSymbolOrderBook.cs @@ -16,6 +16,9 @@ public class OKXSymbolOrderBook : SymbolOrderBook private readonly IOKXSocketClient _socketClient; private readonly bool _clientOwner; private bool _initialSnapshotDone; + private int? _levels; + private bool _snapshots; + private OrderBookType _type; private readonly TimeSpan _initialDataTimeout; /// @@ -48,15 +51,27 @@ public OKXSymbolOrderBook(string symbol, _sequencesAreConsecutive = false; _strictLevels = true; _initialDataTimeout = options?.InitialDataTimeout ?? TimeSpan.FromSeconds(30); + _levels = options?.Limit; _socketClient = socketClient ?? new OKXSocketClient(); _clientOwner = socketClient == null; + + + _levels?.ValidateIntValues("Limit", 1, 5, 400); + _type = OrderBookType.BBO_TBT; + if (_levels == 5) + _type = OrderBookType.OrderBook_5; + else if (_levels == 400 || _levels == null) + _type = OrderBookType.OrderBook; + + _snapshots = _type == OrderBookType.OrderBook_5 || _type == OrderBookType.BBO_TBT; } /// protected override async Task> DoStartAsync(CancellationToken ct) { - var result = await _socketClient.UnifiedApi.ExchangeData.SubscribeToOrderBookUpdatesAsync(Symbol, OrderBookType.OrderBook, ProcessUpdate).ConfigureAwait(false); + + var result = await _socketClient.UnifiedApi.ExchangeData.SubscribeToOrderBookUpdatesAsync(Symbol, _type, ProcessUpdate).ConfigureAwait(false); if (!result) return result; @@ -69,7 +84,7 @@ protected override async Task> DoStartAsync(Cance Status = OrderBookStatus.Syncing; var setResult = await WaitForSetOrderBookAsync(_initialDataTimeout, ct).ConfigureAwait(false); - return setResult ? result : new CallResult(setResult.Error!); + return setResult ? result : new CallResult(setResult.Error!); } /// @@ -80,8 +95,7 @@ protected override void DoReset() private void ProcessUpdate(DataEvent data) { - - if (!_initialSnapshotDone) + if (!_initialSnapshotDone || _snapshots) { SetInitialOrderBook(data.Data.Time.Ticks, data.Data.Bids, data.Data.Asks); _initialSnapshotDone = true;