1111using CryptoExchange . Net . Objects ;
1212using CryptoExchange . Net . Sockets ;
1313using NLog ;
14+ using Binance . Net . Objects . Models . Futures . Socket ;
1415
1516
1617namespace BinanceBot . Market ;
@@ -111,7 +112,7 @@ public async Task BuildAsync(MarketDepth marketDepth, short orderBookDepth = 10,
111112
112113 // Step 2: Wait a bit to buffer some events
113114 // Use longer buffer time to ensure we have enough events before snapshot
114- var bufferTimeMs = Math . Max ( updateIntervalMs * 5 , 500 ) ;
115+ var bufferTimeMs = Math . Max ( updateIntervalMs * 5 , 1000 ) ;
115116 _logger . Debug ( $ "2: Buffering events for { bufferTimeMs } ms") ;
116117 await Task . Delay ( bufferTimeMs , ct ) . ConfigureAwait ( false ) ;
117118
@@ -151,8 +152,7 @@ public async Task BuildAsync(MarketDepth marketDepth, short orderBookDepth = 10,
151152 IBinanceEventOrderBook firstEvent = null ;
152153 lock ( _eventBuffer )
153154 {
154- if ( _eventBuffer . Count > 0 )
155- firstEvent = _eventBuffer . Peek ( ) ;
155+ firstEvent = _eventBuffer . Any ( ) ? _eventBuffer . Peek ( ) : null ;
156156 }
157157
158158 if ( firstEvent != null )
@@ -229,13 +229,11 @@ public async Task BuildAsync(MarketDepth marketDepth, short orderBookDepth = 10,
229229
230230
231231 /// <summary>
232- /// Stream <see cref="MarketDepth"/> updates
233- /// </summary>
234- /// <param name="marketDepth">Market depth</param>
235232 /// Stream <see cref="MarketDepth"/> updates asynchronously.
236233 /// </summary>
237234 /// <param name="marketDepth">Market depth</param>
238235 /// <param name="updateInterval">Update interval (100ms or 1000ms)</param>
236+ /// <param name="ct">Cancellation token</param>
239237 public async Task StreamUpdatesAsync ( MarketDepth marketDepth , TimeSpan ? updateInterval = default , CancellationToken ct = default )
240238 {
241239 if ( marketDepth == null )
@@ -269,6 +267,8 @@ public async Task StreamUpdatesAsync(MarketDepth marketDepth, TimeSpan? updateIn
269267
270268 _subscription = subscriptionResult . Data ;
271269 }
270+
271+ /// <summary>
272272 /// Stop streaming updates and unsubscribe
273273 /// </summary>
274274 public async Task StopStreamingAsync ( CancellationToken ct = default )
@@ -308,16 +308,28 @@ private void OnDepthUpdate(MarketDepth marketDepth, IBinanceEventOrderBook data)
308308 private void ApplyDepthUpdate ( MarketDepth marketDepth , IBinanceEventOrderBook eventData )
309309 {
310310 // Step 7: Apply update procedure
311-
311+ long lastUpdateId ;
312+ switch ( marketDepth . Symbol . ContractType )
313+ {
314+ case ContractType . Spot :
315+ lastUpdateId = eventData . LastUpdateId ;
316+ break ;
317+ case ContractType . Perpetual :
318+ lastUpdateId = ( eventData as BinanceFuturesStreamOrderBookDepth ) . LastUpdateIdStream ;
319+ break ;
320+ default :
321+ throw new ArgumentOutOfRangeException ( nameof ( marketDepth . Symbol . ContractType ) , "Unknown contract type." ) ;
322+ }
323+
312324 // 1. Decide whether the update event can be applied
313- if ( eventData . LastUpdateId <= _localOrderBookUpdateId )
325+ if ( lastUpdateId <= _localOrderBookUpdateId )
314326 {
315327 // Event is older than local order book, ignore
316- _logger . Debug ( $ "Ignoring old event: u={ eventData . LastUpdateId } <= local={ _localOrderBookUpdateId } ") ;
328+ _logger . Debug ( $ "Ignoring old event: u={ lastUpdateId } <= local={ _localOrderBookUpdateId } ") ;
317329 return ;
318330 }
319331
320- if ( eventData . FirstUpdateId > _localOrderBookUpdateId + 1 )
332+ if ( lastUpdateId > _localOrderBookUpdateId + 1 )
321333 {
322334 // Missed some events - need to restart
323335 _logger . Error ( $ "Missed updates! Expected U <= { _localOrderBookUpdateId + 1 } , got U={ eventData . FirstUpdateId } ") ;
@@ -331,10 +343,10 @@ private void ApplyDepthUpdate(MarketDepth marketDepth, IBinanceEventOrderBook ev
331343
332344 // 2. Update price levels
333345 if ( _localOrderBookUpdateId % 100 == 0 ) // Log every 100th update to avoid flooding
334- _logger . Debug ( $ "Applying update: U={ eventData . FirstUpdateId } , u={ eventData . LastUpdateId } , Asks={ eventData . Asks . Count ( ) } , Bids={ eventData . Bids . Count ( ) } ") ;
335- marketDepth . UpdateDepth ( eventData . Asks , eventData . Bids , eventData . LastUpdateId ) ;
346+ _logger . Debug ( $ "Applying update: U={ eventData . FirstUpdateId } , u={ lastUpdateId } , Asks={ eventData . Asks . Count ( ) } , Bids={ eventData . Bids . Count ( ) } ") ;
347+ marketDepth . UpdateDepth ( eventData . Asks , eventData . Bids , lastUpdateId ) ;
336348
337349 // 3. Set order book update ID
338- _localOrderBookUpdateId = eventData . LastUpdateId ;
350+ _localOrderBookUpdateId = lastUpdateId ;
339351 }
340352}
0 commit comments