Ejemplo n.º 1
0
        /// <summary>
        /// Handles the OrderBook's event in case the state of an Order changes
        /// </summary>
        /// <param name="order"> </param>
        public void OnOrderChanged(Order order)
        {
            OutputDisruptor.Publish(order);

            // Raising Event to be handled by OrderEventListener
            OrderEvent.Raise(order);
            Log.Debug("Order change received and published. Order: " + order.ToString());
        }
Ejemplo n.º 2
0
 public void OnTrade(Trade trade)
 {
     if (_trades == null)
     {
         _trades = new TradeList(trade.CurrencyPair);
     }
     _trades.Add(trade);
     OutputDisruptor.Publish(trade);
     Log.Debug("Trade received: " + trade.ToString());
 }
        public void PublishOrderToOutputDisruptor_IfOrderIsConvertedToByteArray_ItShouldBeReceivedAndCastedToOrder()
        {
            Order order = OrderFactory.CreateOrder("1234", "XBTUSD", "market", "buy", 5, 0,
                                                   new StubbedOrderIdGenerator());

            //byte[] array = ObjectToByteArray(order);
            OutputDisruptor.Publish(order);
            _manualResetEvent.WaitOne(3000);
            Assert.NotNull(_receviedOrder);
            Assert.AreEqual(_receviedOrder, order);
        }
Ejemplo n.º 4
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 PublishDepthToDisruptor_IfDepthIsConvertedToByteArray_ItShouldBeReceivedAndCastedToDepth()
        {
            Depth depth = new Depth("XBTUSD", 3);

            depth.AddOrder(new Price(490), new Volume(100), OrderSide.Buy);
            depth.AddOrder(new Price(491), new Volume(100), OrderSide.Buy);
            depth.AddOrder(new Price(492), new Volume(200), OrderSide.Buy);
            //byte[] array = ObjectToByteArray(depth);
            OutputDisruptor.Publish(depth);
            _manualResetEvent.WaitOne(3000);
            Assert.NotNull(_receivedDepth);
            Assert.AreEqual(_receivedDepth.BidLevels[0].Price.Value, 492);
            Assert.AreEqual(_receivedDepth.BidLevels[1].Price.Value, 491);
            Assert.AreEqual(_receivedDepth.BidLevels[2].Price.Value, 490);
        }
        public void PublishOrderToOutputDisruptor_IfOrderListenerIsInitiated_ItShouldSaveInDatabase()
        {
            Order order = OrderFactory.CreateOrder("1234", "XBTUSD", "limit", "buy", 5, 10,
                                                   new StubbedOrderIdGenerator());

            OutputDisruptor.Publish(order);
            _manualResetEvent.WaitOne(5000);
            OrderReadModel receivedOrder = _orderRepository.GetOrderById(order.OrderId.Id.ToString());

            Assert.NotNull(receivedOrder);
            Assert.AreEqual(receivedOrder.OrderId, order.OrderId.Id.ToString());
            Assert.AreEqual(receivedOrder.Side, order.OrderSide.ToString());
            Assert.AreEqual(receivedOrder.Type, order.OrderType.ToString());
            Assert.AreEqual(receivedOrder.Price, order.Price.Value);
            Assert.AreEqual(receivedOrder.CurrencyPair, order.CurrencyPair);
        }
        public void PublishOrderBookToDisruptor_IfOrderBookIsConvertedToByteArray_ItShouldBeReceivedAndCastedToOrderBook()
        {
            LimitOrderBook limitOrderBook = new LimitOrderBook("XBTUSD");
            Order          buyOrder       = OrderFactory.CreateOrder("1234", "XBTUSD", "limit", "buy", 5, 10,
                                                                     new StubbedOrderIdGenerator());
            Order sellOrder = OrderFactory.CreateOrder("1234", "XBTUSD", "limit", "sell", 5, 11,
                                                       new StubbedOrderIdGenerator());

            limitOrderBook.PlaceOrder(buyOrder);
            limitOrderBook.PlaceOrder(sellOrder);
            //byte[] array = ObjectToByteArray(limitOrderBook);
            OutputDisruptor.Publish(limitOrderBook);
            _manualResetEvent.WaitOne(3000);
            Assert.NotNull(_receivedLimitOrderBook);
            Assert.AreEqual(_receivedLimitOrderBook.AskCount, 1);
            Assert.AreEqual(_receivedLimitOrderBook.BidCount, 1);
        }
        public void PublishTradeToOutputDisruptor_IfTradeIsConvertedToByteArray_ItShouldBeReceivedAndCastedToTrade()
        {
            Order buyOrder = OrderFactory.CreateOrder("1234", "XBTUSD", "market", "buy", 5, 0,
                                                      new StubbedOrderIdGenerator());
            Order sellOrder = OrderFactory.CreateOrder("1234", "XBTUSD", "market", "sell", 5, 0,
                                                       new StubbedOrderIdGenerator());
            //Trade trade=new Trade("XBTUSD",new Price(100),new Volume(10),DateTime.Now,buyOrder,sellOrder);
            Trade trade = TradeFactory.GenerateTrade("XBTUSD", new Price(100), new Volume(10), buyOrder, sellOrder);

            //byte[] array = ObjectToByteArray(trade);
            OutputDisruptor.Publish(trade);
            _manualResetEvent.WaitOne(3000);
            Assert.NotNull(_receivedTrade);
            Assert.AreEqual(_receivedTrade.TradeId.Id, trade.TradeId.Id);
            Assert.AreEqual(_receivedTrade.BuyOrder, buyOrder);
            Assert.AreEqual(_receivedTrade.SellOrder, sellOrder);
        }
        public void CheckBarFormation_WhenANewTradeIsArrived_NewUpdatedBarShouldGetSaved()
        {
            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    trade1   = new Trade(new TradeId("1"), "XBTUSD", new Price(10), new Volume(10), dateTime.AddSeconds(10), buyOrder, sellOrder);
            Trade    trade2   = new Trade(new TradeId("2"), "XBTUSD", new Price(15), new Volume(15), dateTime.AddSeconds(15), buyOrder, sellOrder);
            Trade    trade3   = new Trade(new TradeId("3"), "XBTUSD", new Price(20), new Volume(5), dateTime.AddSeconds(20), buyOrder, sellOrder);
            Trade    trade4   = new Trade(new TradeId("4"), "XBTUSD", new Price(5), new Volume(10), dateTime.AddSeconds(40), buyOrder, sellOrder);
            Trade    trade5   = new Trade(new TradeId("5"), "XBTUSD", new Price(2), new Volume(10), dateTime.AddMinutes(1), buyOrder, sellOrder);
            Trade    trade6   = new Trade(new TradeId("6"), "XBTUSD", new Price(10), new Volume(5), dateTime.AddMinutes(1.1), buyOrder, sellOrder);

            OutputDisruptor.Publish(trade1);
            OutputDisruptor.Publish(trade2);
            OutputDisruptor.Publish(trade3);
            OutputDisruptor.Publish(trade4);
            OutputDisruptor.Publish(trade5);
            OutputDisruptor.Publish(trade6);
            _manualResetEvent.WaitOne(10000);
            OhlcReadModel model  = _ohlcRepository.GetOhlcByDateTime(dateTime.AddMinutes(1));
            OhlcReadModel model2 = _ohlcRepository.GetOhlcByDateTime(dateTime.AddMinutes(2));

            //bar 1 verification(will form from trade 1-4)
            Assert.NotNull(model);
            Assert.AreEqual(model.High, 20);
            Assert.AreEqual(model.Open, 10);
            Assert.AreEqual(model.Low, 5);
            Assert.AreEqual(model.Close, 5);
            Assert.AreEqual(model.Volume, 40);
            Assert.AreEqual(model.TotalWeight, 475);
            Assert.AreEqual(model.AveragePrice, 11.875);

            //bar 2 verification(will form from trade 5-6)
            Assert.NotNull(model2);
            Assert.AreEqual(model2.High, 10);
            Assert.AreEqual(model2.Open, 2);
            Assert.AreEqual(model2.Low, 2);
            Assert.AreEqual(model2.Close, 10);
            Assert.AreEqual(model2.Volume, 15);
            Assert.AreEqual(model2.TotalWeight, 70);
            Assert.AreEqual(model2.AveragePrice, 4.66667);
        }
        public void PublishBboToDisruptor_IfBboIsConvertedToByteArray_ItShouldBeReceivedAndCastedToBbo()
        {
            DepthLevel askDepthLevel = new DepthLevel(new Price(491.32M));
            bool       addOrder1     = askDepthLevel.AddOrder(new Volume(2000));
            bool       addOrder2     = askDepthLevel.AddOrder(new Volume(1000));
            DepthLevel bidDepthLevel = new DepthLevel(new Price(491.32M));

            addOrder1 = bidDepthLevel.AddOrder(new Volume(2000));
            addOrder2 = bidDepthLevel.AddOrder(new Volume(1000));
            bool addOrder3 = bidDepthLevel.AddOrder(new Volume(3000));
            BBO  bbo       = new BBO("BTCUSD", bidDepthLevel, askDepthLevel);

            //byte[] array = ObjectToByteArray(bbo);
            OutputDisruptor.Publish(bbo);
            _manualResetEvent.WaitOne(3000);
            Assert.NotNull(_receivedBbo);
            Assert.AreEqual(_receivedBbo.BestAsk.OrderCount, 2);
            Assert.AreEqual(_receivedBbo.BestBid.OrderCount, 3);
        }
        public void PublishTradeToOutputDisruptor_IfTradeListenerIsInitiated_ItShouldSaveInDatabase()
        {
            Order buyOrder = OrderFactory.CreateOrder("123", "XBTUSD", "limit", "buy", 10, 100,
                                                      new StubbedOrderIdGenerator());
            Order sellOrder = OrderFactory.CreateOrder("1234", "XBTUSD", "limit", "sell", 10, 100,
                                                       new StubbedOrderIdGenerator());
            //Trade trade=new Trade("XBTUSD",new Price(100),new Volume(10),DateTime.Now,buyOrder,sellOrder);
            Trade trade = TradeFactory.GenerateTrade("XBTUSD", new Price(1000), new Volume(10), buyOrder, sellOrder);

            OutputDisruptor.Publish(trade);
            _manualResetEvent.WaitOne(5000);
            TradeReadModel model = _tradeRepository.GetById(trade.TradeId.Id.ToString());

            Assert.NotNull(model);
            Assert.AreEqual(model.BuyOrderId, buyOrder.OrderId.Id.ToString());
            Assert.AreEqual(model.SellOrderId, sellOrder.OrderId.Id.ToString());
            Assert.AreEqual(model.Price, 1000);
            Assert.AreEqual(model.CurrencyPair, "XBTUSD");
            Assert.AreEqual(model.BuyTraderId, "123");
            Assert.AreEqual(model.SellTraderId, "1234");
            Assert.AreEqual(model.Volume, 10);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Handles the event that signifies that the OrderBook has changed
 /// </summary>
 public void OnOrderBookChanged(LimitOrderBook orderBook)
 {
     OutputDisruptor.Publish(orderBook);
     Log.Debug("OrderBook changed for Currency pair: " + orderBook.CurrencyPair);
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Handles the OrderBook's event in case the state of an Order changes
        /// </summary>
        /// <param name="order"> </param>
        public void OnOrderChanged(Order order)
        {
            OutputDisruptor.Publish(order);

            Log.Debug("Order change received and published. Order: " + order.ToString());
        }
Ejemplo n.º 14
0
 public void OnDepthChanged(Depth depth)
 {
     //Publish to output disruptor
     OutputDisruptor.Publish(depth);
     Log.Debug("Depth changed for currency pair: " + depth.CurrencyPair);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Onn BBO changed event
 /// </summary>
 /// <param name="bbo"> </param>
 public void OnBBOChange(BBO bbo)
 {
     OutputDisruptor.Publish(bbo);
     Log.Debug("Best bid and offer received for currency pair: " + bbo.CurrencyPair);
 }
        public void PublishAllTypesToOutputDisruptor_IfAllTypesAreConvertedToByteArray_ItShouldReceivedAndProperlyCasted()
        {
            Order order = OrderFactory.CreateOrder("1234", "XBTUSD", "market", "buy", 5, 0,
                                                   new StubbedOrderIdGenerator());

            //byte[] array = ObjectToByteArray(order);
            OutputDisruptor.Publish(order);

            Order buyOrder = OrderFactory.CreateOrder("1234", "XBTUSD", "limit", "buy", 5, 10,
                                                      new StubbedOrderIdGenerator());
            Order sellOrder = OrderFactory.CreateOrder("1234", "XBTUSD", "limit", "sell", 5, 11,
                                                       new StubbedOrderIdGenerator());
            Trade trade = new Trade(new TradeId("123"), "XBTUSD", new Price(100), new Volume(10), DateTime.Now, buyOrder, sellOrder);

            //byte[] array1 = ObjectToByteArray(trade);
            OutputDisruptor.Publish(trade);

            LimitOrderBook limitOrderBook = new LimitOrderBook("XBTUSD");

            limitOrderBook.PlaceOrder(buyOrder);
            limitOrderBook.PlaceOrder(sellOrder);
            //byte[] array2 = ObjectToByteArray(limitOrderBook);
            OutputDisruptor.Publish(limitOrderBook);

            Depth depth = new Depth("XBTUSD", 3);

            depth.AddOrder(new Price(490), new Volume(100), OrderSide.Buy);
            depth.AddOrder(new Price(491), new Volume(100), OrderSide.Buy);
            depth.AddOrder(new Price(492), new Volume(200), OrderSide.Buy);
            //byte[] array3 = ObjectToByteArray(depth);
            OutputDisruptor.Publish(depth);

            DepthLevel askDepthLevel = new DepthLevel(new Price(491.32M));
            bool       addOrder1     = askDepthLevel.AddOrder(new Volume(2000));
            bool       addOrder2     = askDepthLevel.AddOrder(new Volume(1000));
            DepthLevel bidDepthLevel = new DepthLevel(new Price(491.32M));

            addOrder1 = bidDepthLevel.AddOrder(new Volume(2000));
            addOrder2 = bidDepthLevel.AddOrder(new Volume(1000));
            bool addOrder3 = bidDepthLevel.AddOrder(new Volume(3000));
            BBO  bbo       = new BBO("XBTUSD", bidDepthLevel, askDepthLevel);

            //byte[] array4 = ObjectToByteArray(bbo);
            OutputDisruptor.Publish(bbo);
            _manualResetEvent.WaitOne(3000);

            Assert.NotNull(_receviedOrder);
            Assert.AreEqual(_receviedOrder, order);

            Assert.NotNull(_receivedTrade);
            Assert.AreEqual(_receivedTrade.BuyOrder, buyOrder);
            Assert.AreEqual(_receivedTrade.SellOrder, sellOrder);

            Assert.NotNull(_receivedLimitOrderBook);
            Assert.AreEqual(_receivedLimitOrderBook.AskCount, 1);
            Assert.AreEqual(_receivedLimitOrderBook.BidCount, 1);

            Assert.NotNull(_receivedDepth);
            Assert.AreEqual(_receivedDepth.BidLevels[0].Price.Value, 492);
            Assert.AreEqual(_receivedDepth.BidLevels[1].Price.Value, 491);
            Assert.AreEqual(_receivedDepth.BidLevels[2].Price.Value, 490);

            Assert.NotNull(_receivedBbo);
            Assert.AreEqual(_receivedBbo.BestAsk.OrderCount, 2);
            Assert.AreEqual(_receivedBbo.BestBid.OrderCount, 3);
        }