Example #1
0
        public void CancelPartiallyFilled()
        {
            MatchingEngine engine = new MatchingEngine();

            engine.InitOrderBook();
            Order order1 = new Order(OrderSide.SELL, 1000, 10, OrderType.GFD, "order1");
            Order order2 = new Order(OrderSide.BUY, 1100, 20, OrderType.GFD, "order2");
            Order order3 = new Order(OrderSide.BUY, 1100, 20, OrderType.GFD, "order3");
            Order order4 = new Order(OrderSide.SELL, 900, 60, OrderType.GFD, "order4");

            engine.SendOrder(order1);
            engine.SendOrder(order2);
            engine.CancelOrder("order2");
            engine.SendOrder(order3);
            engine.MaxBid.Should().Be(1100);

            engine.SendOrder(order4);

            order1.Status.Should().Be(OrderStatus.FILLED);
            order2.Status.Should().Be(OrderStatus.CANCELED);

            engine.OrderEntries[1100].TotalQuantity.Should().Be(0);
            engine.OrderEntries[1100].Orders.Count.Should().Be(0);
            engine.MaxBid.Should().Be(engine.MinPrice);
            engine.MinAsk.Should().Be(900);

            engine.OrderEntries[900].TotalQuantity.Should().Be(40);
            order2.Status.Should().Be(OrderStatus.CANCELED);
            order2.FilledQuantity.Should().Be(10);
            order3.Status.Should().Be(OrderStatus.FILLED);
            order4.Status.Should().Be(OrderStatus.ON_MARKET);
            order4.FilledQuantity.Should().Be(20);
        }
Example #2
0
        public void CancelFilledOrderShouldFail()
        {
            MatchingEngine engine = new MatchingEngine();

            engine.InitOrderBook();
            Order order1 = new Order(OrderSide.BUY, 1000, 10, OrderType.GFD, "order1");
            Order order2 = new Order(OrderSide.SELL, 1000, 10, OrderType.GFD, "order2");
            engine.SendOrder(order1);
            engine.SendOrder(order2);
            engine.CancelOrder("order2");

            order1.Status.Should().Be(OrderStatus.FILLED);
            order2.Status.Should().Be(OrderStatus.FILLED);
            engine.OrderEntries[1000].TotalQuantity.Should().Be(0);
        }
Example #3
0
        private async Task InboundMessageProcessor()
        {
            try
            {
                while (await _inboundChannelReader.WaitToReadAsync())
                {
                    while (_inboundChannelReader.TryRead(out Message message))
                    {
                        try
                        {
                            switch (message.MessageType)
                            {
                            case MessageType.NewOrderRequest:
                                _matchingEngine.AddOrder((OrderWrapper)message.Object);
                                break;

                            case MessageType.CancelRequest:
                                _matchingEngine.CancelOrder(((CancelRequest)message.Object).OrderId);
                                break;

                            case MessageType.BookRequest:
                                var bytes = BookSerializer.Serialize(_matchingEngine.Book, ((BookRequest)message.Object).LevelCount, _matchingEngine.MarketPrice, _timeProvider.GetSecondsFromEpoch());
                                _outboundChannelWritter.TryWrite(bytes);
                                //TODO
                                break;

                            default:
                                //TODO
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                            //TODO
                            throw;
                        }
                    }
                }
                _outboundChannelWritter.Complete();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                //TODO
                throw;
            }
        }
Example #4
0
    static void Main(String[] args)
    {
        /* Enter your code here. Read input using Console.ReadLine. Print output using Console.WriteLine. Your class should be named Solution */
        var engine = new MatchingEngine();

        /*var argsList = new List<string>();
         *
         * while (true)
         * {
         * var arg = Console.ReadLine();
         *
         * if (arg == null)
         *    break;
         *
         * argsList.Add(arg);
         * }*/

        foreach (var str in args)
        {
            var order = ProcessInputLine(str);

            if (order == null)
            {
                engine.PrintStatus();
            }
            else if (order.TradeStatus == TradeStatus.Active)
            {
                engine.AddOrder(order);
                engine.Trade();
            }
            else if (order.TradeStatus == TradeStatus.Modify)
            {
                engine.ModifyOrder(order);
                engine.Trade();
            }
            else if (order.TradeStatus == TradeStatus.Cancelled)
            {
                engine.CancelOrder(order);
            }
            else if (order.OperationType == OperationType.None)
            {
                continue;
            }
        }
    }
Example #5
0
        public void SellOrderShouldNotMatchCanceledBuyOrder()
        {
            MatchingEngine engine = new MatchingEngine();

            engine.InitOrderBook();
            Order order1 = new Order(OrderSide.BUY, 1000, 10, OrderType.GFD, "order1");

            engine.SendOrder(order1);
            engine.CancelOrder("order1");

            engine.OrderDict.Keys.Should().Contain("order1");

            engine.OrderEntries[1000].TotalQuantity.Should().Be(0);
            engine.OrderEntries[1000].Orders.Count.Should().Be(1);
            engine.MaxBid.Should().Be(1);

            order1.Status.Should().Be(OrderStatus.CANCELED);
        }