Ejemplo n.º 1
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);
        }
        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);
        }
Ejemplo 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);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo 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);
        }
        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);
        }
Ejemplo n.º 7
0
        public void ShouldNotMatchHigherSellOrderWithLowerBuyOrder()
        {
            var generatedExecutions = new List <INewExecution>();
            var algo = new LimitOrderMatchingAlgorithm(dateServiceMock.Object);

            algo.AddExecutionsHandler(generatedExecutions.Add);

            ILimitOrder sellOrder = new LimitOrder("ABC", 10, 100, WayEnum.Sell, 13);
            ILimitOrder buyOrder  = new LimitOrder("ABC", 10, 90, WayEnum.Buy, 12);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(0, generatedExecutions.Count);
        }
Ejemplo n.º 8
0
        public void ShouldNotMatchOrdersWithDifferentSymbols()
        {
            var generatedExecutions = new List <INewExecution>();
            var algo = new LimitOrderMatchingAlgorithm(dateServiceMock);

            algo.AddExecutionsHandler(generatedExecutions.Add);

            ILimitOrder sellOrder = new LimitOrder("ABC", 10, 90, WayEnum.Sell, 13);
            ILimitOrder buyOrder  = new LimitOrder("QQQ", 10, 90, WayEnum.Buy, 12);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(0, generatedExecutions.Count);
        }
        public void ShouldExecuteOnLowestQuantity()
        {
            var staticDatetimeOffset = DateTimeOffset.UtcNow;
            dateServiceMock.Stub(a => a.UtcNow()).Return(staticDatetimeOffset);

            var generatedExecutions = new List<INewExecution>();
            var algo = new LimitOrderMatchingAlgorithm(dateServiceMock);
            algo.AddExecutionsHandler(generatedExecutions.Add);

            ILimitOrder sellOrder = new LimitOrder("ABC", 40, 90, WayEnum.Sell, 13);
            ILimitOrder buyOrder = new LimitOrder("ABC", 100, 90, WayEnum.Buy, 12);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(1, generatedExecutions.Count);
            Assert.AreEqual(40, generatedExecutions[0].MatchedQuantity);
        }
        public void OrdersShouldNotHaveModifiedQuantitiesAfterNoMatch()
        {
            var staticDatetimeOffset = DateTimeOffset.UtcNow;
            dateServiceMock.Stub(a => a.UtcNow()).Return(staticDatetimeOffset);

            var generatedExecutions = new List<INewExecution>();
            var algo = new LimitOrderMatchingAlgorithm(dateServiceMock);
            algo.AddExecutionsHandler(generatedExecutions.Add);

            ILimitOrder sellOrder = new LimitOrder("ABC", 40, 91, WayEnum.Sell, 13);
            ILimitOrder buyOrder = new LimitOrder("ABC", 100, 90, WayEnum.Buy, 12);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(40, sellOrder.Quantity);
            Assert.AreEqual(100, buyOrder.Quantity);
        }
Ejemplo n.º 11
0
        public void OrdersShouldNotHaveModifiedQuantitiesAfterNoMatch()
        {
            var staticDatetimeOffset = DateTimeOffset.UtcNow;

            dateServiceMock.Stub(a => a.UtcNow()).Return(staticDatetimeOffset);

            var generatedExecutions = new List <INewExecution>();
            var algo = new LimitOrderMatchingAlgorithm(dateServiceMock);

            algo.AddExecutionsHandler(generatedExecutions.Add);

            ILimitOrder sellOrder = new LimitOrder("ABC", 40, 91, WayEnum.Sell, 13);
            ILimitOrder buyOrder  = new LimitOrder("ABC", 100, 90, WayEnum.Buy, 12);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(40, sellOrder.Quantity);
            Assert.AreEqual(100, buyOrder.Quantity);
        }
Ejemplo n.º 12
0
        public void ShouldExecuteOnLowestQuantity()
        {
            var staticDatetimeOffset = DateTimeOffset.UtcNow;

            dateServiceMock.Stub(a => a.UtcNow()).Return(staticDatetimeOffset);

            var generatedExecutions = new List <INewExecution>();
            var algo = new LimitOrderMatchingAlgorithm(dateServiceMock);

            algo.AddExecutionsHandler(generatedExecutions.Add);

            ILimitOrder sellOrder = new LimitOrder("ABC", 40, 90, WayEnum.Sell, 13);
            ILimitOrder buyOrder  = new LimitOrder("ABC", 100, 90, WayEnum.Buy, 12);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(1, generatedExecutions.Count);
            Assert.AreEqual(40, generatedExecutions[0].MatchedQuantity);
        }
Ejemplo n.º 13
0
        public void ShouldMatchBuyOrderCompletely()
        {
            var executions = new List<INewExecution>();
            var executionalgo = new LimitOrderMatchingAlgorithm(new DateService());
            executionalgo.AddExecutionsHandler(executions.Add);

            var priceSlot = new PriceSlot(90, executionalgo, new MarketOrderMatchingAlgorithm(new DateService()));

            var sellOrder = new LimitOrder("ABC", 100, 90, WayEnum.Sell, 90);
            var buyOrder = new LimitOrder("ABC", 10, 90, WayEnum.Buy, 80);

            priceSlot.AddOrder(sellOrder);
            priceSlot.TryMatchLimitOrder(buyOrder);

            Assert.AreEqual(1, executions.Count);
            Assert.AreEqual(0, priceSlot.BuyOrders.Count);
            Assert.AreEqual(1, priceSlot.SellOrders.Count);
            Assert.AreEqual(90, priceSlot.SellOrders[0].Quantity);
        }
Ejemplo n.º 14
0
        public void ShouldNotMatchOrdersWithZeroQuantity()
        {
            var generatedExecutions = new List <INewExecution>();
            var algo = new LimitOrderMatchingAlgorithm(dateServiceMock.Object);

            algo.AddExecutionsHandler(generatedExecutions.Add);

            ILimitOrder sellOrder = new LimitOrder("ABC", 0, 90, WayEnum.Sell, 13);
            ILimitOrder buyOrder  = new LimitOrder("ABC", 10, 90, WayEnum.Buy, 12);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(0, generatedExecutions.Count);

            sellOrder.Modify(90);
            buyOrder.Modify(0);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(0, generatedExecutions.Count);
        }
Ejemplo n.º 15
0
        public void ShouldMatchMultipleSellOrdersToSameBuyOrder()
        {
            var executions = new List<INewExecution>();
            var executionalgo = new LimitOrderMatchingAlgorithm(new DateService());
            executionalgo.AddExecutionsHandler(executions.Add);

            var priceSlot = new PriceSlot(90, executionalgo, new MarketOrderMatchingAlgorithm(new DateService()));

            var sellOrder1 = new LimitOrder("ABC", 10, 90, WayEnum.Sell, 90);
            var sellOrder2 = new LimitOrder("ABC", 40, 90, WayEnum.Sell, 90);
            var buyOrder = new LimitOrder("ABC", 50, 90, WayEnum.Buy, 80);

            priceSlot.AddOrder(sellOrder1);
            priceSlot.AddOrder(sellOrder2);
            priceSlot.TryMatchLimitOrder(buyOrder);

            Assert.AreEqual(2, executions.Count);
            Assert.AreEqual(0, priceSlot.BuyOrders.Count);
            Assert.AreEqual(0, priceSlot.SellOrders.Count);
        }
Ejemplo n.º 16
0
        public void ShouldMatchMultipleSellOrdersToSameBuyOrder()
        {
            var executions    = new List <INewExecution>();
            var executionalgo = new LimitOrderMatchingAlgorithm(new DateService());

            executionalgo.AddExecutionsHandler(executions.Add);

            var priceSlot = new PriceSlot(90, executionalgo, new MarketOrderMatchingAlgorithm(new DateService()));

            var sellOrder1 = new LimitOrder("ABC", 10, 90, WayEnum.Sell, 90);
            var sellOrder2 = new LimitOrder("ABC", 40, 90, WayEnum.Sell, 90);
            var buyOrder   = new LimitOrder("ABC", 50, 90, WayEnum.Buy, 80);

            priceSlot.AddOrder(sellOrder1);
            priceSlot.AddOrder(sellOrder2);
            priceSlot.TryMatchLimitOrder(buyOrder);

            Assert.AreEqual(2, executions.Count);
            Assert.AreEqual(0, priceSlot.BuyOrders.Count);
            Assert.AreEqual(0, priceSlot.SellOrders.Count);
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
0
        public void ShouldMatchBuyOrderCompletely()
        {
            var executions    = new List <INewExecution>();
            var executionalgo = new LimitOrderMatchingAlgorithm(new DateService());

            executionalgo.AddExecutionsHandler(executions.Add);



            var priceSlot = new PriceSlot(90, executionalgo, new MarketOrderMatchingAlgorithm(new DateService()));

            var sellOrder = new LimitOrder("ABC", 100, 90, WayEnum.Sell, 90);
            var buyOrder  = new LimitOrder("ABC", 10, 90, WayEnum.Buy, 80);

            priceSlot.AddOrder(sellOrder);
            priceSlot.TryMatchLimitOrder(buyOrder);

            Assert.AreEqual(1, executions.Count);
            Assert.AreEqual(0, priceSlot.BuyOrders.Count);
            Assert.AreEqual(1, priceSlot.SellOrders.Count);
            Assert.AreEqual(90, priceSlot.SellOrders[0].Quantity);
        }
Ejemplo n.º 19
0
        public void ShouldGenerateExecutionWithSamePriceAsBuyAndSell()
        {
            var staticDatetimeOffset = DateTimeOffset.UtcNow;

            dateServiceMock.Stub(a => a.UtcNow()).Return(staticDatetimeOffset);

            var generatedExecutions = new List <INewExecution>();
            var algo = new LimitOrderMatchingAlgorithm(dateServiceMock);

            algo.AddExecutionsHandler(generatedExecutions.Add);

            ILimitOrder sellOrder = new LimitOrder("ABC", 10, 90, WayEnum.Sell, 13);
            ILimitOrder buyOrder  = new LimitOrder("ABC", 10, 90, WayEnum.Buy, 12);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(1, generatedExecutions.Count);

            Assert.AreEqual(10, generatedExecutions[0].MatchedQuantity);
            Assert.AreEqual(90, generatedExecutions[0].MatchedPrice);
            Assert.AreEqual(buyOrder, generatedExecutions[0].BuySideOrder);
            Assert.AreEqual(sellOrder, generatedExecutions[0].SellSideOrder);
            Assert.AreEqual(staticDatetimeOffset, generatedExecutions[0].ExecutionTime);
        }
Ejemplo n.º 20
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, 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);
        }
Ejemplo n.º 21
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, 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);
        }
Ejemplo n.º 22
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, 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 ShouldGenerateExecutionWithSamePriceAsBuyAndSell()
        {
            var staticDatetimeOffset = DateTimeOffset.UtcNow;
            dateServiceMock.Stub(a => a.UtcNow()).Return(staticDatetimeOffset);

            var generatedExecutions = new List<INewExecution>();
            var algo = new LimitOrderMatchingAlgorithm(dateServiceMock);
            algo.AddExecutionsHandler(generatedExecutions.Add);

            ILimitOrder sellOrder = new LimitOrder("ABC", 10, 90, WayEnum.Sell, 13);
            ILimitOrder buyOrder = new LimitOrder("ABC", 10, 90, WayEnum.Buy, 12);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(1, generatedExecutions.Count);

            Assert.AreEqual(10, generatedExecutions[0].MatchedQuantity);
            Assert.AreEqual(90, generatedExecutions[0].MatchedPrice);
            Assert.AreEqual(buyOrder, generatedExecutions[0].BuySideOrder);
            Assert.AreEqual(sellOrder, generatedExecutions[0].SellSideOrder);
            Assert.AreEqual(staticDatetimeOffset, generatedExecutions[0].ExecutionTime);
        }
        public void ShouldNotMatchOrdersWithZeroQuantity()
        {
            var generatedExecutions = new List<INewExecution>();
            var algo = new LimitOrderMatchingAlgorithm(dateServiceMock);
            algo.AddExecutionsHandler(generatedExecutions.Add);

            ILimitOrder sellOrder = new LimitOrder("ABC", 0, 90, WayEnum.Sell, 13);
            ILimitOrder buyOrder = new LimitOrder("ABC", 10, 90, WayEnum.Buy, 12);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(0, generatedExecutions.Count);

            sellOrder.Modify(90);
            buyOrder.Modify(0);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(0, generatedExecutions.Count);
        }
        public void ShouldNotMatchHigherSellOrderWithLowerBuyOrder()
        {
            var generatedExecutions = new List<INewExecution>();
            var algo = new LimitOrderMatchingAlgorithm(dateServiceMock);
            algo.AddExecutionsHandler(generatedExecutions.Add);

            ILimitOrder sellOrder = new LimitOrder("ABC", 10, 100, WayEnum.Sell, 13);
            ILimitOrder buyOrder = new LimitOrder("ABC", 10, 90, WayEnum.Buy, 12);

            algo.TryMatch(buyOrder, sellOrder);
            Assert.AreEqual(0, generatedExecutions.Count);
        }