Ejemplo n.º 1
0
        private void UpdateQuotaion(Quotation quotation, string jsonText)
        {
            JObject jObject = JObject.Parse(jsonText);

            decimal convertRate = quotation.Ticker.ConvertRate;

            PriceQuantityCollection buyOrders = GetOrders(jObject["buyOrder"].ToString(), convertRate);

            quotation.Ticker.BidPrice = buyOrders.First().OrginalPrice;
            quotation.BuyOrders       = buyOrders;

            PriceQuantityCollection sellOrders = GetOrders(jObject["sellOrder"].ToString(), convertRate);

            quotation.Ticker.AskPrice = sellOrders.First().OrginalPrice;
            quotation.SellOrders      = sellOrders;

            string  historyJson = jObject["trade"].ToString();
            History history     = GetHistory(historyJson, quotation.Ticker);

            quotation.History = history;
            quotation.Ticker.LastTradePrice = history.First().PriceQuantity.OrginalPrice;
            quotation.Ticker.HighPrice      = history.Max(a => a.PriceQuantity.OrginalPrice);
            quotation.Ticker.LowPrice       = history.Min(a => a.PriceQuantity.OrginalPrice);
            quotation.History = history;
        }
Ejemplo n.º 2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(string.Empty);
            }
            if (reader.TokenType == JsonToken.String)
            {
                return(serializer.Deserialize(reader, objectType));
            }

            var token = JArray.FromObject(existingValue);

            var priceQuantity = new PriceQuantityCollection();

            foreach (var child in token)
            {
                var array    = (JArray)child;
                var price    = Convert.ToDouble(array[0].ToString());
                var quantity = Convert.ToDouble(array[1].ToString());
                priceQuantity.Set(price, quantity);
            }

            return(priceQuantity);
        }
Ejemplo n.º 3
0
        private PriceQuantityCollection GetOrders(string ordersJson, decimal convertRate)
        {
            JArray asksJArray = JArray.Parse(ordersJson);
            PriceQuantityCollection orders = new PriceQuantityCollection();

            foreach (var item in asksJArray)
            {
                List <decimal>    itemResult = JsonConvert.DeserializeObject <List <decimal> >(item.ToString());
                PriceQuantityItem order      = new PriceQuantityItem();
                order.ConvertRate = convertRate;
                order.Price       = itemResult[0];
                order.Quantity    = itemResult[1];
                orders.Add(order);
            }
            return(orders);
        }
Ejemplo n.º 4
0
        private void UpdateQuotation(string json, ref Quotation quotation)
        {
            JObject jObject = JObject.Parse(json);

            decimal convertRate = quotation.Ticker.ConvertRate;

            string asks = jObject["asks"].ToString();
            PriceQuantityCollection sellOrders = GetOrders(asks, convertRate);

            quotation.SellOrders = sellOrders;

            string bids = jObject["bids"].ToString();
            PriceQuantityCollection buyOrders = GetOrders(bids, convertRate);

            quotation.BuyOrders = buyOrders;
        }
Ejemplo n.º 5
0
        private PriceQuantityCollection GetOrders(string ordersJson, decimal convertRate)
        {
            JArray asksJArray = JArray.Parse(ordersJson);
            PriceQuantityCollection orders = new PriceQuantityCollection();

            foreach (var item in asksJArray)
            {
                var def    = new { count = 0, rate = 0m, vol = 0m };
                var result = JsonConvert.DeserializeAnonymousType(item.ToString(), def);
                PriceQuantityItem order = new PriceQuantityItem();
                order.ConvertRate = convertRate;
                order.Price       = result.rate;
                order.Quantity    = result.vol;
                orders.Add(order);
            }
            return(orders);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 获取市场深度。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// 示例:(同样价格可能出现多条数据)
        /// {
        ///     "asks":
        ///     [
        ///         ["199.888","0.32405322"],
        ///         ["199.8888","1.88"],
        ///         ["200.0","0.01"],
        ///         ...
        ///         ["1000.4039","0.0100545"]
        ///     ],
        ///     "bids":
        ///     [
        ///         ["191.49200999","0.01145931"],
        ///         ["191.48944319","0.2"],
        ///         ["191.48870982","0.07530287"],
        ///         ...
        ///         ["0.8011","0.0078"]
        ///     ]
        /// }
        /// </remarks>
        private void DownloadQuotationCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            Quotation    quotation    = e.UserState as Quotation;
            CurrencyPair currencyPair = quotation.Ticker.CurrencyPair;

            if (e.Error != null)
            {
                EventSourceLogger.Logger.DownloadDataAsyncError(DataTypeDict["Quotation"], Exchange.Name, currencyPair.Code, e.Error.Message);
                return;
            }
            EventSourceLogger.Logger.UpdateDataSuccess(DataTypeDict["Quotation"], Exchange.Name, currencyPair.Code);
            string  json    = e.Result;
            JObject jObject = JObject.Parse(json);

            decimal convertRate = quotation.Ticker.ConvertRate;

            string asks = jObject["asks"].ToString();

            if (asks == "{}")
            {
                return;
            }
            PriceQuantityCollection sellOrders = GetOrders(asks, convertRate);

            quotation.SellOrders = sellOrders;

            string bids = jObject["bids"].ToString();

            if (bids == "{}")
            {
                return;
            }
            PriceQuantityCollection buyOrders = GetOrders(bids, convertRate);

            quotation.BuyOrders = buyOrders;

            var ticker        = quotation.Ticker;
            var tickerUpdater = apiManager.GetApi <ITickerApi>(Exchange.AbbrName);

            tickerUpdater.UpdateTicker(ref ticker);

            var refHistory     = quotation.History;
            var historyUpdater = apiManager.GetApi <IHistoryApi>(Exchange.AbbrName);

            historyUpdater.UpdateHistory(ref refHistory);
        }
Ejemplo n.º 7
0
        private void UpdateQuotation(Quotation quotation, string json)
        {
            JObject jObject     = JObject.Parse(json);
            decimal convertRate = quotation.Ticker.ConvertRate;
            string  bids        = jObject["bids"].ToString();

            quotation.BuyOrders = GetOrders(bids, convertRate);

            string asks = jObject["asks"].ToString();
            PriceQuantityCollection sellOrders = GetOrders(asks, convertRate);
            var reverse       = sellOrders.Reverse();
            var reverseOrders = new PriceQuantityCollection();

            foreach (PriceQuantityItem order in reverse)
            {
                reverseOrders.Add(order);
            }
            quotation.SellOrders = reverseOrders;
        }
Ejemplo n.º 8
0
 //
 /// <summary>
 /// 从Api的Json数据获取订单。
 /// </summary>
 /// <param name="ordersJson"></param>
 /// <returns></returns>
 /// <example>
 /// {"price":"0.00000034","quantity":"0.00000000","total":"23.04005139"},
 /// </example>
 private PriceQuantityCollection GetOrders(string ordersJson, decimal convertRate)
 {
     //当心买盘为0时orderJson为空!
     try
     {
         JArray asksJArray = JArray.Parse(ordersJson);
         PriceQuantityCollection orders = new PriceQuantityCollection();
         foreach (var item in asksJArray)
         {
             var itemDef             = new { price = 0m, quantity = 0m };
             var itemResult          = JsonConvert.DeserializeAnonymousType(item.ToString(), itemDef);
             PriceQuantityItem order = new PriceQuantityItem();
             order.ConvertRate = convertRate;
             order.Price       = itemResult.price;
             order.Quantity    = itemResult.quantity;
             orders.Add(order);
         }
         return(orders);
     }
     catch
     {
         return(new PriceQuantityCollection());
     }
 }