public void AddBuyOrderBookToMemoryImage_ChecksWhetherOrderBooksGetAddedToImageListsPrperly_VerifiesImageListsToConfirm()
        {
            LimitOrderBook orderBook = new LimitOrderBook(CurrencyConstants.BtcUsd);
            Order          buyOrder1 = new Order(new OrderId("1"), CurrencyConstants.BtcUsd, new Price(1251), OrderSide.Buy,
                                                 OrderType.Limit, new Volume(250), new TraderId("1"));
            Order buyOrder2 = new Order(new OrderId("2"), CurrencyConstants.BtcUsd, new Price(1250), OrderSide.Buy,
                                        OrderType.Limit, new Volume(250), new TraderId("2"));
            Order buyOrder3 = new Order(new OrderId("3"), CurrencyConstants.BtcUsd, new Price(1252), OrderSide.Buy,
                                        OrderType.Limit, new Volume(250), new TraderId("3"));
            Order buyOrder4 = new Order(new OrderId("4"), CurrencyConstants.BtcUsd, new Price(1251), OrderSide.Buy,
                                        OrderType.Limit, new Volume(250), new TraderId("4"));

            orderBook.AddOrder(buyOrder1);
            orderBook.AddOrder(buyOrder2);
            orderBook.AddOrder(buyOrder3);
            orderBook.AddOrder(buyOrder4);

            OrderBookMemoryImage orderBookMemory = new OrderBookMemoryImage();

            orderBookMemory.OnOrderBookChanged(orderBook);

            Assert.AreEqual(CurrencyConstants.BtcUsd, orderBookMemory.BidBooks.First().CurrencyPair, "Currency pair of the first book in the" +
                            " memory image's list of BidOrderBook representations");
            Assert.AreEqual(4, orderBookMemory.BidBooks.First().Count(), "Count of the Bids in the first Bid Book present in the memory image");
            Assert.AreEqual(0, orderBookMemory.AskBooks.First().Count(), "Count of the Asks in te first Ask Book present in the memory image");
        }
        public void InitializeOrderBookRepresentationForACurrency_ChecksWhetherTheOrderRepresentationBooksInitializeForCurrencies_VerifiesUsingMemoryImage()
        {
            OrderBookMemoryImage orderBookMemory = new OrderBookMemoryImage();

            // At initialization, one OrderRepresentationBook gets initialized for bid and ask each
            Assert.AreEqual(1, orderBookMemory.BidBooks.Count(), "Count of the Bid Books present in the memory image");
            Assert.AreEqual(1, orderBookMemory.AskBooks.Count(), "Count of the Ask Books present in the memory image");
        }
 /// <summary>
 /// Default constructor
 /// </summary>
 public MarketDataQueryService(OrderBookMemoryImage orderBookMemoryImage, DepthMemoryImage depthMemoryImage,
                               BBOMemoryImage bboMemoryImage, IOhlcRepository ohlcRepository, TickerInfoService tickerInfoService)
 {
     _orderBookMemoryImage = orderBookMemoryImage;
     _depthMemoryImage     = depthMemoryImage;
     _bboMemoryImage       = bboMemoryImage;
     _ohlcRepository       = ohlcRepository;
     _tickerInfoService    = tickerInfoService;
 }
        public void SellOrdersPartiallyFilledTest_ChecksIfFilledOrdersAreRepresentedCorrectlyInAskBooksVolumes_VerifiesThroughAskBookOutput()
        {
            LimitOrderBook orderBook = new LimitOrderBook(CurrencyConstants.BtcUsd);

            Order buyOrder1 = new Order(new OrderId("1"), CurrencyConstants.BtcUsd, new Price(941), OrderSide.Buy,
                                        OrderType.Limit, new Volume(100), new TraderId("1"));
            Order buyOrder2 = new Order(new OrderId("2"), CurrencyConstants.BtcUsd, new Price(942), OrderSide.Buy,
                                        OrderType.Limit, new Volume(200), new TraderId("2"));
            Order buyOrder3 = new Order(new OrderId("3"), CurrencyConstants.BtcUsd, new Price(943), OrderSide.Buy,
                                        OrderType.Limit, new Volume(300), new TraderId("3"));
            Order buyOrder4 = new Order(new OrderId("4"), CurrencyConstants.BtcUsd, new Price(944), OrderSide.Buy,
                                        OrderType.Limit, new Volume(400), new TraderId("4"));

            Order sellOrder1 = new Order(new OrderId("5"), CurrencyConstants.BtcUsd, new Price(941), OrderSide.Sell,
                                         OrderType.Limit, new Volume(200), new TraderId("5"));
            Order sellOrder2 = new Order(new OrderId("6"), CurrencyConstants.BtcUsd, new Price(942), OrderSide.Sell,
                                         OrderType.Limit, new Volume(400), new TraderId("6"));
            Order sellOrder3 = new Order(new OrderId("7"), CurrencyConstants.BtcUsd, new Price(943), OrderSide.Sell,
                                         OrderType.Limit, new Volume(600), new TraderId("7"));
            Order sellOrder4 = new Order(new OrderId("8"), CurrencyConstants.BtcUsd, new Price(944), OrderSide.Sell,
                                         OrderType.Limit, new Volume(800), new TraderId("8"));

            orderBook.AddOrder(buyOrder1);
            orderBook.AddOrder(buyOrder2);
            orderBook.AddOrder(buyOrder3);
            orderBook.AddOrder(buyOrder4);
            orderBook.AddOrder(sellOrder4);
            orderBook.AddOrder(sellOrder3);
            orderBook.AddOrder(sellOrder2);
            orderBook.AddOrder(sellOrder1);

            OrderBookMemoryImage orderBookMemory = new OrderBookMemoryImage();

            orderBookMemory.OnOrderBookChanged(orderBook);

            Assert.AreEqual(CurrencyConstants.BtcUsd, orderBookMemory.BidBooks.First().CurrencyPair, "Currency pair of " +
                            "the first book in the memory image's list of BidOrderBook representations");
            // Both books contain one currency pair
            Assert.AreEqual(1, orderBookMemory.BidBooks.Count());
            Assert.AreEqual(1, orderBookMemory.AskBooks.Count());

            // Each currency pair contains several entries for the orders
            Assert.AreEqual(0, orderBookMemory.BidBooks.First().Count(), "Count of the Bids in the first Bid Book present in the memory image");
            Assert.AreEqual(4, orderBookMemory.AskBooks.First().Count(), "Count of the Asks in te first Ask Book present in the memory image");

            // Check the Volume elements of the first order book in the Memory Image's list of Order representations
            Assert.AreEqual(100, orderBookMemory.AskBooks.First().ToList()[0].Volume);
            Assert.AreEqual(200, orderBookMemory.AskBooks.First().ToList()[1].Volume);
            Assert.AreEqual(300, orderBookMemory.AskBooks.First().ToList()[2].Volume);
            Assert.AreEqual(400, orderBookMemory.AskBooks.First().ToList()[3].Volume);

            // Check Prices
            Assert.AreEqual(941, orderBookMemory.AskBooks.First().ToList()[0].Price);
            Assert.AreEqual(942, orderBookMemory.AskBooks.First().ToList()[1].Price);
            Assert.AreEqual(943, orderBookMemory.AskBooks.First().ToList()[2].Price);
            Assert.AreEqual(944, orderBookMemory.AskBooks.First().ToList()[3].Price);
        }
        public void GetOrderBookTest_ChecksIfOrderBookIsRetreivedProperly_ValidatesReturnedOrderBook()
        {
            // Get the context
            // IApplicationContext applicationContext = ContextRegistry.GetContext();

            // Get the instance through Spring configuration
            MarketController marketController = (MarketController)_applicationContext["MarketController"];
            LimitOrderBook   limitOrderBook   = new LimitOrderBook("XBTUSD");

            Order buyOrder1 = OrderFactory.CreateOrder("1233", "XBTUSD", Constants.ORDER_TYPE_LIMIT,
                                                       Constants.ORDER_SIDE_BUY, 150, 481.34M, new StubbedOrderIdGenerator());
            Order buyOrder2 = OrderFactory.CreateOrder("1234", "XBTUSD", Constants.ORDER_TYPE_LIMIT,
                                                       Constants.ORDER_SIDE_BUY, 50, 482.34M, new StubbedOrderIdGenerator());
            Order buyOrder3 = OrderFactory.CreateOrder("1222", "XBTUSD", Constants.ORDER_TYPE_LIMIT,
                                                       Constants.ORDER_SIDE_BUY, 50, 483.34M, new StubbedOrderIdGenerator());
            Order sellOrder1 = OrderFactory.CreateOrder("1233", "XBTUSD", Constants.ORDER_TYPE_LIMIT,
                                                        Constants.ORDER_SIDE_SELL, 150, 491.34M, new StubbedOrderIdGenerator());
            Order sellOrder2 = OrderFactory.CreateOrder("1234", "XBTUSD", Constants.ORDER_TYPE_LIMIT,
                                                        Constants.ORDER_SIDE_SELL, 50, 492.34M, new StubbedOrderIdGenerator());
            Order sellrder3 = OrderFactory.CreateOrder("1222", "XBTUSD", Constants.ORDER_TYPE_LIMIT,
                                                       Constants.ORDER_SIDE_SELL, 50, 493.34M, new StubbedOrderIdGenerator());

            limitOrderBook.PlaceOrder(buyOrder1);
            limitOrderBook.PlaceOrder(buyOrder2);
            limitOrderBook.PlaceOrder(buyOrder3);
            limitOrderBook.PlaceOrder(sellOrder1);
            limitOrderBook.PlaceOrder(sellOrder2);
            limitOrderBook.PlaceOrder(sellrder3);

            OrderBookMemoryImage orderBookMemoryImage = (OrderBookMemoryImage)_applicationContext["OrderBookMemoryImage"];

            orderBookMemoryImage.OnOrderBookChanged(limitOrderBook);
            IHttpActionResult httpActionResult = marketController.GetOrderBook("XBTUSD");

            OkNegotiatedContentResult <object> okResponseMessage =
                (OkNegotiatedContentResult <object>)httpActionResult;
            OrderBookRepresentation representation = okResponseMessage.Content as OrderBookRepresentation;

            Assert.IsNotNull(okResponseMessage);
            Assert.IsNotNull(representation);
            // Check the Currency Pair for Bid Book
            Assert.AreEqual("XBTUSD", representation.Bids.CurrencyPair);
            // Check the Currency Pair for Ask Book
            Assert.AreEqual("XBTUSD", representation.Asks.CurrencyPair);

            // Count of the number of Bids in the Bid Order Book
            Assert.AreEqual(3, representation.Bids.Count());
            // Count of the number of Asks in the Ask Order Book
            Assert.AreEqual(3, representation.Asks.Count());

            Assert.AreEqual(50, representation.Bids.ToList()[0].Volume);     // Highest Bid Volumein Bid Order Book
            Assert.AreEqual(483.34M, representation.Bids.ToList()[0].Price); // Highest Bid Price in Bid Order Book

            Assert.AreEqual(150, representation.Asks.ToList()[0].Volume);    // Highest Ask Volumein Ask Order Book
            Assert.AreEqual(491.34M, representation.Asks.ToList()[0].Price); // Highest Ask Price in Ask Order Book
        }
        public void GetBidOrderBookTest_ChecksIfOrderBookIsretreivedSuccessfully_VerifiesOrderBookToSeeItContainsValuesAsExpected()
        {
            OrderBookMemoryImage   orderBookMemoryImage    = new OrderBookMemoryImage();
            MarketDataQueryService memoryImageQueryService = new MarketDataQueryService(orderBookMemoryImage, null, null, null, null);
            LimitOrderBook         limitOrderBook          = new LimitOrderBook("BTCUSD");

            Order buyOrder1 = OrderFactory.CreateOrder("1233", "BTCUSD", Constants.ORDER_TYPE_LIMIT,
                                                       Constants.ORDER_SIDE_BUY, 100, 941, new StubbedOrderIdGenerator());
            Order buyOrder2 = OrderFactory.CreateOrder("1234", "BTCUSD", Constants.ORDER_TYPE_LIMIT,
                                                       Constants.ORDER_SIDE_BUY, 100, 945, new StubbedOrderIdGenerator());
            Order buyOrder3 = OrderFactory.CreateOrder("1234", "BTCUSD", Constants.ORDER_TYPE_LIMIT,
                                                       Constants.ORDER_SIDE_BUY, 100, 946, new StubbedOrderIdGenerator());
            Order buyOrder4 = OrderFactory.CreateOrder("12347", "BTCUSD", Constants.ORDER_TYPE_LIMIT,
                                                       Constants.ORDER_SIDE_BUY, 100, 940, new StubbedOrderIdGenerator());

            Order sellOrder1 = OrderFactory.CreateOrder("1244", "BTCUSD", Constants.ORDER_TYPE_LIMIT,
                                                        Constants.ORDER_SIDE_SELL, 100, 949, new StubbedOrderIdGenerator());
            Order sellOrder2 = OrderFactory.CreateOrder("1222", "BTCUSD", Constants.ORDER_TYPE_LIMIT,
                                                        Constants.ORDER_SIDE_SELL, 100, 948, new StubbedOrderIdGenerator());
            Order sellOrder3 = OrderFactory.CreateOrder("1222", "BTCUSD", Constants.ORDER_TYPE_LIMIT,
                                                        Constants.ORDER_SIDE_SELL, 100, 947, new StubbedOrderIdGenerator());
            Order sellOrder4 = OrderFactory.CreateOrder("127633", "BTCUSD", Constants.ORDER_TYPE_LIMIT,
                                                        Constants.ORDER_SIDE_SELL, 100, 950, new StubbedOrderIdGenerator());

            limitOrderBook.AddOrder(buyOrder1);
            limitOrderBook.AddOrder(buyOrder2);
            limitOrderBook.AddOrder(buyOrder3);
            limitOrderBook.AddOrder(buyOrder4);
            limitOrderBook.AddOrder(sellOrder1);
            limitOrderBook.AddOrder(sellOrder2);
            limitOrderBook.AddOrder(sellOrder3);
            limitOrderBook.AddOrder(sellOrder4);

            Assert.AreEqual(4, limitOrderBook.Bids.Count());
            Assert.AreEqual(4, limitOrderBook.Asks.Count());
            orderBookMemoryImage.OnOrderBookChanged(limitOrderBook);

            OrderBookRepresentation orderBooksTuple = (OrderBookRepresentation)memoryImageQueryService.GetOrderBook("BTCUSD", 3);

            Assert.AreEqual(3, orderBooksTuple.Bids.Count());
            Assert.AreEqual("BTCUSD", orderBooksTuple.Bids.CurrencyPair);
            Assert.AreEqual(100, orderBooksTuple.Bids.ToList()[0].Volume); // Volume
            Assert.AreEqual(946, orderBooksTuple.Bids.ToList()[0].Price);  // Price

            Assert.AreEqual(100, orderBooksTuple.Bids.ToList()[1].Volume); // Volume
            Assert.AreEqual(945, orderBooksTuple.Bids.ToList()[1].Price);  // Price

            Assert.AreEqual(100, orderBooksTuple.Bids.ToList()[2].Volume); // Volume
            Assert.AreEqual(941, orderBooksTuple.Bids.ToList()[2].Price);  // Price
        }
        public void CheckAskBookVolumes_ChecksWhetherOrderOfAsksPricesIsSameAsTheLimitOrderBook_VerifiesImageListsToConfirm()
        {
            LimitOrderBook orderBook  = new LimitOrderBook(CurrencyConstants.BtcUsd);
            Order          sellOrder1 = new Order(new OrderId("1"), CurrencyConstants.BtcUsd, new Price(1251), OrderSide.Sell,
                                                  OrderType.Limit, new Volume(100), new TraderId("1"));
            Order sellOrder2 = new Order(new OrderId("2"), CurrencyConstants.BtcUsd, new Price(1250), OrderSide.Sell,
                                         OrderType.Limit, new Volume(200), new TraderId("2"));
            Order sellOrder3 = new Order(new OrderId("3"), CurrencyConstants.BtcUsd, new Price(1252), OrderSide.Sell,
                                         OrderType.Limit, new Volume(300), new TraderId("3"));
            Order sellOrder4 = new Order(new OrderId("4"), CurrencyConstants.BtcUsd, new Price(1253), OrderSide.Sell,
                                         OrderType.Limit, new Volume(400), new TraderId("4"));

            orderBook.AddOrder(sellOrder1);
            orderBook.AddOrder(sellOrder2);
            orderBook.AddOrder(sellOrder3);
            orderBook.AddOrder(sellOrder4);

            OrderBookMemoryImage orderBookMemory = new OrderBookMemoryImage();

            orderBookMemory.OnOrderBookChanged(orderBook);

            Assert.AreEqual(CurrencyConstants.BtcUsd, orderBookMemory.AskBooks.First().CurrencyPair, "Currency pair of " +
                            "the first book in the memory image's list of BidOrderBook representations");
            Assert.AreEqual(1, orderBookMemory.BidBooks.Count());
            Assert.AreEqual(1, orderBookMemory.AskBooks.Count());

            Assert.AreEqual(0, orderBookMemory.BidBooks.First().Count(), "Count of the Bids in the first Bid Book present in the memory image");
            Assert.AreEqual(4, orderBookMemory.AskBooks.First().Count(), "Count of the Asks in te first Ask Book present in the memory image");

            // Check the Prices elements of the first order book in the Memory Image's list of Order representations
            Assert.AreEqual(200, orderBookMemory.AskBooks.First().ToList()[0].Volume);
            Assert.AreEqual(100, orderBookMemory.AskBooks.First().ToList()[1].Volume);
            Assert.AreEqual(300, orderBookMemory.AskBooks.First().ToList()[2].Volume);
            Assert.AreEqual(400, orderBookMemory.AskBooks.First().ToList()[3].Volume);

            Assert.AreEqual(1, orderBookMemory.BidBooks.Count());
            Assert.AreEqual(1, orderBookMemory.AskBooks.Count());
        }