Esempio n. 1
0
        private OrderBook CloneWithChangedSource(OrderBook ob)
        {
            var newOb = ob.Clone(ob.Timestamp);

            newOb.Source = GetSyntheticSourceName(ob.Asset);
            return(newOb);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="event"></param>
        /// <returns></returns>
        protected override async ValueTask <OrderBookCacheEventArgs> OnAction(DepthUpdateEventArgs @event)
        {
            if (_limit > 0)
            {
                // Ignore events with same or earlier order book update.
                if (_orderBookClone != null && @event.LastUpdateId <= _orderBookClone.LastUpdateId)
                {
                    Logger?.LogDebug($"{nameof(OrderBookCache)} ({_symbol}): Ignoring event (last update ID: {@event.LastUpdateId}).  [thread: {Thread.CurrentThread.ManagedThreadId}{(@event.Token.IsCancellationRequested ? ", canceled" : string.Empty)}]");
                    return(null);
                }

                // Top <limit> bids and asks, pushed every second.
                // NOTE: LastUpdateId is not contiguous between events when using partial depth stream.
                _orderBookClone = new OrderBook(_symbol, @event.LastUpdateId, @event.Bids, @event.Asks);
            }
            else
            {
                // If order book has not been initialized or is out-of-sync (gap in data).
                while (_orderBook == null || @event.FirstUpdateId > _orderBook.LastUpdateId + 1)
                {
                    if (_orderBook != null)
                    {
                        OutOfSync?.Invoke(this, EventArgs.Empty);
                    }

                    // Synchronize.
                    await SynchronizeOrderBookAsync(@event.Token)
                    .ConfigureAwait(false);
                }

                // Ignore events prior to order book snapshot.
                if (@event.LastUpdateId <= _orderBook.LastUpdateId)
                {
                    Logger?.LogDebug($"{nameof(OrderBookCache)} ({_symbol}): Ignoring event (last update ID: {@event.LastUpdateId}).  [thread: {Thread.CurrentThread.ManagedThreadId}{(@event.Token.IsCancellationRequested ? ", canceled" : string.Empty)}]");
                    return(null);
                }

                Logger?.LogDebug($"{nameof(OrderBookCache)} ({_symbol}): Updating order book (last update ID: {_orderBook.LastUpdateId} => {@event.LastUpdateId}).  [thread: {Thread.CurrentThread.ManagedThreadId}{(@event.Token.IsCancellationRequested ? ", canceled" : string.Empty)}]");

                _orderBook.Modify(@event.LastUpdateId, @event.Bids, @event.Asks);

                _orderBookClone = _orderBook.Clone();
            }

            return(new OrderBookCacheEventArgs(_orderBookClone));
        }
        private void ApplyEventToOrderBook(OrderBookUpdate update)
        {
            if (update == null)
            {
                throw new ArgumentNullException(nameof(update));
            }

            var newOrderbook = OrderBook.Clone(DateTime.UtcNow);

            foreach (var ask in update.Asks)
            {
                newOrderbook.UpdateAsk(ask[0], ask[1]);
            }

            foreach (var bid in update.Bids)
            {
                newOrderbook.UpdateBid(bid[0], bid[1]);
            }

            OrderBook = newOrderbook;
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="event"></param>
        /// <returns></returns>
        protected override async Task <OrderBookCacheEventArgs> OnAction(DepthUpdateEventArgs @event)
        {
            if (_limit > 0)
            {
                // Ignore events with same or earlier order book update.
                if (_orderBookClone != null && @event.LastUpdateId <= _orderBookClone.LastUpdateId)
                {
                    return(null);
                }

                // Top <level> bids and asks, pushed every second.
                // NOTE: LastUpdateId is not contiguous between events when using partial depth stream.
                _orderBookClone = new OrderBook(_symbol, @event.LastUpdateId, @event.Bids, @event.Asks);
            }
            else
            {
                // If order book has not been initialized.
                if (_orderBook == null)
                {
                    // Synchronize.
                    await SynchronizeOrderBookAsync()
                    .ConfigureAwait(false);
                }

                // Ignore events prior to order book snapshot.
                if (@event.LastUpdateId <= _orderBook.LastUpdateId)
                {
                    return(null);
                }

                // If there is a gap in events (order book out-of-sync).
                // ReSharper disable once InvertIf
                if (@event.FirstUpdateId > _orderBook.LastUpdateId + 1)
                {
                    Logger?.LogError($"{nameof(OrderBookCache)}: Synchronization failure (first update ID > last update ID + 1).");

                    await Task.Delay(1000, Token)
                    .ConfigureAwait(false);     // wait a bit.

                    // Re-synchronize.
                    await SynchronizeOrderBookAsync()
                    .ConfigureAwait(false);

                    // If still out-of-sync.
                    // ReSharper disable once InvertIf
                    if (@event.FirstUpdateId > _orderBook.LastUpdateId + 1)
                    {
                        Logger?.LogError($"{nameof(OrderBookCache)}: Re-Synchronization failure (first update ID > last update ID + 1).");

                        // Reset and wait for next event.
                        _orderBook = null;
                        return(null);
                    }
                }

                Logger?.LogDebug($"{nameof(OrderBookCache)}: Updating order book [last update ID: {@event.LastUpdateId}].");

                _orderBook.Modify(@event.LastUpdateId, @event.Bids, @event.Asks);

                _orderBookClone = _orderBook.Clone();
            }

            return(new OrderBookCacheEventArgs(_orderBookClone));
        }