internal async void UpdateOrderBook(Interface.Model.OrderBook exchangeOrderBook) { await orderBookSemaphoreSlim.WaitAsync(symbolCancellationTokenSource.Token); try { if (OrderBook == null) { OrderBook = await orderBookHelper.CreateLocalOrderBook(Symbol, exchangeOrderBook, OrderBookDisplayCount, OrderBookChartDisplayCount); if (IsLoadingOrderBook) { IsLoadingOrderBook = false; } } else if (OrderBook.LastUpdateId >= exchangeOrderBook.LastUpdateId) { // If the incoming order book is older than the local one ignore it. return; } else { orderBookHelper.UpdateLocalOrderBook(OrderBook, exchangeOrderBook, symbol.PricePrecision, symbol.QuantityPrecision, OrderBookDisplayCount, OrderBookChartDisplayCount); } } finally { orderBookSemaphoreSlim.Release(); } }
public void UpdateLocalOrderBook(OrderBook orderBook, Interface.Model.OrderBook updateOrderBook, int pricePrecision, int quantityPrecision, int listDisplayCount, int chartDisplayCount) { orderBook.LastUpdateId = updateOrderBook.LastUpdateId; List <OrderBookPriceLevel> topAsks; List <OrderBookPriceLevel> topBids; List <OrderBookPriceLevel> chartAsks; List <OrderBookPriceLevel> chartBids; List <OrderBookPriceLevel> aggregatedAsks; List <OrderBookPriceLevel> aggregatedBids; GetBidsAndAsks(updateOrderBook, pricePrecision, quantityPrecision, listDisplayCount, chartDisplayCount, out topAsks, out topBids, out chartAsks, out chartBids, out aggregatedAsks, out aggregatedBids); // Create new instances of the top // bids and asks, reversing the asks orderBook.TopAsks = topAsks; orderBook.TopBids = topBids; // Update the existing orderbook chart // bids and asks, reversing the bids. UpdateChartValues(orderBook.ChartAsks, chartAsks); UpdateChartValues(orderBook.ChartBids, chartBids); UpdateChartValues(orderBook.ChartAggregatedAsks, aggregatedAsks); UpdateChartValues(orderBook.ChartAggregatedBids, aggregatedBids); }
private Interface.Model.OrderBook NewOrderBook(OrderBook ob) { var orderBook = new Interface.Model.OrderBook { Symbol = ob.Symbol, LastUpdateId = ob.LastUpdateId, Top = new Interface.Model.OrderBookTop { Symbol = ob.Top.Symbol, Ask = new Interface.Model.OrderBookPriceLevel { Price = ob.Top.Ask.Price, Quantity = ob.Top.Ask.Quantity }, Bid = new Interface.Model.OrderBookPriceLevel { Price = ob.Top.Bid.Price, Quantity = ob.Top.Bid.Quantity } } }; orderBook.Asks = (from ask in ob.Asks select new Interface.Model.OrderBookPriceLevel { Price = ask.Price, Quantity = ask.Quantity }).ToList(); orderBook.Bids = (from bid in ob.Bids select new Interface.Model.OrderBookPriceLevel { Price = bid.Price, Quantity = bid.Quantity }).ToList(); return(orderBook); }
public void UpdateLocalOrderBook(OrderBook orderBook, Interface.Model.OrderBook updateOrderBook, int pricePrecision, int quantityPrecision, int listDisplayCount, int chartDisplayCount) { long latestSquence = orderBook.LastUpdateId; if (updateOrderBook.Asks.Any(a => a.Price > 0) || updateOrderBook.Bids.Any(b => b.Price > 0)) { var x = string.Empty; } bool isUpdated = false; orderBook.Asks = ReplayPriceLevels(orderBook.Asks, updateOrderBook.Asks, orderBook.LastUpdateId, ref latestSquence, ref isUpdated); orderBook.Bids = ReplayPriceLevels(orderBook.Bids, updateOrderBook.Bids, orderBook.LastUpdateId, ref latestSquence, ref isUpdated); orderBook.LastUpdateId = latestSquence; var orderBookCount = chartDisplayCount > listDisplayCount ? chartDisplayCount : listDisplayCount; var asks = orderBook.Asks.Select(ask => new OrderBookPriceLevel { Price = ask.Price.Trim(pricePrecision), Quantity = ask.Quantity.Trim(quantityPrecision) }).ToList(); var bids = orderBook.Bids.Select(bid => new OrderBookPriceLevel { Price = bid.Price.Trim(pricePrecision), Quantity = bid.Quantity.Trim(quantityPrecision) }).ToList(); // Take the top bids and asks for the order book bid and ask lists and order descending. orderBook.TopAsks = asks.Take(listDisplayCount).OrderByDescending(a => a.Price).ToList(); orderBook.TopBids = bids.OrderByDescending(b => b.Price).Take(listDisplayCount).ToList(); var skipExcessBids = 0; if (bids.Count > chartDisplayCount) { skipExcessBids = bids.Count - chartDisplayCount; } // Take the bid and aks to display in the the order book chart. var chartAsks = asks.Take(chartDisplayCount).ToList(); var chartBids = bids.Skip(skipExcessBids).ToList(); // Create the aggregated bids and asks for the aggregated bid and ask chart. var aggregatedAsks = GetAggregatedAsks(chartAsks); var aggregatedBids = GetAggregatedBids(chartBids); UpdateChartValues(orderBook.ChartAsks, chartAsks); UpdateChartValues(orderBook.ChartBids, chartBids); UpdateChartValues(orderBook.ChartAggregatedAsks, aggregatedAsks); UpdateChartValues(orderBook.ChartAggregatedBids, aggregatedBids); }
private Interface.Model.OrderBook NewOrderBook(OrderBook ob) { var orderBook = new Interface.Model.OrderBook { Symbol = ob.Symbol, Exchange = Exchange.Binance, LastUpdateId = ob.LastUpdateId }; orderBook.Asks = (from ask in ob.Asks select new Interface.Model.OrderBookPriceLevel { Price = ask.Price, Quantity = ask.Quantity }).ToList(); orderBook.Bids = (from bid in ob.Bids select new Interface.Model.OrderBookPriceLevel { Price = bid.Price, Quantity = bid.Quantity }).ToList(); return(orderBook); }
private void GetBidsAndAsks(Interface.Model.OrderBook orderBook, int pricePrecision, int quantityPrecision, int listDisplayCount, int chartDisplayCount, out List <OrderBookPriceLevel> topAsks, out List <OrderBookPriceLevel> topBids, out List <OrderBookPriceLevel> chartAsks, out List <OrderBookPriceLevel> chartBids, out List <OrderBookPriceLevel> aggregatedAsks, out List <OrderBookPriceLevel> aggregatedBids) { var orderBookCount = chartDisplayCount > listDisplayCount ? chartDisplayCount : listDisplayCount; // 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(pricePrecision), Quantity = ask.Quantity.Trim(quantityPrecision) }).ToList(); var bids = orderedBids.Take(orderBookCount).Select(bid => new OrderBookPriceLevel { Price = bid.Price.Trim(pricePrecision), Quantity = bid.Quantity.Trim(quantityPrecision) }).ToList(); // Take the top bids and asks for the order book bid and ask lists and order descending. topAsks = asks.Take(listDisplayCount).Reverse().ToList(); topBids = bids.Take(listDisplayCount).ToList(); // Take the bid and aks to display in the the order book chart. chartAsks = asks.Take(chartDisplayCount).ToList(); var tempbids = bids.Take(chartDisplayCount).ToList(); chartBids = tempbids.Reverse <OrderBookPriceLevel>().ToList(); // Create the aggregated bids and asks for the aggregated bid and ask chart. aggregatedAsks = GetAggregatedList(asks); var aggBids = GetAggregatedList(bids); aggregatedBids = aggBids.Reverse <OrderBookPriceLevel>().ToList(); }
public Task <OrderBook> CreateLocalOrderBook(Symbol symbol, Interface.Model.OrderBook orderBook, int listDisplayCount, int chartDisplayCount) { var tcs = new TaskCompletionSource <OrderBook>(); try { List <OrderBookPriceLevel> topAsks; List <OrderBookPriceLevel> topBids; List <OrderBookPriceLevel> chartAsks; List <OrderBookPriceLevel> chartBids; List <OrderBookPriceLevel> aggregatedAsks; List <OrderBookPriceLevel> aggregatedBids; GetBidsAndAsks(orderBook, symbol.PricePrecision, symbol.QuantityPrecision, listDisplayCount, chartDisplayCount, out topAsks, out topBids, out chartAsks, out chartBids, out aggregatedAsks, out aggregatedBids); var newOrderBook = new OrderBook { LastUpdateId = orderBook.LastUpdateId, Symbol = orderBook.Symbol, BaseSymbol = symbol.BaseAsset.Symbol, QuoteSymbol = symbol.QuoteAsset.Symbol, TopAsks = topAsks, TopBids = topBids, ChartAsks = new ChartValues <OrderBookPriceLevel>(chartAsks), ChartBids = new ChartValues <OrderBookPriceLevel>(chartBids), ChartAggregatedAsks = new ChartValues <OrderBookPriceLevel>(aggregatedAsks), ChartAggregatedBids = new ChartValues <OrderBookPriceLevel>(aggregatedBids) }; tcs.SetResult(newOrderBook); } catch (Exception ex) { tcs.SetException(ex); } return(tcs.Task); }
public async Task <OrderBook> CreateLocalOrderBook(Symbol symbol, Interface.Model.OrderBook orderBook, int listDisplayCount, int chartDisplayCount) { var cancellationTokenSource = new CancellationTokenSource(); var orderBookCount = chartDisplayCount > listDisplayCount ? chartDisplayCount : listDisplayCount; var limit = orderBookCount < 21 ? 20 : 100; var snapShot = await kucoinExchangeApi.GetOrderBookAsync(symbol.ExchangeSymbol, limit, cancellationTokenSource.Token); // Order by price: bids (ASC) and asks (ASC) // Discard those that we are not interested in displaying on the screen. var snapShotAsks = new List <Interface.Model.OrderBookPriceLevel>(snapShot.Asks.Take(orderBookCount).OrderBy(a => a.Price).ToList()); var snapShotBids = new List <Interface.Model.OrderBookPriceLevel>(snapShot.Bids.Take(orderBookCount).OrderBy(b => b.Price).ToList()); long latestSquence = snapShot.LastUpdateId; bool isUpdated = false; var replayedAsks = ReplayPriceLevels(snapShotAsks, orderBook.Asks, snapShot.LastUpdateId, ref latestSquence, ref isUpdated); var replayedBids = ReplayPriceLevels(snapShotBids, orderBook.Bids, snapShot.LastUpdateId, ref latestSquence, ref isUpdated); var pricePrecision = symbol.PricePrecision; var quantityPrecision = symbol.QuantityPrecision; var asks = replayedAsks.Select(ask => new OrderBookPriceLevel { Price = ask.Price.Trim(pricePrecision), Quantity = ask.Quantity.Trim(quantityPrecision) }).ToList(); var bids = replayedBids.Select(bid => new OrderBookPriceLevel { Price = bid.Price.Trim(pricePrecision), Quantity = bid.Quantity.Trim(quantityPrecision) }).ToList(); // Take the top bids and asks for the order book bid and ask lists and order descending. var topAsks = asks.Take(listDisplayCount).OrderByDescending(a => a.Price).ToList(); var topBids = bids.OrderByDescending(b => b.Price).Take(listDisplayCount).ToList(); var skipExcessBids = 0; if (bids.Count > chartDisplayCount) { skipExcessBids = bids.Count - chartDisplayCount; } // Take the bid and aks to display in the the order book chart. var chartAsks = asks.Take(chartDisplayCount).ToList(); var chartBids = bids.Skip(skipExcessBids).ToList(); // Create the aggregated bids and asks for the aggregated bid and ask chart. var aggregatedAsks = GetAggregatedAsks(chartAsks); var aggregatedBids = GetAggregatedBids(chartBids); return(new OrderBook { LastUpdateId = latestSquence, Symbol = orderBook.Symbol, BaseSymbol = symbol.BaseAsset.Symbol, QuoteSymbol = symbol.QuoteAsset.Symbol, Asks = replayedAsks, Bids = replayedBids, TopAsks = topAsks, TopBids = topBids, ChartAsks = new ChartValues <OrderBookPriceLevel>(chartAsks), ChartBids = new ChartValues <OrderBookPriceLevel>(chartBids), ChartAggregatedAsks = new ChartValues <OrderBookPriceLevel>(aggregatedAsks), ChartAggregatedBids = new ChartValues <OrderBookPriceLevel>(aggregatedBids) }); }