Ejemplo n.º 1
0
        public static OrderBookViewModel ToOrderBookViewModel(this MarketDeep source)
        {
            var asksList = new List <OrderPriceAndQuantityViewModel>();
            var bidsList = new List <OrderPriceAndQuantityViewModel>();


            foreach (var order in source.Asks)
            {
                asksList.Add(new OrderPriceAndQuantityViewModel
                {
                    price    = order.Price,
                    quantity = order.Amount
                });
            }

            foreach (var order in source.Bids)
            {
                bidsList.Add(new OrderPriceAndQuantityViewModel
                {
                    price    = order.Price,
                    quantity = order.Amount
                });
            }

            return(new OrderBookViewModel
            {
                asks = asksList,
                bids = bidsList
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 市场深度
        /// </summary>
        /// <returns>返回系统支持的所有交易对的市场深度(委托挂单),其中 asks 是委卖单, bids 是委买单。</returns>
        public static async Task <Dictionary <string, MarketDeep> > OrderBooksAsync()
        {
            try
            {
                var requestUrl = QUERY_URL + ORDER_BOOKS_URL;
                var json       = await GetReqAsync(requestUrl);

                var dic = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(json);


                var result = new Dictionary <string, MarketDeep>();

                foreach (var item in dic)
                {
                    if (item.Value.result == "true")
                    {
                        var marketDeep = new MarketDeep();

                        var asks = item.Value.asks as JArray;
                        var bids = item.Value.bids as JArray;

                        foreach (var ask in asks.Values <JArray>())
                        {
                            var v = ask.ToObject <List <decimal> >();
                            marketDeep.Asks.Add(new Deep
                            {
                                Price  = v.ElementAt(0),
                                Amount = v.ElementAt(1)
                            });
                        }

                        foreach (var bid in bids)
                        {
                            var v = bid.ToObject <List <decimal> >();
                            marketDeep.Bids.Add(new Deep
                            {
                                Price  = v.ElementAt(0),
                                Amount = v.ElementAt(1)
                            });
                        }

                        result.Add(item.Key, marketDeep);
                    }
                }

                return(result);
            }
            catch (JsonSerializationException)
            {
                throw new BotException("1002");
            }
            catch (Exception ex)
            {
                throw new BotException("1000", ex.Message);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 单项市场深度
        /// </summary>
        /// <param name="currA">被兑换货币</param>
        /// <param name="currB">兑换货币</param>
        /// <returns></returns>
        public static async Task <MarketDeep> OrderBookAsync(string currA, string currB)
        {
            try
            {
                var requestUrl = $"{QUERY_URL}{ORDER_BOOK_URL}/{currA}_{currB}";
                var json       = await GetReqAsync(requestUrl);

                dynamic parse = JToken.Parse(json) as dynamic;

                var marketDeep = new MarketDeep();

                if (parse.result == "true")
                {
                    var asks = parse.asks as JArray;
                    var bids = parse.bids as JArray;

                    foreach (var ask in asks.Values <JArray>())
                    {
                        var v = ask.ToObject <List <decimal> >();
                        marketDeep.Asks.Add(new Deep
                        {
                            Price  = v.ElementAt(0),
                            Amount = v.ElementAt(1)
                        });
                    }

                    foreach (var bid in bids)
                    {
                        var v = bid.ToObject <List <decimal> >();
                        marketDeep.Bids.Add(new Deep
                        {
                            Price  = v.ElementAt(0),
                            Amount = v.ElementAt(1)
                        });
                    }
                }

                return(marketDeep);
            }
            catch (JsonSerializationException)
            {
                throw new BotException("1002");
            }
            catch (Exception ex)
            {
                throw new BotException("1000", ex.Message);
            }
        }