/// <summary> /// Get full order book bids and asks via web socket. This is efficient and will only use the order book deltas. /// </summary> /// <param name="callback">Callback of symbol, order book</param> /// <param name="maxCount">Max count of bids and asks - not all exchanges will honor this parameter</param> /// <param name="symbol">Ticker symbols or null/empty for all of them (if supported)</param> /// <returns>Web socket, call Dispose to close</returns> public static IDisposable GetOrderBookWebSocket(this IExchangeAPI api, Action <ExchangeOrderBook> callback, int maxCount = 20, params string[] symbols) { // Gets a delta socket for a collection of order books, then maintains a full order book. // The suggested way to use this is: // 1. Open this socket and begin buffering events you receive // 2. Get a depth snapshot of the order books you care about // 3. Drop any event where SequenceNumber is less than or equal to the snapshot last update id // Notes: // * Confirm with the Exchange's API docs whether the data in each event is the absolute quantity or differential quantity // * If the quantity is 0, remove the price level // * Receiving an event that removes a price level that is not in your local order book can happen and is normal. // Dictionary <string, ExchangeOrderBook> fullBooks = new Dictionary <string, ExchangeOrderBook>(); void innerCallback(ExchangeOrderBook book) { // see if we have a full order book for the symbol if (!fullBooks.TryGetValue(book.Symbol, out ExchangeOrderBook fullBook)) { fullBooks[book.Symbol] = fullBook = api.GetOrderBook(book.Symbol, 1000); fullBook.Symbol = book.Symbol; } // update deltas as long as the full book is at or before the delta timestamp if (fullBook.SequenceId <= book.SequenceId) { foreach (var ask in book.Asks) { if (ask.Value.Amount <= 0m || ask.Value.Price <= 0m) { fullBook.Asks.Remove(ask.Value.Price); } else { fullBook.Asks[ask.Value.Price] = ask.Value; } } foreach (var bid in book.Bids) { if (bid.Value.Amount <= 0m || bid.Value.Price <= 0m) { fullBook.Bids.Remove(bid.Value.Price); } else { fullBook.Bids[bid.Value.Price] = bid.Value; } } fullBook.SequenceId = book.SequenceId; } callback(fullBook); }; return(api.GetOrderBookDeltasWebSocket(innerCallback, maxCount, symbols)); }
/// <summary>Get full order book bids and asks via web socket. This is efficient and will /// only use the order book deltas (if supported by the exchange).</summary> /// <param name="callback">Callback containing full order book</param> /// <param name="maxCount">Max count of bids and asks - not all exchanges will honor this /// parameter</param> /// <param name="symbols">Ticker symbols or null/empty for all of them (if supported)</param> /// <returns>Web socket, call Dispose to close</returns> public static IWebSocket GetOrderBookWebSocket(this IExchangeAPI api, Action <ExchangeOrderBook> callback, int maxCount = 20, params string[] symbols) { // Notes: // * Confirm with the Exchange's API docs whether the data in each event is the absolute quantity or differential quantity // * Receiving an event that removes a price level that is not in your local order book can happen and is normal. var fullBooks = new ConcurrentDictionary <string, ExchangeOrderBook>(); void applyDelta(SortedDictionary <decimal, ExchangeOrderPrice> deltaValues, SortedDictionary <decimal, ExchangeOrderPrice> bookToEdit) { foreach (ExchangeOrderPrice record in deltaValues.Values) { if (record.Amount <= 0 || record.Price <= 0) { bookToEdit.Remove(record.Price); } else { bookToEdit[record.Price] = record; } } } void updateOrderBook(ExchangeOrderBook fullOrderBook, ExchangeOrderBook freshBook) { lock (fullOrderBook) { // update deltas as long as the full book is at or before the delta timestamp if (fullOrderBook.SequenceId <= freshBook.SequenceId) { applyDelta(freshBook.Asks, fullOrderBook.Asks); applyDelta(freshBook.Bids, fullOrderBook.Bids); fullOrderBook.SequenceId = freshBook.SequenceId; } } } async Task innerCallback(ExchangeOrderBook freshBook) { bool foundFullBook = fullBooks.TryGetValue(freshBook.Symbol, out ExchangeOrderBook fullOrderBook); switch (api.Name) { // Fetch an initial book the first time and apply deltas on top case ExchangeName.Bittrex: case ExchangeName.Binance: case ExchangeName.Poloniex: { // If we don't have an initial order book for this symbol, fetch it if (!foundFullBook) { fullBooks[freshBook.Symbol] = fullOrderBook = await api.GetOrderBookAsync(freshBook.Symbol, 1000); fullOrderBook.Symbol = freshBook.Symbol; } else { updateOrderBook(fullOrderBook, freshBook); } break; } // First response from exchange will be the full order book. // Subsequent updates will be deltas case ExchangeName.Okex: { if (!foundFullBook) { fullBooks[freshBook.Symbol] = fullOrderBook = freshBook; } else { updateOrderBook(fullOrderBook, freshBook); } break; } // Websocket always returns full order book case ExchangeName.Huobi: { fullBooks[freshBook.Symbol] = fullOrderBook = freshBook; break; } } fullOrderBook.LastUpdatedUtc = DateTime.UtcNow; callback(fullOrderBook); } IWebSocket socket = api.GetOrderBookDeltasWebSocket(async(b) => await innerCallback(b), maxCount, symbols); socket.Connected += (s) => { // when we re-connect, we must invalidate the order books, who knows how long we were disconnected // and how out of date the order books are fullBooks.Clear(); }; return(socket); }
/// <summary>Get full order book bids and asks via web socket. This is efficient and will /// only use the order book deltas (if supported by the exchange).</summary> /// <param name="callback">Callback containing full order book</param> /// <param name="maxCount">Max count of bids and asks - not all exchanges will honor this /// parameter</param> /// <param name="symbols">Ticker symbols or null/empty for all of them (if supported)</param> /// <returns>Web socket, call Dispose to close</returns> public static IWebSocket GetOrderBookWebSocket(this IExchangeAPI api, Action <ExchangeOrderBook> callback, int maxCount = 20, params string[] symbols) { // Notes: // * Confirm with the Exchange's API docs whether the data in each event is the absolute quantity or differential quantity // * Receiving an event that removes a price level that is not in your local order book can happen and is normal. var fullBooks = new ConcurrentDictionary <string, ExchangeOrderBook>(); var partialOrderBookQueues = new Dictionary <string, Queue <ExchangeOrderBook> >(); void applyDelta(SortedDictionary <decimal, ExchangeOrderPrice> deltaValues, SortedDictionary <decimal, ExchangeOrderPrice> bookToEdit) { foreach (ExchangeOrderPrice record in deltaValues.Values) { if (record.Amount <= 0 || record.Price <= 0) { bookToEdit.Remove(record.Price); } else { bookToEdit[record.Price] = record; } } } void updateOrderBook(ExchangeOrderBook fullOrderBook, ExchangeOrderBook freshBook) { lock (fullOrderBook) { // update deltas as long as the full book is at or before the delta timestamp if (fullOrderBook.SequenceId <= freshBook.SequenceId) { applyDelta(freshBook.Asks, fullOrderBook.Asks); applyDelta(freshBook.Bids, fullOrderBook.Bids); fullOrderBook.SequenceId = freshBook.SequenceId; } } } async Task innerCallback(ExchangeOrderBook newOrderBook) { // depending on the exchange, newOrderBook may be a complete or partial order book // ideally all exchanges would send the full order book on first message, followed by delta order books // but this is not the case bool foundFullBook = fullBooks.TryGetValue(newOrderBook.Symbol, out ExchangeOrderBook fullOrderBook); switch (api.Name) { // Fetch an initial book the first time and apply deltas on top // send these exchanges scathing support tickets that they should send // the full book for the first web socket callback message case ExchangeName.Bittrex: case ExchangeName.Binance: case ExchangeName.Poloniex: { Queue <ExchangeOrderBook> partialOrderBookQueue; bool requestFullOrderBook = false; // attempt to find the right queue to put the partial order book in to be processed later lock (partialOrderBookQueues) { if (!partialOrderBookQueues.TryGetValue(newOrderBook.Symbol, out partialOrderBookQueue)) { // no queue found, make a new one partialOrderBookQueues[newOrderBook.Symbol] = partialOrderBookQueue = new Queue <ExchangeOrderBook>(); requestFullOrderBook = !foundFullBook; } // always enqueue the partial order book, they get dequeued down below partialOrderBookQueue.Enqueue(newOrderBook); } // request the entire order book if we need it if (requestFullOrderBook) { fullOrderBook = await api.GetOrderBookAsync(newOrderBook.Symbol, maxCount); fullOrderBook.Symbol = newOrderBook.Symbol; fullBooks[newOrderBook.Symbol] = fullOrderBook; } else if (!foundFullBook) { // we got a partial book while the full order book was being requested // return out, the full order book loop will process this item in the queue return; } // else new partial book with full order book available, will get dequeued below // check if any old books for this symbol, if so process them first // lock dictionary of queues for lookup only lock (partialOrderBookQueues) { partialOrderBookQueues.TryGetValue(newOrderBook.Symbol, out partialOrderBookQueue); } if (partialOrderBookQueue != null) { // lock the individual queue for processing, fifo queue lock (partialOrderBookQueue) { while (partialOrderBookQueue.Count != 0) { updateOrderBook(fullOrderBook, partialOrderBookQueue.Dequeue()); } } } break; } // First response from exchange will be the full order book. // Subsequent updates will be deltas, at least some exchanges have their heads on straight case ExchangeName.BitMEX: case ExchangeName.Okex: case ExchangeName.Coinbase: { if (!foundFullBook) { fullBooks[newOrderBook.Symbol] = fullOrderBook = newOrderBook; } else { updateOrderBook(fullOrderBook, newOrderBook); } break; } // Websocket always returns full order book, WTF...? case ExchangeName.Huobi: { fullBooks[newOrderBook.Symbol] = fullOrderBook = newOrderBook; break; } default: throw new NotSupportedException("Full order book web socket not supported for exchange " + api.Name); } fullOrderBook.LastUpdatedUtc = DateTime.UtcNow; callback(fullOrderBook); } IWebSocket socket = api.GetOrderBookDeltasWebSocket(async(b) => { try { await innerCallback(b); } catch { } }, maxCount, symbols); socket.Connected += (s) => { // when we re-connect, we must invalidate the order books, who knows how long we were disconnected // and how out of date the order books are fullBooks.Clear(); lock (partialOrderBookQueues) { partialOrderBookQueues.Clear(); } return(Task.CompletedTask); }; return(socket); }
/// <summary>Get full order book bids and asks via web socket. This is efficient and will /// only use the order book deltas (if supported by the exchange).</summary> /// <param name="callback">Callback containing full order book</param> /// <param name="maxCount">Max count of bids and asks - not all exchanges will honor this /// parameter</param> /// <param name="symbols">Ticker symbols or null/empty for all of them (if supported)</param> /// <returns>Web socket, call Dispose to close</returns> public static IWebSocket GetOrderBookWebSocket(this IExchangeAPI api, Action <ExchangeOrderBook> callback, int maxCount = 20, params string[] symbols) { // Notes: // * Confirm with the Exchange's API docs whether the data in each event is the absolute quantity or differential quantity // * Receiving an event that removes a price level that is not in your local order book can happen and is normal. var fullBooks = new ConcurrentDictionary <string, ExchangeOrderBook>(); var freshBooksQueue = new Dictionary <string, Queue <ExchangeOrderBook> >(); var fullBookRequestLock = new HashSet <string>(); void applyDelta(SortedDictionary <decimal, ExchangeOrderPrice> deltaValues, SortedDictionary <decimal, ExchangeOrderPrice> bookToEdit) { foreach (ExchangeOrderPrice record in deltaValues.Values) { if (record.Amount <= 0 || record.Price <= 0) { bookToEdit.Remove(record.Price); } else { bookToEdit[record.Price] = record; } } } void updateOrderBook(ExchangeOrderBook fullOrderBook, ExchangeOrderBook freshBook) { lock (fullOrderBook) { // update deltas as long as the full book is at or before the delta timestamp if (fullOrderBook.SequenceId <= freshBook.SequenceId) { applyDelta(freshBook.Asks, fullOrderBook.Asks); applyDelta(freshBook.Bids, fullOrderBook.Bids); fullOrderBook.SequenceId = freshBook.SequenceId; } } } async Task innerCallback(ExchangeOrderBook freshBook) { bool foundFullBook = fullBooks.TryGetValue(freshBook.Symbol, out ExchangeOrderBook fullOrderBook); switch (api.Name) { // Fetch an initial book the first time and apply deltas on top // send these exchanges scathing support tickets that they should send // the full book for the first web socket callback message case ExchangeName.Bittrex: case ExchangeName.Binance: case ExchangeName.Poloniex: { if (!foundFullBook) { // attempt to find the right queue to put the partial order book in to be processed later lock (freshBooksQueue) { if (!freshBooksQueue.TryGetValue(freshBook.Symbol, out Queue <ExchangeOrderBook> freshQueue)) { // no queue found, make a new one freshBooksQueue[freshBook.Symbol] = freshQueue = new Queue <ExchangeOrderBook>(); } freshQueue.Enqueue(freshBook); } bool makeRequest; lock (fullBookRequestLock) { makeRequest = fullBookRequestLock.Add(freshBook.Symbol); } if (makeRequest) { // we are the first to see this symbol, make a full request to API fullBooks[freshBook.Symbol] = fullOrderBook = await api.GetOrderBookAsync(freshBook.Symbol, maxCount); fullOrderBook.Symbol = freshBook.Symbol; // now that we have the full order book, we can process it (and any books in the queue) } else { // stop processing, other code will take these items out of the queue later return; } } // check if any old books for this symbol, if so process them first lock (freshBooksQueue) { if (freshBooksQueue.TryGetValue(freshBook.Symbol, out Queue <ExchangeOrderBook> freshQueue)) { while (freshQueue.Count != 0) { updateOrderBook(fullOrderBook, freshQueue.Dequeue()); } } } break; } // First response from exchange will be the full order book. // Subsequent updates will be deltas, at least some exchanges have their heads on straight case ExchangeName.BitMEX: case ExchangeName.Okex: case ExchangeName.Coinbase: { if (!foundFullBook) { fullBooks[freshBook.Symbol] = fullOrderBook = freshBook; } else { updateOrderBook(fullOrderBook, freshBook); } break; } // Websocket always returns full order book case ExchangeName.Huobi: { fullBooks[freshBook.Symbol] = fullOrderBook = freshBook; break; } default: throw new NotSupportedException("Full order book web socket not supported for exchange " + api.Name); } fullOrderBook.LastUpdatedUtc = DateTime.UtcNow; callback(fullOrderBook); } IWebSocket socket = api.GetOrderBookDeltasWebSocket(async(b) => { try { await innerCallback(b); } catch { } }, maxCount, symbols); socket.Connected += (s) => { // when we re-connect, we must invalidate the order books, who knows how long we were disconnected // and how out of date the order books are fullBooks.Clear(); lock (freshBooksQueue) { freshBooksQueue.Clear(); } lock (fullBookRequestLock) { fullBookRequestLock.Clear(); } return(Task.CompletedTask); }; return(socket); }
/// <summary>Get full order book bids and asks via web socket. This is efficient and will /// only use the order book deltas (if supported by the exchange).</summary> /// <param name="callback">Callback containing full order book</param> /// <param name="maxCount">Max count of bids and asks - not all exchanges will honor this /// parameter</param> /// <param name="symbols">Ticker symbols or null/empty for all of them (if supported)</param> /// <returns>Web socket, call Dispose to close</returns> public static IDisposable GetOrderBookWebSocket(this IExchangeAPI api, Action <ExchangeOrderBook> callback, int maxCount = 20, params string[] symbols) { // Notes: // * Confirm with the Exchange's API docs whether the data in each event is the absolute quantity or differential quantity // * Receiving an event that removes a price level that is not in your local order book can happen and is normal. var fullBooks = new Dictionary <string, ExchangeOrderBook>(); void innerCallback(ExchangeOrderBook freshBook) { bool foundFullBook = fullBooks.TryGetValue(freshBook.Symbol, out ExchangeOrderBook fullOrderBook); switch (api.Name) { // Fetch an initial book the first time and apply deltas on top case ExchangeName.Bittrex: case ExchangeName.Binance: case ExchangeName.Poloniex: { // If we don't have an initial order book for this symbol, fetch it if (!foundFullBook) { fullBooks[freshBook.Symbol] = fullOrderBook = api.GetOrderBook(freshBook.Symbol, 1000); fullOrderBook.Symbol = freshBook.Symbol; } // update deltas as long as the full book is at or before the delta timestamp if (fullOrderBook.SequenceId <= freshBook.SequenceId) { ApplyDelta(freshBook.Asks, fullOrderBook.Asks); ApplyDelta(freshBook.Bids, fullOrderBook.Bids); fullOrderBook.SequenceId = freshBook.SequenceId; } break; } // First response from exchange will be the full order book. // Subsequent updates will be deltas case ExchangeName.BitMEX: case ExchangeName.Okex: { if (!foundFullBook) { fullBooks[freshBook.Symbol] = fullOrderBook = freshBook; fullOrderBook.Symbol = freshBook.Symbol; } else { ApplyDelta(freshBook.Asks, fullOrderBook.Asks); ApplyDelta(freshBook.Bids, fullOrderBook.Bids); } break; } // Websocket always returns full order book case ExchangeName.Huobi: fullBooks[freshBook.Symbol] = fullOrderBook = freshBook; break; } callback(fullOrderBook); } return(api.GetOrderBookDeltasWebSocket(innerCallback, maxCount, symbols)); }