Example #1
0
 public IMessageIn Visit(OrderChange msg)
 {
     msg.OrderId  = (string)_data["order_id"];
     msg.Price    = (decimal?)_data["price"];
     msg.NewSize  = (decimal?)_data["new_size"];
     msg.OldSize  = (decimal?)_data["old_size"];
     msg.NewFunds = (decimal?)_data["new_funds"];
     msg.OldFunds = (decimal?)_data["old_funds"];
     return(msg);
 }
Example #2
0
            public object Visit(WebSocket.OrderChange msg)
            {
                Condition.Requires(msg, "msg").IsNotNull();
                Condition.Requires(msg.OrderId, "msg.OrderId").IsNotNullOrEmpty();

                if (!msg.Price.HasValue)
                {
                    return(null);                      // Market order.
                }
                Condition.Requires(msg.OldSize.HasValue, "msg.OldSize.HasValue");
                Condition.Requires(msg.NewSize.HasValue, "msg.NewSize.HasValue");

                decimal sizeDelta = msg.NewSize.Value - msg.OldSize.Value;

                if (sizeDelta == 0m)
                {
                    return(null);
                }

                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 (-sizeDelta > level.TotalSize)
                {
                    throw new ArgumentException("OrderChange with the delta size exceeding total level size: " + msg);
                }
                level.TotalSize += sizeDelta;
                _delta.Add(new PriceLevel()
                {
                    Price = msg.Price.Value, SizeDelta = sizeDelta
                });
                return(null);
            }
Example #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));
        }