private async Task ProcessOrderBookAsync(OrderBookMessage orderBookMessage)
        {
            var validationErrors = ValidateOrderBook(orderBookMessage);

            if (validationErrors.Any())
            {
                var message = string.Join("\r\n", validationErrors);
                var context = new
                {
                    orderBookMessage.AssetPair,
                    orderBookMessage.IsBuy,
                    orderBookMessage.Timestamp
                };

                _log.Warning(nameof(ProcessOrderBookAsync), message, context: context.ToJson());

                return;
            }

            // It is too frequent case to log it
            var prices = orderBookMessage.Prices.Where(p => p.Price > 0).ToArray();

            if (!prices.Any())
            {
                return;
            }

            await _quotesManager.ProcessOrderBookAsync(
                orderBookMessage.AssetPair,
                orderBookMessage.IsBuy,
                orderBookMessage.Timestamp,
                prices.Select(vp => vp.Price));
        }
Exemplo n.º 2
0
        public override BookInfo GetOrderBook(string pair, string type = "both")
        {
            string[] p = pair.Split("-".ToCharArray());
            string   a = p[1] + "_" + p[0];

            string[]         args = { a, type };
            OrderBookMessage mt   = new OrderBookMessage(Constants.GetOrderBookUrl, args);

            mt.Initialize();
            return(mt.Data);
        }
        private static ICollection <string> ValidateOrderBook(OrderBookMessage orderBookMessage)
        {
            var errors = new List <string>();

            if (orderBookMessage == null)
            {
                errors.Add("Argument 'Order' is null.");
            }
            else
            {
                if (string.IsNullOrWhiteSpace(orderBookMessage.AssetPair))
                {
                    errors.Add("'AssetPair' is empty");
                }
                if ((orderBookMessage.Timestamp == DateTime.MinValue || orderBookMessage.Timestamp == DateTime.MaxValue))
                {
                    errors.Add($"Invalid 'Timestamp' range: '{ (object)orderBookMessage.Timestamp}'");
                }
                if (orderBookMessage.Timestamp.Kind != DateTimeKind.Utc)
                {
                    errors.Add($"Invalid 'Timestamp' Kind (UTC is required): '{ (object)orderBookMessage.Timestamp}'");
                }
                if (orderBookMessage.Prices == null)
                {
                    errors.Add("Invalid 'Prices': null");
                }
                else if (orderBookMessage.Prices.All(p => p.Price < 0) && orderBookMessage.Prices.Any())
                {
                    var prices = orderBookMessage.Prices.Limit(10).Select(p => p.Price.ToString(CultureInfo.InvariantCulture));

                    errors.Add($"All prices is negative. Top 10: [{string.Join(", ", prices)}]");
                }
            }

            return(errors);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <see cref="Message"/>
        /// <param name="message">Source message.</param>
        public OrderBookData(Message message)
        {
            Symbol = SymbolsContainer.GetSymbol(message.MarketId);
            OrderBookMessage orderBookMessage = message.OrderBookMsg.Clone();

            MarketId       = message.MarketId;
            MarkPrice      = DWConverter.FromProtoDecimal(orderBookMessage.MarkPrice);
            LastTradePrice = DWConverter.FromProtoDecimal(orderBookMessage.LastTradePrice);
            Asks           = new Dictionary <decimal, decimal>();
            Bids           = new Dictionary <decimal, decimal>();
            foreach (OrderBookEntryMessage ask in orderBookMessage.Asks)
            {
                decimal price    = DWConverter.FromProtoDecimal(ask.Price);
                decimal quantity = DWConverter.FromProtoDecimal(ask.Quantity);
                Asks.Add(price, quantity);
            }
            foreach (OrderBookEntryMessage bid in orderBookMessage.Bids)
            {
                decimal price    = DWConverter.FromProtoDecimal(bid.Price);
                decimal quantity = DWConverter.FromProtoDecimal(bid.Quantity);
                Bids.Add(price, quantity);
            }
            LastTradeQuantity = DWConverter.FromProtoDecimal(orderBookMessage.LastTradeQuantity);
        }