Exemplo n.º 1
0
        public void BookShouldSetBestBidOnHigherBuyOrders()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock.Object, marketOrderMatchingAlgorithmMock.Object, orderBookBestBidAsk);

            Assert.IsNull(orderBookBestBidAsk.BestBidPrice);

            var buyOrder1 = new LimitOrder("ABC", 10, 90.1d, WayEnum.Buy, 1);

            book.AddLimitOrder(buyOrder1);

            Assert.AreEqual(90.1d, orderBookBestBidAsk.BestBidPrice);

            var buyOrder2 = new LimitOrder("ABC", 10, 90.2d, WayEnum.Buy, 1);

            book.AddLimitOrder(buyOrder2);

            Assert.AreEqual(90.2d, orderBookBestBidAsk.BestBidPrice);

            var buyOrder3 = new LimitOrder("ABC", 10, 80.0d, WayEnum.Buy, 1);

            book.AddLimitOrder(buyOrder3);

            Assert.AreEqual(90.2d, orderBookBestBidAsk.BestBidPrice);
        }
Exemplo n.º 2
0
        public void BookShouldSetBestAskOnLowerSellOrders()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock.Object, marketOrderMatchingAlgorithmMock.Object, orderBookBestBidAsk);

            Assert.IsNull(orderBookBestBidAsk.BestAskPrice);

            var order1 = new LimitOrder("ABC", 10, 90.1d, WayEnum.Sell, 1);

            book.AddLimitOrder(order1);

            Assert.AreEqual(90.1d, orderBookBestBidAsk.BestAskPrice);

            var order2 = new LimitOrder("ABC", 10, 90.0d, WayEnum.Sell, 1);

            book.AddLimitOrder(order2);

            Assert.AreEqual(90.0d, orderBookBestBidAsk.BestAskPrice);

            var order3 = new LimitOrder("ABC", 10, 99.0d, WayEnum.Sell, 1);

            book.AddLimitOrder(order3);

            Assert.AreEqual(90.0d, orderBookBestBidAsk.BestAskPrice);
        }
Exemplo n.º 3
0
        public void ShouldNotMatchOrderWhenMatchingIsSuspendedButMatchWhenSuspensionsIsCancelled()
        {
            var executions = new List <INewExecution>();

            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var matchAlgo           = new LimitOrderMatchingAlgorithm(new DateService());

            matchAlgo.AddExecutionsHandler(executions.Add);

            var book = new OrderBook("ABC", matchAlgo, marketOrderMatchingAlgorithmMock.Object, orderBookBestBidAsk);

            var sellOrder1 = new LimitOrder("ABC", 10, 90, WayEnum.Sell, 9);
            var buyOrder1  = new LimitOrder("ABC", 10, 80, WayEnum.Buy, 9);

            sellOrder1.RegisterModifyNotificationHandler(book.HandleLimitOrderModify);
            buyOrder1.RegisterModifyNotificationHandler(book.HandleLimitOrderModify);

            book.AddLimitOrder(sellOrder1);
            book.AddLimitOrder(buyOrder1);

            Assert.AreEqual(0, executions.Count);
            book.SetSuspendLimitOrderMatchingStatus(true);

            sellOrder1.Modify(10, 70);
            Assert.AreEqual(0, executions.Count);

            book.SetSuspendLimitOrderMatchingStatus(true);

            book.TryMatchLimitOrder(sellOrder1);
            book.TryMatchLimitOrder(buyOrder1);

            Assert.AreEqual(1, executions.Count);
        }
Exemplo n.º 4
0
        public void ShouldNotMatchSellStopLimitOrderLowerThanMarket()
        {
            var executions = new List <INewExecution>();

            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var matchAlgo           = new LimitOrderMatchingAlgorithm(new DateService());

            matchAlgo.AddExecutionsHandler(executions.Add);

            var book = new OrderBook("ABC", matchAlgo, marketOrderMatchingAlgorithmMock.Object, orderBookBestBidAsk);

            var sellOrder1 = new LimitOrder("ABC", 10, 1161.8d, WayEnum.Sell, 9);

            book.AddLimitOrder(sellOrder1);

            var buyOrder1 = new LimitOrder("ABC", 10, 1161.7d, WayEnum.Buy, 9);

            book.AddLimitOrder(buyOrder1);

            var trigger = new BestPriceTrigger("ABC", 1160, WayEnum.Sell);

            trigger.SetTriggerAction(() =>
            {
                Console.WriteLine("Boom!");
                int i = 0;
            });

            var sellStopLimit = new StopLimitOrder("ABC", 1, 1160, 1160, WayEnum.Sell, 5, trigger);

            book.AddStopLimitOrder(sellStopLimit);

            Assert.AreEqual(0, executions.Count);
        }
Exemplo n.º 5
0
        public void ShouldCreateNewOrderBookItMatchItAndRemoveIt()
        {
            var repo       = new OrderRepository();
            var limitAlgo  = new LimitOrderMatchingAlgorithm(new DateService());
            var marketAlgo = new MarketOrderMatchingAlgorithm(new DateService());
            var level1     = new OrderBookBestBidAsk("ABC");
            var book       = new OrderBook("ABC", limitAlgo, marketAlgo, level1);

            var sellOrder1 = repo.NewLimitOrder("ABC", 9, 88.2d, 100, WayEnum.Sell);

            book.AddLimitOrder(sellOrder1);

            var buyOrder1 = repo.NewLimitOrder("ABC", 9, 88.0d, 50, WayEnum.Buy);

            book.AddLimitOrder(buyOrder1);

            Assert.AreEqual(88.2d, level1.BestAskPrice);
            Assert.AreEqual(88.0d, level1.BestBidPrice);

            buyOrder1.Modify(50, 88.2d);

            Assert.AreEqual(88.2d, level1.BestAskPrice);
            Assert.IsNull(level1.BestBidPrice);

            var retrievedBuyOrder = repo.TryGetLimitOrder(buyOrder1.ExchangeOrderId);

            Assert.IsNull(retrievedBuyOrder);
        }
Exemplo n.º 6
0
        public void BookShouldUpdateBestAskWhenBetterSellPriceComesIn()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock.Object, marketOrderMatchingAlgorithmMock.Object, orderBookBestBidAsk);

            var sellOrder1 = new LimitOrder("ABC", 10, 90, WayEnum.Sell, 9);

            book.AddLimitOrder(sellOrder1);

            Assert.AreEqual(90, orderBookBestBidAsk.BestAskPrice);

            var sellOrder2 = new LimitOrder("ABC", 10, 89, WayEnum.Sell, 9);

            book.AddLimitOrder(sellOrder2);

            Assert.AreEqual(89, orderBookBestBidAsk.BestAskPrice);
        }
Exemplo n.º 7
0
        public void BookShouldUpdateBestBidWhenBetterBuyPriceComesIn()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock.Object, marketOrderMatchingAlgorithmMock.Object, orderBookBestBidAsk);

            var buyOrder1 = new LimitOrder("ABC", 10, 90, WayEnum.Buy, 9);

            book.AddLimitOrder(buyOrder1);

            Assert.AreEqual(90, orderBookBestBidAsk.BestBidPrice);

            var buyOrder2 = new LimitOrder("ABC", 10, 91, WayEnum.Buy, 9);

            book.AddLimitOrder(buyOrder2);

            Assert.AreEqual(91, orderBookBestBidAsk.BestBidPrice);
        }
Exemplo n.º 8
0
        public void IncomingBuyOrderShouldBeMatchedCompletelyAndNotBooked()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", new LimitOrderMatchingAlgorithm(new DateService()), new MarketOrderMatchingAlgorithm(new DateService()), orderBookBestBidAsk);

            var sellOrder1 = new LimitOrder("ABC", 100, 90, WayEnum.Sell, 9);

            book.AddLimitOrder(sellOrder1);

            var buyOrder1 = new LimitOrder("ABC", 10, 90, WayEnum.Buy, 9);

            book.AddLimitOrder(buyOrder1);

            Assert.AreEqual(0, buyOrder1.Quantity);
            Assert.AreEqual(90, sellOrder1.Quantity);

            Assert.AreEqual(0, book.PriceSlots[90].BuyOrders.Count);
            Assert.AreEqual(1, book.PriceSlots[90].SellOrders.Count);
        }
Exemplo n.º 9
0
        public void BookShouldSetBestAskAndNullBidOnOneSellOrder()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock.Object, marketOrderMatchingAlgorithmMock.Object, orderBookBestBidAsk);

            var sellOrder = new LimitOrder("ABC", 10, 90, WayEnum.Sell, 9);

            book.AddLimitOrder(sellOrder);

            Assert.AreEqual(90, orderBookBestBidAsk.BestAskPrice);
            Assert.IsNull(orderBookBestBidAsk.BestBidPrice);
        }
Exemplo n.º 10
0
        public void BookShouldSetBestBidAndNullAskOnOneBuyOrder()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock, marketOrderMatchingAlgorithmMock, orderBookBestBidAsk);

            var buyOrder = new LimitOrder("ABC", 10, 90, WayEnum.Buy, 9);

            book.AddLimitOrder(buyOrder);

            Assert.AreEqual(90, orderBookBestBidAsk.BestBidPrice);
            Assert.IsNull(orderBookBestBidAsk.BestAskPrice);
        }
Exemplo n.º 11
0
        public void BookShouldRemoveSlotWhenOrderIsFilled()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock.Object, marketOrderMatchingAlgorithmMock.Object, orderBookBestBidAsk);

            var buyOrder = new LimitOrder("ABC", 10, 90, WayEnum.Buy, 9);

            book.AddLimitOrder(buyOrder);

            Assert.IsTrue(book.PriceSlots.ContainsKey(90));
            Assert.AreEqual(buyOrder, book.PriceSlots[90].BuyOrders[0]);

            buyOrder.Modify(0);

            Assert.IsFalse(book.PriceSlots.ContainsKey(90));
        }
Exemplo n.º 12
0
        public void HigherBuyOrderShouldMatchLowerSellOrdersAndPlaceBuyOrderInBookOnRestQuantity()
        {
            var executions = new List <INewExecution>();

            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var matchAlgo           = new LimitOrderMatchingAlgorithm(new DateService());

            matchAlgo.AddExecutionsHandler(executions.Add);

            var book = new OrderBook("ABC", matchAlgo, marketOrderMatchingAlgorithmMock.Object, orderBookBestBidAsk);

            var sellOrder1 = new LimitOrder("ABC", 10, 90, WayEnum.Sell, 9);
            var sellOrder2 = new LimitOrder("ABC", 10, 91, WayEnum.Sell, 9);
            var sellOrder3 = new LimitOrder("ABC", 10, 92, WayEnum.Sell, 9);
            var sellOrder4 = new LimitOrder("ABC", 10, 93, WayEnum.Sell, 9);
            var sellOrder5 = new LimitOrder("ABC", 10, 94, WayEnum.Sell, 9);

            book.AddLimitOrder(sellOrder1);
            book.AddLimitOrder(sellOrder2);
            book.AddLimitOrder(sellOrder3);
            book.AddLimitOrder(sellOrder4);
            book.AddLimitOrder(sellOrder5);

            Assert.AreEqual(90, orderBookBestBidAsk.BestAskPrice);
            Assert.AreEqual(10, orderBookBestBidAsk.BestAskQuantity);

            var buyOrder1 = new LimitOrder("ABC", 100, 93, WayEnum.Buy, 9);

            book.AddLimitOrder(buyOrder1);

            Assert.AreEqual(4, executions.Count);
            Assert.AreEqual(60, buyOrder1.Quantity);

            Assert.AreEqual(94, orderBookBestBidAsk.BestAskPrice);
            Assert.AreEqual(10, orderBookBestBidAsk.BestAskQuantity);

            Assert.AreEqual(93, orderBookBestBidAsk.BestBidPrice);
            Assert.AreEqual(60, orderBookBestBidAsk.BestBidQuantity);
        }