Skip to content

Commit

Permalink
Added support for different order book levels in OKXSymbolOrderBook
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Oct 23, 2024
1 parent 7f50795 commit 875107c
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions OKX.Net/SymbolOrderBooks/OKXSymbolOrderBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand Down Expand Up @@ -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;
}

/// <inheritdoc />
protected override async Task<CallResult<UpdateSubscription>> 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;

Expand All @@ -69,7 +84,7 @@ protected override async Task<CallResult<UpdateSubscription>> DoStartAsync(Cance
Status = OrderBookStatus.Syncing;

var setResult = await WaitForSetOrderBookAsync(_initialDataTimeout, ct).ConfigureAwait(false);
return setResult ? result : new CallResult<UpdateSubscription>(setResult.Error!);
return setResult ? result : new CallResult<UpdateSubscription>(setResult.Error!);
}

/// <inheritdoc />
Expand All @@ -80,8 +95,7 @@ protected override void DoReset()

private void ProcessUpdate(DataEvent<OKXOrderBook> data)
{

if (!_initialSnapshotDone)
if (!_initialSnapshotDone || _snapshots)
{
SetInitialOrderBook(data.Data.Time.Ticks, data.Data.Bids, data.Data.Asks);
_initialSnapshotDone = true;
Expand Down

0 comments on commit 875107c

Please sign in to comment.