void RefreshOrderBook(string product, OrderBookBuilder book) { // Coinbase doesn't give us server time together with the full order book, // so we retrieve it with a separate request BEFORE requesting the order book. DateTime serverTime = _restClient.SendRequest(new REST.TimeRequest()).Result.Time; REST.OrderBookResponse snapshot = _restClient.SendRequest(new REST.OrderBookRequest() { Product = product }).Result; DateTime received = DateTime.UtcNow; OrderBookDelta delta = book.OnSnapshot(serverTime, snapshot); // Throws if the snapshot is malformed. if (delta != null && (delta.Bids.Any() || delta.Asks.Any())) { try { OnOrderBook?.Invoke( product, new TimestampedMsg <OrderBookDelta>() { Received = received, Value = delta }); } catch (Exception e) { _log.Warn(e, "Ignoring exception from OnOrderBook"); } } }
public OrderBookDelta OnSnapshot(DateTime serverTime, REST.OrderBookResponse snapshot) { Condition.Requires(snapshot, "snapshot").IsNotNull(); Condition.Requires(snapshot.Sequence, "snapshot.Sequence").IsGreaterOrEqual(_seqNum); var bids = Aggregate(snapshot.Bids); var asks = Aggregate(snapshot.Asks); var delta = new OrderBookDelta() { Time = serverTime, Bids = Diff(_bids, bids).ToList(), Asks = Diff(_asks, asks).ToList(), Sequence = snapshot.Sequence, }; delta.Bids.Reverse(); _seqNum = snapshot.Sequence; _bids = bids; _asks = asks; return(delta); }