Inheritance: IOrderBookBestBidAsk
        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);
        }
        public void HandleAddLimitOrder(ILimitOrder limitOrder)
        {
            string symbol = limitOrder.Symbol;
            if (!OrderBooks.ContainsKey(symbol))
            {
                var bookMatchingLimitAlgo = new LimitOrderMatchingAlgorithm(dateService);
                bookMatchingLimitAlgo.AddExecutionsHandler(outgoingQueue.EnqueueClientExecution);

                var bookMatchingMarketAlgo = new MarketOrderMatchingAlgorithm(dateService);
                bookMatchingMarketAlgo.AddExecutionsHandler(outgoingQueue.EnqueueClientExecution);

                var level1 = new OrderBookBestBidAsk(symbol);
                level1.RegisterUpdateHandler(outgoingQueue.EnqueueLevel1Update);

                var book = new OrderBook(symbol, bookMatchingLimitAlgo, bookMatchingMarketAlgo, level1);
                OrderBooks.Add(symbol, book);
            }

            outgoingQueue.EnqueueAddedLimitOrder(limitOrder);
            limitOrder.RegisterDeleteNotificationHandler(OrderBooks[symbol].RemoveLimitOrder);
            limitOrder.RegisterFilledNotification(OrderBooks[symbol].RemoveLimitOrder);
            limitOrder.RegisterModifyNotificationHandler(OrderBooks[symbol].HandleLimitOrderModify);

            OrderBooks[symbol].AddLimitOrder(limitOrder);
        }
        public void BookShouldSetBestAskAndNullBidOnOneSellOrder()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock, marketOrderMatchingAlgorithmMock, orderBookBestBidAsk);

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

            Assert.AreEqual(90, orderBookBestBidAsk.BestAskPrice);
            Assert.IsNull(orderBookBestBidAsk.BestBidPrice);
        }
        public void BookShouldRemoveSlotWhenOrderIsFilled()
        {
            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.IsTrue(book.PriceSlots.ContainsKey(90));
            Assert.AreEqual(buyOrder, book.PriceSlots[90].BuyOrders[0]);

            buyOrder.Modify(0);

            Assert.IsFalse(book.PriceSlots.ContainsKey(90));
        }
        public void ShouldNotTriggerSellSideOnHigherBidAndAskPrice()
        {
            bool triggered = false;

            var trigger = new BestPriceTrigger("abc", 40, WayEnum.Sell);
            trigger.SetTriggerAction(() => triggered = true);

            var bb = new OrderBookBestBidAsk("abc");
            bb.Set(50, 51, 1, 1);

            var triggerResult = trigger.TryExecute(bb);

            Assert.IsFalse(triggerResult);
            Assert.IsFalse(triggered);
        }
        public void ShouldNotTriggerBuySideOnLowerBidPrice()
        {
            bool triggered = false;

            var trigger = new BestPriceTrigger("abc", 10.4d, WayEnum.Buy);
            trigger.SetTriggerAction(() => triggered = true);

            var bb = new OrderBookBestBidAsk("abc");
            bb.Set(9.4d, null, 1, 1);

            var triggerResult = trigger.TryExecute(bb);

            Assert.IsFalse(triggerResult);
            Assert.IsFalse(triggered);
        }
        public void BookShouldSetBestAskOnLowerSellOrders()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock, marketOrderMatchingAlgorithmMock, 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);
        }
        public void ShouldRemoveTriggerWhenTriggerIsExecuted()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock, marketOrderMatchingAlgorithmMock, orderBookBestBidAsk);

            var trigger = new BestPriceTrigger("ABC", 10, WayEnum.Buy);
            var stopOrder = new StopLimitOrder("abc", 10, 90, 90, WayEnum.Buy, 2, trigger);

            trigger.SetTriggerAction(() =>
            {
                stopOrder.Delete();
            });

            book.AddStopLimitOrder(stopOrder);

            Assert.AreEqual(trigger, book.BuySideStopLimitOrders[0].Trigger);

            orderBookBestBidAsk.Set(11,12, 100, 100);
            trigger.TryExecute(orderBookBestBidAsk);

            Assert.AreEqual(0, book.BuySideStopLimitOrders.Count);
        }
        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, 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);
        }
        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, 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);
        }
        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);
        }
        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, 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);
        }
        public void BookShouldUpdateBestBidWhenBetterBuyPriceComesIn()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock, marketOrderMatchingAlgorithmMock, 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);
        }
        public void BookShouldUpdateBestAskWhenBetterSellPriceComesIn()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock, marketOrderMatchingAlgorithmMock, 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);
        }
        public void BookShouldSetBestBidOnHigherBuyOrders()
        {
            var orderBookBestBidAsk = new OrderBookBestBidAsk("ABC");
            var book = new OrderBook("ABC", limitOrderMatchingAlgorithmMock, marketOrderMatchingAlgorithmMock, 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);
        }
        public void ShouldTriggerBuySideOnEqualBidHigherAskPrice()
        {
            bool triggered = false;

            var trigger = new BestPriceTrigger("abc", 50d, WayEnum.Buy);
            trigger.SetTriggerAction(() => triggered = true);

            var bb = new OrderBookBestBidAsk("abc");
            bb.Set(50, 51, 1, 1);

            var triggerResult = trigger.TryExecute(bb);

            Assert.IsTrue(triggerResult);
            Assert.IsTrue(triggered);
        }