/// <summary>
        /// Calculate ticker info from recent trade and update it
        /// </summary>
        /// <param name="trade"></param>
        public void CalculateTickerInfo(Trade trade)
        {
            var tickerReadModel = _tickerInfoRepository.GetTickerInfoByCurrencyPair(trade.CurrencyPair);

            if (tickerReadModel == null)
            {
                TickerInfoReadModel model = new TickerInfoReadModel(trade.CurrencyPair, trade.ExecutionPrice.Value, trade.ExecutedVolume.Value);
                //model.CurrencyPair = trade.CurrencyPair;
                _persistanceRepository.SaveOrUpdate(model);
            }
            else
            {
                object[] todays = CalculateTodaysData(trade);
                log.Debug("Recevied today:" + todays);
                object[] last24Hours = Calculate24HoursData(trade);
                log.Debug("Received 24Hours:" + last24Hours);
                decimal openingPrice          = 0;
                decimal lastTradePrice        = 0;
                decimal lastTradeVolume       = 0;
                IList <TradeReadModel> trades = _tradeRepository.GetTradesBetweenDates(trade.ExecutionTime, DateTime.Today, trade.CurrencyPair);
                if (trades != null)
                {
                    if (trades.Count > 0)
                    {
                        log.Debug("Total Trades=" + trades.Count);
                        openingPrice    = trades[trades.Count - 1].Price;
                        lastTradePrice  = trades[0].Price;
                        lastTradeVolume = trades[0].Volume;
                    }
                }
                tickerReadModel.UpdateTickerInfo(todays, last24Hours, openingPrice, lastTradePrice, lastTradeVolume);
                _persistanceRepository.SaveOrUpdate(tickerReadModel);
            }
        }
        /// <summary>
        /// Method that wil append get ticker info and append bbo to it
        /// </summary>
        /// <param name="currenyPair"></param>
        /// <returns></returns>
        public TickerInfoReadModel GetTickerInfo(string currenyPair)
        {
            TickerInfoReadModel model             = _tickerInfoRepository.GetTickerInfoByCurrencyPair(currenyPair);
            BBORepresentation   bboRepresentation = _bboMemoryImage.BBORepresentationList.Contains(currenyPair);

            if (bboRepresentation != null)
            {
                model.UpdateBboInTickerInfo(bboRepresentation);
            }
            return(model);
        }
Exemple #3
0
        public void VerifyTickerInfoCalculations_WhenANewTradeIsArrived_NewUpdatedTickerInfoShouldGetSaved()
        {
            Order buyOrder = OrderFactory.CreateOrder("123", "XBTUSD", "limit", "buy", 10, 100,
                                                      new StubbedOrderIdGenerator());
            Order sellOrder = OrderFactory.CreateOrder("1234", "XBTUSD", "limit", "sell", 10, 100,
                                                       new StubbedOrderIdGenerator());

            DateTime dateTime = DateTime.Now.AddSeconds(-1 * DateTime.Now.Second);
            Trade    trade5   = new Trade(new TradeId("1"), "XBTUSD", new Price(2), new Volume(10), dateTime.AddDays(-1),
                                          buyOrder, sellOrder);
            Trade trade6 = new Trade(new TradeId("2"), "XBTUSD", new Price(3), new Volume(5), dateTime.AddDays(-1).AddMinutes(1),
                                     buyOrder, sellOrder);
            Trade trade1 = new Trade(new TradeId("3"), "XBTUSD", new Price(10), new Volume(10), dateTime.AddSeconds(10),
                                     buyOrder, sellOrder);
            Trade trade2 = new Trade(new TradeId("4"), "XBTUSD", new Price(15), new Volume(15), dateTime.AddSeconds(15),
                                     buyOrder, sellOrder);
            Trade trade3 = new Trade(new TradeId("5"), "XBTUSD", new Price(20), new Volume(5), dateTime.AddSeconds(20),
                                     buyOrder, sellOrder);
            Trade trade4 = new Trade(new TradeId("6"), "XBTUSD", new Price(5), new Volume(10), dateTime.AddSeconds(40),
                                     buyOrder, sellOrder);

            OutputDisruptor.Publish(trade5);
            _manualResetEvent.WaitOne(4000);
            OutputDisruptor.Publish(trade6);
            _manualResetEvent.WaitOne(4000);
            OutputDisruptor.Publish(trade1);
            _manualResetEvent.WaitOne(4000);
            OutputDisruptor.Publish(trade2);
            _manualResetEvent.WaitOne(4000);
            OutputDisruptor.Publish(trade3);
            _manualResetEvent.WaitOne(4000);
            OutputDisruptor.Publish(trade4);
            _manualResetEvent.WaitOne(10000);

            TickerInfoReadModel model = _tickerInfoRepository.GetTickerInfoByCurrencyPair("XBTUSD");

            Assert.NotNull(model);
            Assert.AreEqual(model.CurrencyPair, "XBTUSD");
            Assert.AreEqual(model.TradePrice, 5);
            Assert.AreEqual(model.TradeVolume, 10);
            Assert.AreEqual(model.OpeningPrice, 10);
            Assert.AreEqual(model.TodaysHigh, 20);
            Assert.AreEqual(model.Last24HoursHigh, 20);
            Assert.AreEqual(model.TodaysLow, 5);
            Assert.AreEqual(model.Last24HoursLow, 3);
            Assert.AreEqual(model.TodaysVolume, 40);
            Assert.AreEqual(model.Last24HourVolume, 45);
            Assert.AreEqual(model.TodaysTrades, 4);
            Assert.AreEqual(model.Last24HourTrades, 5);
            Assert.AreEqual(model.TodaysVolumeWeight, 11.875m);
            Assert.AreEqual(model.Last24HourVolumeWeight, 10.88889m);
        }
        public void SendSomeOrders_IfOrdersAreMatching_OhlcAndTickerInfoWillBeFormed()
        {
            // Get the context
            // IApplicationContext applicationContext = ContextRegistry.GetContext();

            // Get the instance through Spring configuration
            OrderController orderController = (OrderController)_applicationContext["OrderController"];

            orderController.Request = new HttpRequestMessage(HttpMethod.Post, "");
            orderController.Request.Headers.Add("Auth", "123456789");

            ManualResetEvent  manualResetEvent = new ManualResetEvent(false);
            IHttpActionResult orderHttpResult  = orderController.CreateOrder(new CreateOrderParam()
            {
                Pair   = "XBTUSD",
                Price  = 491,
                Volume = 100,
                Side   = "buy",
                Type   = "limit"
            });

            OkNegotiatedContentResult <NewOrderRepresentation> orderRepresentationContent = (OkNegotiatedContentResult <NewOrderRepresentation>)orderHttpResult;

            Assert.IsNotNull(orderRepresentationContent.Content);
            manualResetEvent.Reset();
            manualResetEvent.WaitOne(3000);
            orderController.CreateOrder(new CreateOrderParam()
            {
                Pair   = "XBTUSD",
                Price  = 492,
                Volume = 300,
                Side   = "buy",
                Type   = "limit"
            });
            manualResetEvent.Reset();
            manualResetEvent.WaitOne(3000);
            orderController.CreateOrder(new CreateOrderParam()
            {
                Pair   = "XBTUSD",
                Price  = 492,
                Volume = 1000,
                Side   = "sell",
                Type   = "limit"
            });
            manualResetEvent.Reset();
            manualResetEvent.WaitOne(3000);
            orderController.CreateOrder(new CreateOrderParam()
            {
                Pair   = "XBTUSD",
                Price  = 492,
                Volume = 900,
                Side   = "buy",
                Type   = "limit"
            });
            manualResetEvent.Reset();
            manualResetEvent.WaitOne(3000);
            orderController.CreateOrder(new CreateOrderParam()
            {
                Pair   = "XBTUSD",
                Price  = 498,
                Volume = 800,
                Side   = "sell",
                Type   = "limit"
            });
            manualResetEvent.Reset();
            manualResetEvent.WaitOne(3000);
            orderController.CreateOrder(new CreateOrderParam()
            {
                Pair   = "XBTUSD",
                Price  = 497,
                Volume = 700,
                Side   = "sell",
                Type   = "limit"
            });
            manualResetEvent.Reset();
            manualResetEvent.WaitOne(20000);
            MarketController  marketController           = (MarketController)_applicationContext["MarketController"];
            IHttpActionResult tickerinfo                 = marketController.TickerInfo("XBTUSD");
            OkNegotiatedContentResult <object> readmodel = (OkNegotiatedContentResult <object>)tickerinfo;

            Assert.NotNull(readmodel);
            TickerInfoReadModel model = readmodel.Content as TickerInfoReadModel;

            Assert.NotNull(model);
            Assert.AreEqual(model.CurrencyPair, "XBTUSD");
            Assert.AreEqual(model.TradePrice, 492);
            Assert.AreEqual(model.TradeVolume, 700);
            Assert.AreEqual(model.OpeningPrice, 492);
            Assert.AreEqual(model.TodaysHigh, 492);
            Assert.AreEqual(model.Last24HoursHigh, 492);
            Assert.AreEqual(model.TodaysLow, 492);
            Assert.AreEqual(model.Last24HoursLow, 492);
            Assert.AreEqual(model.TodaysVolume, 1000);
            Assert.AreEqual(model.Last24HourVolume, 1000);
            Assert.AreEqual(model.TodaysTrades, 2);
            Assert.AreEqual(model.Last24HourTrades, 2);
            Assert.AreEqual(model.TodaysVolumeWeight, 492);
            Assert.AreEqual(model.Last24HourVolumeWeight, 492);
            Assert.AreEqual(model.AskPrice, 497);
            Assert.AreEqual(model.AskVolume, 700);
            Assert.AreEqual(model.BidPrice, 492);
            Assert.AreEqual(model.BidVolume, 200);

            IHttpActionResult ohlcActionResult = marketController.OhlcInfo("XBTUSD");
            OkNegotiatedContentResult <OhlcRepresentation> representation = (OkNegotiatedContentResult <OhlcRepresentation>)ohlcActionResult;

            Assert.NotNull(representation);
            OhlcRepresentation ohlc = representation.Content as OhlcRepresentation;

            Assert.NotNull(ohlc);
            object[] ohlcValues = ohlc.OhlcInfo[0] as object[];
            Assert.AreEqual(ohlc.PairName, "XBTUSD");
            Assert.AreEqual(ohlcValues[0], 492); //open
            Assert.AreEqual(ohlcValues[1], 492); //high
            Assert.AreEqual(ohlcValues[2], 492); //low
            Assert.AreEqual(ohlcValues[3], 492); //close
        }