Пример #1
0
        /// <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);
        }
Пример #3
0
        public override async Task RunCommand()
        {
            var marketSymbol  = "BTC-USD";
            var marketSymbol2 = "XXBTZUSD";

            IExchangeAPI
                apiCoinbase = await ExchangeAPI.GetExchangeAPIAsync(ExchangeName.Coinbase),
                apiGemini   = await ExchangeAPI.GetExchangeAPIAsync(ExchangeName.Gemini),
                apiKraken   = await ExchangeAPI.GetExchangeAPIAsync(ExchangeName.Kraken),
                apiBitfinex = await ExchangeAPI.GetExchangeAPIAsync(ExchangeName.Bitfinex);

            //TODO: Make this multi-threaded and add parameters
            Console.WriteLine("Use CTRL-C to stop.");

            while (true)
            {
                var ticker = await apiCoinbase.GetTickerAsync(marketSymbol);

                var orders = await apiCoinbase.GetOrderBookAsync(marketSymbol);

                var askAmountSum = orders.Asks.Values.Sum(o => o.Amount);
                var askPriceSum  = orders.Asks.Values.Sum(o => o.Price);
                var bidAmountSum = orders.Bids.Values.Sum(o => o.Amount);
                var bidPriceSum  = orders.Bids.Values.Sum(o => o.Price);

                var ticker2 = await apiGemini.GetTickerAsync(marketSymbol);

                var orders2 = await apiGemini.GetOrderBookAsync(marketSymbol);

                var askAmountSum2 = orders2.Asks.Values.Sum(o => o.Amount);
                var askPriceSum2  = orders2.Asks.Values.Sum(o => o.Price);
                var bidAmountSum2 = orders2.Bids.Values.Sum(o => o.Amount);
                var bidPriceSum2  = orders2.Bids.Values.Sum(o => o.Price);

                var ticker3 = await apiKraken.GetTickerAsync(marketSymbol2);

                var orders3 = await apiKraken.GetOrderBookAsync(marketSymbol2);

                var askAmountSum3 = orders3.Asks.Values.Sum(o => o.Amount);
                var askPriceSum3  = orders3.Asks.Values.Sum(o => o.Price);
                var bidAmountSum3 = orders3.Bids.Values.Sum(o => o.Amount);
                var bidPriceSum3  = orders3.Bids.Values.Sum(o => o.Price);

                var ticker4 = await apiBitfinex.GetTickerAsync(marketSymbol);

                var orders4 = await apiBitfinex.GetOrderBookAsync(marketSymbol);

                var askAmountSum4 = orders4.Asks.Values.Sum(o => o.Amount);
                var askPriceSum4  = orders4.Asks.Values.Sum(o => o.Price);
                var bidAmountSum4 = orders4.Bids.Values.Sum(o => o.Amount);
                var bidPriceSum4  = orders4.Bids.Values.Sum(o => o.Price);

                Console.Clear();
                Console.WriteLine("GDAX: {0,13:N}, {1,15:N}, {2,8:N}, {3,13:N}, {4,8:N}, {5,13:N}", ticker.Last,
                                  ticker.Volume.QuoteCurrencyVolume, askAmountSum, askPriceSum, bidAmountSum, bidPriceSum);
                Console.WriteLine("GEMI: {0,13:N}, {1,15:N}, {2,8:N}, {3,13:N}, {4,8:N}, {5,13:N}", ticker2.Last,
                                  ticker2.Volume.QuoteCurrencyVolume, askAmountSum2, askPriceSum2, bidAmountSum2, bidPriceSum2);
                Console.WriteLine("KRAK: {0,13:N}, {1,15:N}, {2,8:N}, {3,13:N}, {4,8:N}, {5,13:N}", ticker3.Last,
                                  ticker3.Volume.QuoteCurrencyVolume, askAmountSum3, askPriceSum3, bidAmountSum3, bidPriceSum3);
                Console.WriteLine("BITF: {0,13:N}, {1,15:N}, {2,8:N}, {3,13:N}, {4,8:N}, {5,13:N}", ticker4.Last,
                                  ticker4.Volume.QuoteCurrencyVolume, askAmountSum4, askPriceSum4, bidAmountSum4, bidPriceSum4);
                Thread.Sleep(IntervalMs);
            }
        }