コード例 #1
0
        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)
            });
        }
コード例 #2
0
ファイル: ExchangeService.cs プロジェクト: lulzzz/tradeview
        public async Task <OrderBook> GetOrderBookAsync(string symbol, int limit, CancellationToken cancellationToken)
        {
            var orderBook = await exchangeApi.GetOrderBookAsync(symbol, limit, cancellationToken).ConfigureAwait(false);

            return(orderBook);
        }
コード例 #3
0
        public async Task <OrderBook> CreateLocalOrderBook(Symbol symbol, Core.Model.OrderBook orderBook, int listDisplayCount, int chartDisplayCount)
        {
            if (symbol == null)
            {
                throw new ArgumentNullException(nameof(symbol));
            }

            if (orderBook == null)
            {
                throw new ArgumentNullException(nameof(orderBook));
            }

            Core.Model.OrderBook snapShot;

            using (var cancellationTokenSource = new CancellationTokenSource())
            {
                snapShot = await kucoinExchangeApi.GetOrderBookAsync(symbol.ExchangeSymbol, 100, cancellationTokenSource.Token).ConfigureAwait(false);
            }

            // Order by price: bids (ASC) and asks (ASC)
            // Discard those that we are not interested in displaying on the screen.
            var snapShotAsks = new List <Core.Model.OrderBookPriceLevel>(snapShot.Asks.OrderBy(a => a.Price).ToList());
            var snapShotBids = new List <Core.Model.OrderBookPriceLevel>(snapShot.Bids.OrderBy(b => b.Price).ToList());

            long latestSquence = snapShot.LastUpdateId;

            ReplayPriceLevels(snapShotAsks, orderBook.Asks, snapShot.LastUpdateId, ref latestSquence);
            ReplayPriceLevels(snapShotBids, orderBook.Bids, snapShot.LastUpdateId, ref latestSquence);

            var pricePrecision    = symbol.PricePrecision;
            var quantityPrecision = symbol.QuantityPrecision;

            var asks = snapShotAsks.Select(ask => new OrderBookPriceLevel
            {
                Price    = ask.Price.Trim(pricePrecision),
                Quantity = ask.Quantity.Trim(quantityPrecision)
            }).ToList();

            var bids = snapShotBids.Select(bid => new OrderBookPriceLevel
            {
                Price    = bid.Price.Trim(pricePrecision),
                Quantity = bid.Quantity.Trim(quantityPrecision)
            }).ToList();

            var topAsk = asks.First();
            var topBid = bids.First();

            decimal bidAskSpread;

            if (topAsk.Price != 0)
            {
                bidAskSpread = Math.Round(((topAsk.Price - topBid.Price) / topAsk.Price) * 100, 2, MidpointRounding.AwayFromZero);
            }
            else
            {
                bidAskSpread = 0.00m;
            }

            // 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);

            var newOrderBook = new OrderBook
            {
                LastUpdateId = latestSquence,
                Symbol       = orderBook.Symbol,
                BaseSymbol   = symbol.BaseAsset.Symbol,
                QuoteSymbol  = symbol.QuoteAsset.Symbol,
                BidAskSpread = bidAskSpread
            };

            newOrderBook.Asks.AddRange(snapShotAsks);
            newOrderBook.Bids.AddRange(snapShotBids);
            newOrderBook.TopAsks.AddRange(topAsks);
            newOrderBook.TopBids.AddRange(topBids);
            newOrderBook.ChartAsks.AddRange(new ChartValues <OrderBookPriceLevel>(chartAsks));
            newOrderBook.ChartBids.AddRange(new ChartValues <OrderBookPriceLevel>(chartBids));
            newOrderBook.ChartAggregatedAsks.AddRange(new ChartValues <OrderBookPriceLevel>(aggregatedAsks));
            newOrderBook.ChartAggregatedBids.AddRange(new ChartValues <OrderBookPriceLevel>(aggregatedBids));

            return(newOrderBook);
        }