Esempio n. 1
0
            public object Visit(WebSocket.OrderDone msg)
            {
                Condition.Requires(msg, "msg").IsNotNull();
                Condition.Requires(msg.OrderId, "msg.OrderId").IsNotNullOrEmpty();

                if (!msg.Price.HasValue)
                {
                    return(null);                      // Market order.
                }
                Level level;

                if (!_book.TryGetValue(msg.Price.Value, out level))
                {
                    return(null);                                                 // Not an open order.
                }
                Condition.Requires(level.TotalSize).IsGreaterOrEqual(0m);
                Condition.Requires(level.OrderIds).IsNotEmpty();
                if (!level.OrderIds.Contains(msg.OrderId))
                {
                    return(null);                                        // Not an open order.
                }
                if (level.OrderIds.Count == 1)
                {
                    if (!msg.RemainingSize.HasValue)
                    {
                        throw new ArgumentException("Price level with no orders and non-zero total size: " + msg);
                    }
                    Condition.Requires(msg.RemainingSize.Value).IsEqualTo(level.TotalSize);
                    if (msg.RemainingSize.Value > 0)
                    {
                        _delta.Add(new PriceLevel()
                        {
                            Price = msg.Price.Value, SizeDelta = -msg.RemainingSize.Value
                        });
                    }
                    Condition.Requires(_book.Remove(msg.Price.Value));  // Can't fail.
                    return(null);
                }

                if (msg.RemainingSize.HasValue)
                {
                    Condition.Requires(msg.RemainingSize.Value, "msg.RemainingSize.Value").IsGreaterOrEqual(0m);
                    if (msg.RemainingSize.Value > level.TotalSize)
                    {
                        throw new ArgumentException("OrderDone with the remaining size exceeding total level size: " + msg);
                    }
                    if (msg.RemainingSize.Value > 0)
                    {
                        level.TotalSize -= msg.RemainingSize.Value;
                        _delta.Add(new PriceLevel()
                        {
                            Price = msg.Price.Value, SizeDelta = -msg.RemainingSize.Value
                        });
                    }
                }
                Condition.Requires(level.OrderIds.Remove(msg.OrderId));  // Can't fail because of the `if` above.
                return(null);
            }
Esempio n. 2
0
 public IMessageIn Visit(OrderDone msg)
 {
     msg.OrderId       = (string)_data["order_id"];
     msg.Price         = (decimal?)_data["price"];
     msg.RemainingSize = (decimal?)_data["remaining_size"];
     msg.Reason        = ParseDoneReason((string)_data["reason"]);
     msg.OrderType     = ParseOrderType((string)_data["order_type"]);
     return(msg);
 }
Esempio n. 3
0
        public static IMessageIn Parse(string serialized)
        {
            var data = Json.ParseObject(serialized);

            Condition.Requires(data, "data").IsNotNull();
            string type = (string)data["type"];

            Condition.Requires(type, "type").IsNotNull();
            IMessageIn res = null;

            switch (type)
            {
            case "received":
                res = new OrderReceived();
                break;

            case "open":
                res = new OrderOpen();
                break;

            case "done":
                res = new OrderDone();
                break;

            case "match":
                res = new OrderMatch();
                break;

            case "change":
                res = new OrderChange();
                break;

            default:
                throw new ArgumentException("Unexpected message type: " + type);
            }
            var parser = new MessageParser(data);

            parser.ParseCommon(res);
            return(res.Visit(parser));
        }