Example #1
0
        private void UpdateOrderBook(Interface.OrderBook orderBook)
        {
            if (!Symbol.Name.Equals(orderBook.Symbol))
            {
                return;
            }

            lock (orderBookLock)
            {
                if (OrderBook == null ||
                    OrderBook.Symbol != Symbol.Name)
                {
                    OrderBook = new OrderBook
                    {
                        Symbol      = orderBook.Symbol,
                        BaseSymbol  = Symbol.BaseAsset.Symbol,
                        QuoteSymbol = Symbol.QuoteAsset.Symbol
                    };
                }

                if (OrderBook.LastUpdateId < orderBook.LastUpdateId)
                {
                    OrderBook.LastUpdateId = orderBook.LastUpdateId;
                    OrderBook.Top          = new OrderBookTop
                    {
                        Ask = new OrderBookPriceLevel
                        {
                            Price    = orderBook.Top.Ask.Price.Trim(Symbol.PricePrecision),
                            Quantity = orderBook.Top.Ask.Quantity.Trim(Symbol.QuantityPrecision)
                        },
                        Bid = new OrderBookPriceLevel
                        {
                            Price    = orderBook.Top.Bid.Price.Trim(Symbol.PricePrecision),
                            Quantity = orderBook.Top.Bid.Quantity.Trim(Symbol.QuantityPrecision)
                        }
                    };

                    var asks = new List <OrderBookPriceLevel>(
                        (from ask in orderBook.Asks
                         orderby ask.Price
                         select new OrderBookPriceLevel
                    {
                        Price = ask.Price.Trim(Symbol.PricePrecision),
                        Quantity = ask.Quantity.Trim(Symbol.QuantityPrecision)
                    }));

                    var bids = new List <OrderBookPriceLevel>(
                        (from bid in orderBook.Bids
                         orderby bid.Price
                         select new OrderBookPriceLevel
                    {
                        Price = bid.Price.Trim(Symbol.PricePrecision),
                        Quantity = bid.Quantity.Trim(Symbol.QuantityPrecision)
                    }));

                    OrderBook.Update(asks, bids);
                }
            }
        }
Example #2
0
        internal void UpdateOrderBook(Interface.OrderBook orderBook)
        {
            if (!Symbol.Name.Equals(orderBook.Symbol))
            {
                throw new Exception("Orderbook update for wrong symbol");
            }

            lock (orderBookLock)
            {
                Logger.Log($"Orders New Update {swOrderUpdate.Elapsed}", Category.Info, Priority.Low);

                var sw = new Stopwatch();
                sw.Start();
                Logger.Log($"Start OrderBookUpdate", Category.Info, Priority.Low);

                bool firstOrders = false;

                if (OrderBook == null)
                {
                    // First incoming order book create the local order book.
                    firstOrders = true;

                    OrderBook = new OrderBook
                    {
                        Symbol      = orderBook.Symbol,
                        BaseSymbol  = Symbol.BaseAsset.Symbol,
                        QuoteSymbol = Symbol.QuoteAsset.Symbol
                    };
                }
                else if (OrderBook.LastUpdateId >= orderBook.LastUpdateId)
                {
                    // If the incoming order book is older than the local one ignore it.
                    return;
                }

                OrderBook.LastUpdateId = orderBook.LastUpdateId;
                OrderBook.Top          = new OrderBookTop
                {
                    Ask = new OrderBookPriceLevel
                    {
                        Price    = orderBook.Top.Ask.Price.Trim(Symbol.PricePrecision),
                        Quantity = orderBook.Top.Ask.Quantity.Trim(Symbol.QuantityPrecision)
                    },
                    Bid = new OrderBookPriceLevel
                    {
                        Price    = orderBook.Top.Bid.Price.Trim(Symbol.PricePrecision),
                        Quantity = orderBook.Top.Bid.Quantity.Trim(Symbol.QuantityPrecision)
                    }
                };

                // Order by price: bids (DESC) and asks (ASC)
                var orderedAsks = orderBook.Asks.OrderBy(a => a.Price).ToList();
                var orderedBids = orderBook.Bids.OrderByDescending(b => b.Price).ToList();

                // The OrderBookCount is the greater of the OrderBookDisplayCount OrderBookChartDisplayCount.
                // Take the asks and bids for the OrderBookCount as new instances of type OrderBookPriceLevel
                // i.e. discard those that we are not interested in displaying on the screen.
                var asks = orderedAsks.Take(OrderBookCount).Select(ask => new OrderBookPriceLevel
                {
                    Price    = ask.Price.Trim(Symbol.PricePrecision),
                    Quantity = ask.Quantity.Trim(Symbol.QuantityPrecision)
                }).ToList();

                var bids = orderedBids.Take(OrderBookCount).Select(bid => new OrderBookPriceLevel
                {
                    Price    = bid.Price.Trim(Symbol.PricePrecision),
                    Quantity = bid.Quantity.Trim(Symbol.QuantityPrecision)
                }).ToList();

                // Take the top bids and asks for the order book bid and ask lists and order descending.
                var topAsks = asks.Take(OrderBookDisplayCount).Reverse().ToList();
                var topBids = bids.Take(OrderBookDisplayCount).ToList();

                // Take the bid and aks to display in the the order book chart.
                var chartAsks = asks.Take(OrderBookChartDisplayCount).ToList();
                var chartBids = bids.Take(OrderBookChartDisplayCount).ToList();

                // Create the aggregated bids and asks for the aggregated bid and ask chart.
                var aggregatedAsks = chartAsks.GetAggregatedList();
                var aggregatedBids = chartBids.GetAggregatedList();

                // Create new instances of the top bids and asks, reversing the asks
                OrderBook.TopAsks = topAsks;
                OrderBook.TopBids = topBids;

                if (firstOrders)
                {
                    // Create new instances of the chart bids and asks, reversing the bids.
                    OrderBook.ChartAsks           = new ChartValues <OrderBookPriceLevel>(chartAsks);
                    OrderBook.ChartBids           = new ChartValues <OrderBookPriceLevel>(chartBids.Reverse <OrderBookPriceLevel>().ToList());
                    OrderBook.ChartAggregatedAsks = new ChartValues <OrderBookPriceLevel>(aggregatedAsks);
                    OrderBook.ChartAggregatedBids = new ChartValues <OrderBookPriceLevel>(aggregatedBids.Reverse <OrderBookPriceLevel>().ToList());
                }
                else
                {
                    // Update the existing orderbook chart bids and asks, reversing the bids.
                    OrderBook.UpdateChartAsks(chartAsks);
                    OrderBook.UpdateChartBids(chartBids.Reverse <OrderBookPriceLevel>().ToList());
                    OrderBook.UpdateChartAggregateAsks(aggregatedAsks);
                    OrderBook.UpdateChartAggregateBids(aggregatedBids.Reverse <OrderBookPriceLevel>().ToList());
                }

                sw.Stop();
                Logger.Log($"End OrderBookUpdate {sw.Elapsed}", Category.Info, Priority.Low);
                swOrderUpdate.Restart();
            }
        }