public void AskDepthLevelsVolumeCheck_ChecksWhetherTheLevelshaveTheSameVolumeAsInDepthLevels_VerifiesFromTheAskDepthRepresentations()
        {
            DepthMemoryImage depthMemoryImage = new DepthMemoryImage();
            Depth            depth            = new Depth(CurrencyConstants.BtcUsd, 10);

            depth.AddOrder(new Price(491), new Volume(100), OrderSide.Sell);
            depth.AddOrder(new Price(498), new Volume(800), OrderSide.Sell);
            depth.AddOrder(new Price(493), new Volume(300), OrderSide.Sell);
            depth.AddOrder(new Price(496), new Volume(600), OrderSide.Sell);

            Assert.AreEqual(10, depth.BidLevels.Count());
            Assert.AreEqual(10, depth.AskLevels.Count());

            depthMemoryImage.OnDepthArrived(depth);

            Assert.AreEqual(1, depthMemoryImage.BidDepths.Count());
            Assert.AreEqual(1, depthMemoryImage.AskDepths.Count());

            Assert.AreEqual(4, depthMemoryImage.AskDepths.First().Value.Count());

            Assert.AreEqual(100, depthMemoryImage.AskDepths.First().Value.First().Volume);
            Assert.AreEqual(300, depthMemoryImage.AskDepths.First().Value.ToList()[1].Volume);
            Assert.AreEqual(600, depthMemoryImage.AskDepths.First().Value.ToList()[2].Volume);
            Assert.AreEqual(800, depthMemoryImage.AskDepths.First().Value.ToList()[3].Volume);
        }
        public void GetAskDepthForACurrencyTest_TestsTheAskDepthRetreivalForAParticularCurrencyPair_ReturnsDepthLevelinformationForEachLevel()
        {
            DepthMemoryImage       depthMemoryImage        = new DepthMemoryImage();
            MarketDataQueryService memoryImageQueryService = new MarketDataQueryService(null, depthMemoryImage, null, null, null);

            Depth depth = new Depth("BTCUSD", 10);

            depth.AddOrder(new Price(941), new Volume(300), OrderSide.Buy);
            depth.AddOrder(new Price(941), new Volume(200), OrderSide.Buy);
            depth.AddOrder(new Price(942), new Volume(200), OrderSide.Buy);
            depth.AddOrder(new Price(943), new Volume(500), OrderSide.Buy);
            depth.AddOrder(new Price(948), new Volume(300), OrderSide.Sell);
            depth.AddOrder(new Price(949), new Volume(200), OrderSide.Sell);
            depth.AddOrder(new Price(949), new Volume(200), OrderSide.Sell);
            depth.AddOrder(new Price(949), new Volume(500), OrderSide.Sell);
            depthMemoryImage.OnDepthArrived(depth);

            var returnedDepth = memoryImageQueryService.GetDepth("BTCUSD") as DepthTupleRepresentation;

            Assert.AreEqual(300, returnedDepth.AskDepth[0].Volume);   // Aggregated Volume
            Assert.AreEqual(948, returnedDepth.AskDepth[0].Price);    // Price
            Assert.AreEqual(1, returnedDepth.AskDepth[0].OrderCount); // OrderCount
            Assert.AreEqual(900, returnedDepth.AskDepth[1].Volume);   // Aggregated Volume
            Assert.AreEqual(949, returnedDepth.AskDepth[1].Price);    // Price
            Assert.AreEqual(3, returnedDepth.AskDepth[1].OrderCount); // OrderCount
            Assert.IsNull(returnedDepth.AskDepth[2]);                 // Second index is null as there is no depth level for it
        }
 /// <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 SellDepthMemoryImageInitialization_ChecksWhetherTheDictionariesInitializesAsExpected_ChecksTheCountForDepthDictionaries()
        {
            DepthMemoryImage depthMemoryImage = new DepthMemoryImage();
            Depth            depth            = new Depth(CurrencyConstants.BtcUsd, 10);

            depth.AddOrder(new Price(491), new Volume(100), OrderSide.Sell);
            depth.AddOrder(new Price(498), new Volume(100), OrderSide.Sell);
            depth.AddOrder(new Price(493), new Volume(100), OrderSide.Sell);
            depth.AddOrder(new Price(496), new Volume(100), OrderSide.Sell);

            Assert.AreEqual(10, depth.BidLevels.Count());
            Assert.AreEqual(10, depth.AskLevels.Count());

            depthMemoryImage.OnDepthArrived(depth);

            Assert.AreEqual(1, depthMemoryImage.BidDepths.Count());
            Assert.AreEqual(1, depthMemoryImage.AskDepths.Count());

            Assert.AreEqual(4, depthMemoryImage.AskDepths.First().Value.Count());
        }
        public void GetDepthFromControllerTest_TestsTheLinkBetweenMarketControllerAndMArketQueryService_ChecksTheOutputToBeAsExpected()
        {
            // Get the context
            //IApplicationContext applicationContext = ContextRegistry.GetContext();

            // Get the instance through Spring configuration
            MarketController marketController = (MarketController)_applicationContext["MarketController"];
            Depth            depth            = new Depth("XBTUSD", 3);

            depth.AddOrder(new Price(491), new Volume(100), OrderSide.Buy);
            depth.AddOrder(new Price(492), new Volume(100), OrderSide.Buy);
            depth.AddOrder(new Price(492), new Volume(200), OrderSide.Buy);

            depth.AddOrder(new Price(490), new Volume(100), OrderSide.Sell);
            depth.AddOrder(new Price(490), new Volume(100), OrderSide.Sell);
            depth.AddOrder(new Price(490), new Volume(200), OrderSide.Sell);

            DepthMemoryImage depthMemoryImage = (DepthMemoryImage)_applicationContext["DepthMemoryImage"];

            depthMemoryImage.OnDepthArrived(depth);

            IHttpActionResult httpActionResult = marketController.GetDepth("XBTUSD");

            OkNegotiatedContentResult <object> result =
                (OkNegotiatedContentResult <object>)httpActionResult;

            DepthTupleRepresentation returnedDepths = result.Content as DepthTupleRepresentation;

            Assert.IsNotNull(returnedDepths);
            // Bid Depth First Element represented as Item1
            Assert.AreEqual(300, returnedDepths.BidDepth.ToList()[0].Volume);   // Aggregated Volume
            Assert.AreEqual(492, returnedDepths.BidDepth.ToList()[0].Price);    // Price
            Assert.AreEqual(2, returnedDepths.BidDepth.ToList()[0].OrderCount); // OrderCount

            // Ask Depth First Element represented as Item2
            Assert.AreEqual(400, returnedDepths.AskDepth.ToList()[0].Volume);   // Aggregated Volume
            Assert.AreEqual(490, returnedDepths.AskDepth.ToList()[0].Price);    // Price
            Assert.AreEqual(3, returnedDepths.AskDepth.ToList()[0].OrderCount); // OrderCount
        }
        public void LastAskRemovedTest_TestsIfTheLastAskIsRemovedFromTheMemorymagesListOfDepths_VerifiesusingMemoryImagesAskBook()
        {
            DepthMemoryImage depthMemoryImage = new DepthMemoryImage();
            Depth            depth            = new Depth(CurrencyConstants.BtcUsd, 5);

            depth.AddOrder(new Price(491), new Volume(100), OrderSide.Buy);
            depth.AddOrder(new Price(498), new Volume(800), OrderSide.Buy);
            depth.AddOrder(new Price(493), new Volume(300), OrderSide.Buy);
            depth.AddOrder(new Price(496), new Volume(600), OrderSide.Buy);

            depth.AddOrder(new Price(491), new Volume(100), OrderSide.Sell);
            depth.AddOrder(new Price(498), new Volume(800), OrderSide.Sell);
            depth.AddOrder(new Price(493), new Volume(300), OrderSide.Sell);
            depth.AddOrder(new Price(496), new Volume(600), OrderSide.Sell);

            // Checks first and last bid prices
            Assert.AreEqual(498, depth.BidLevels[0].Price.Value);
            Assert.AreEqual(491, depth.BidLevels[3].Price.Value);
            // Checks first and last ask prices
            Assert.AreEqual(491, depth.AskLevels[0].Price.Value);
            Assert.AreEqual(498, depth.AskLevels[3].Price.Value);

            depthMemoryImage.OnDepthArrived(depth);

            Assert.AreEqual(1, depthMemoryImage.BidDepths.Count());
            Assert.AreEqual(1, depthMemoryImage.AskDepths.Count());

            // Bids in DepthMemoryImage
            Assert.AreEqual(800, depthMemoryImage.BidDepths.First().Value.DepthLevels[0].Volume);
            Assert.AreEqual(498, depthMemoryImage.BidDepths.First().Value.DepthLevels[0].Price);
            Assert.AreEqual(1, depthMemoryImage.BidDepths.First().Value.DepthLevels[0].OrderCount);
            Assert.AreEqual(600, depthMemoryImage.BidDepths.First().Value.DepthLevels[1].Volume);
            Assert.AreEqual(496, depthMemoryImage.BidDepths.First().Value.DepthLevels[1].Price);
            Assert.AreEqual(1, depthMemoryImage.BidDepths.First().Value.DepthLevels[1].OrderCount);
            Assert.AreEqual(300, depthMemoryImage.BidDepths.First().Value.DepthLevels[2].Volume);
            Assert.AreEqual(493, depthMemoryImage.BidDepths.First().Value.DepthLevels[2].Price);
            Assert.AreEqual(1, depthMemoryImage.BidDepths.First().Value.DepthLevels[2].OrderCount);
            Assert.AreEqual(100, depthMemoryImage.BidDepths.First().Value.DepthLevels[3].Volume);
            Assert.AreEqual(491, depthMemoryImage.BidDepths.First().Value.DepthLevels[3].Price);
            Assert.AreEqual(1, depthMemoryImage.BidDepths.First().Value.DepthLevels[3].OrderCount);
            // Asks in DepthMemoryImage
            Assert.AreEqual(100, depthMemoryImage.AskDepths.First().Value.DepthLevels[0].Volume);
            Assert.AreEqual(491, depthMemoryImage.AskDepths.First().Value.DepthLevels[0].Price);
            Assert.AreEqual(1, depthMemoryImage.AskDepths.First().Value.DepthLevels[0].OrderCount);
            Assert.AreEqual(300, depthMemoryImage.AskDepths.First().Value.DepthLevels[1].Volume);
            Assert.AreEqual(493, depthMemoryImage.AskDepths.First().Value.DepthLevels[1].Price);
            Assert.AreEqual(1, depthMemoryImage.AskDepths.First().Value.DepthLevels[1].OrderCount);
            Assert.AreEqual(600, depthMemoryImage.AskDepths.First().Value.DepthLevels[2].Volume);
            Assert.AreEqual(496, depthMemoryImage.AskDepths.First().Value.DepthLevels[2].Price);
            Assert.AreEqual(1, depthMemoryImage.AskDepths.First().Value.DepthLevels[2].OrderCount);
            Assert.AreEqual(800, depthMemoryImage.AskDepths.First().Value.DepthLevels[3].Volume);
            Assert.AreEqual(498, depthMemoryImage.AskDepths.First().Value.DepthLevels[3].Price);
            Assert.AreEqual(1, depthMemoryImage.AskDepths.First().Value.DepthLevels[3].OrderCount);

            DepthLevel depthLevel498 = depth.FindLevel(new Price(498), OrderSide.Sell, depth.AskLevels);

            depth.EraseLevel(depthLevel498, OrderSide.Sell);
            DepthLevel depthLevel496 = depth.FindLevel(new Price(496), OrderSide.Sell, depth.AskLevels);

            depth.EraseLevel(depthLevel496, OrderSide.Sell);
            DepthLevel depthLevel493 = depth.FindLevel(new Price(493), OrderSide.Sell, depth.AskLevels);

            depth.EraseLevel(depthLevel493, OrderSide.Sell);
            DepthLevel depthLevel491 = depth.FindLevel(new Price(491), OrderSide.Sell, depth.AskLevels);

            depth.EraseLevel(depthLevel491, OrderSide.Sell);

            // Checks first and last bid prices
            Assert.AreEqual(498, depth.BidLevels[0].Price.Value);
            Assert.AreEqual(491, depth.BidLevels[3].Price.Value);
            // Checks first and last ask prices
            Assert.AreEqual(null, depth.AskLevels[0].Price);
            Assert.AreEqual(null, depth.AskLevels[3].Price);

            depthMemoryImage.OnDepthArrived(depth);
            Assert.AreEqual(1, depthMemoryImage.BidDepths.Count());
            Assert.AreEqual(1, depthMemoryImage.AskDepths.Count());

            // Bids in DepthMemoryImage
            Assert.AreEqual(498, depthMemoryImage.BidDepths.First().Value.DepthLevels[0].Price);
            Assert.AreEqual(496, depthMemoryImage.BidDepths.First().Value.DepthLevels[1].Price);
            Assert.AreEqual(493, depthMemoryImage.BidDepths.First().Value.DepthLevels[2].Price);
            Assert.AreEqual(491, depthMemoryImage.BidDepths.First().Value.DepthLevels[3].Price);
            // Asks in DepthMemoryImage
            Assert.IsNull(depthMemoryImage.AskDepths.First().Value.DepthLevels[0]);
            Assert.IsNull(depthMemoryImage.AskDepths.First().Value.DepthLevels[1]);
            Assert.IsNull(depthMemoryImage.AskDepths.First().Value.DepthLevels[2]);
            Assert.IsNull(depthMemoryImage.AskDepths.First().Value.DepthLevels[3]);
        }