public void GetOpenOrders_IfTraderIdIsProvided_RetrievedOrdersShouldBeDateTimeSortedAsDescending()
        {
            ManualResetEvent resetEvent = new ManualResetEvent(false);
            Order            order      = OrderFactory.CreateOrder("100", "XBTUSD", "limit", "buy", 5, 10,
                                                                   new StubbedOrderIdGenerator());

            resetEvent.WaitOne(2000);
            resetEvent.Reset();
            Order order1 = OrderFactory.CreateOrder("100", "XBTUSD", "limit", "buy", 5, 10,
                                                    new StubbedOrderIdGenerator());

            resetEvent.WaitOne(2000);
            resetEvent.Reset();
            Order order2 = OrderFactory.CreateOrder("100", "XBTUSD", "limit", "buy", 5, 10,
                                                    new StubbedOrderIdGenerator());

            resetEvent.WaitOne(2000);
            resetEvent.Reset();
            OrderReadModel model = ReadModelAdapter.GetOrderReadModel(order);

            _persistanceRepository.SaveOrUpdate(model);
            OrderReadModel model1 = ReadModelAdapter.GetOrderReadModel(order1);

            _persistanceRepository.SaveOrUpdate(model1);
            OrderReadModel model2 = ReadModelAdapter.GetOrderReadModel(order2);

            _persistanceRepository.SaveOrUpdate(model2);
            IList <OrderReadModel> getReadModel = _orderRepository.GetOpenOrders("100");

            Assert.NotNull(getReadModel);
            Assert.AreEqual(getReadModel.Count, 3);
            Assert.True(getReadModel[0].DateTime > getReadModel[1].DateTime);
            Assert.True(getReadModel[1].DateTime > getReadModel[2].DateTime);
        }
        public void ValidateCancelOrderCommand_IfOrderIdAndTraderIdIsValidAndOrderStatusIsNotCompleteOrCancelled_CommandWillBeValidated()
        {
            Order order = OrderFactory.CreateOrder("1234", "XBTUSD", "limit", "buy", 5, 10,
                                                   new StubbedOrderIdGenerator());
            OrderReadModel model = ReadModelAdapter.GetOrderReadModel(order);

            _persistanceRepository.SaveOrUpdate(model);
            CancelOrderCommand            command    = new CancelOrderCommand(order.OrderId, order.TraderId);
            ICancelOrderCommandValidation validation = new CancelOrderCommandValidation(_orderRepository);

            Assert.True(validation.ValidateCancelOrderCommand(command));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Receives trade when events are raised
        /// </summary>
        /// <param name="trade"></param>
        void OnTradeArrived(Trade trade)
        {
            _persistanceRepository.SaveOrUpdate(ReadModelAdapter.GetTradeReadModel(trade));
            _ohlcCalculation.CalculateAndPersistOhlc(trade);
            _tickerInfoCalculation.CalculateTickerInfo(trade);
            Tuple <string, string> currencies = CurrencySplitterService.SplitCurrencyPair(trade.CurrencyPair);

            // Update the balance on hte Funds BC
            _balanceValidationService.TradeExecuted(currencies.Item1, currencies.Item2, trade.ExecutedVolume.Value,
                                                    trade.ExecutionPrice.Value, trade.ExecutionTime, trade.TradeId.Id, trade.BuyOrder.TraderId.Id,
                                                    trade.SellOrder.TraderId.Id, trade.BuyOrder.OrderId.Id, trade.SellOrder.OrderId.Id);
        }
        public void ValidateCancelOrderCommand_IfOrderIdAndTraderIdIsValidAndOrderStatusIsCancelled_InvalidOperationException()
        {
            Order order = OrderFactory.CreateOrder("1234", "XBTUSD", "limit", "buy", 5, 10,
                                                   new StubbedOrderIdGenerator());

            order.OrderState = OrderState.Cancelled;
            OrderReadModel model = ReadModelAdapter.GetOrderReadModel(order);

            _persistanceRepository.SaveOrUpdate(model);
            CancelOrderCommand            command    = new CancelOrderCommand(order.OrderId, order.TraderId);
            ICancelOrderCommandValidation validation = new CancelOrderCommandValidation(_orderRepository);

            validation.ValidateCancelOrderCommand(command);
        }
        public void SaveOrderReadModel_IfStateIsAccepted_ClosingTimeShouldBeNull()
        {
            Order order = OrderFactory.CreateOrder("1234", "XBTUSD", "limit", "buy", 5, 10,
                                                   new StubbedOrderIdGenerator());
            string id = DateTime.Now.ToString();

            order.OrderState = OrderState.Accepted;
            OrderReadModel model = ReadModelAdapter.GetOrderReadModel(order);

            _persistanceRepository.SaveOrUpdate(model);
            OrderReadModel getReadModel = _orderRepository.GetOrderById(order.OrderId.Id.ToString());

            Assert.NotNull(getReadModel);
            AssertAreEqual(getReadModel, model);
            Assert.Null(getReadModel.ClosingDateTime);
        }
        public void SaveOrderReadModel_IfSaveOrUpdateMethodIsCalled_ItShouldGetSavedInTheDatabase()
        {
            Order order = OrderFactory.CreateOrder("1234", "XBTUSD", "limit", "buy", 5, 10,
                                                   new StubbedOrderIdGenerator());
            string id = DateTime.Now.ToString();

            order.OrderState = OrderState.Complete;
            OrderReadModel model = ReadModelAdapter.GetOrderReadModel(order);

            _persistanceRepository.SaveOrUpdate(model);
            OrderReadModel getReadModel = _orderRepository.GetOrderById(order.OrderId.Id.ToString());

            Assert.NotNull(getReadModel);
            AssertAreEqual(getReadModel, model);
            Assert.NotNull(getReadModel.ClosingDateTime);
        }
        public void GetClosedOrders_IfTraderIdIsProvided_ItShouldRetireveAllClosedOrdersOfTrader()
        {
            Order order = OrderFactory.CreateOrder("999", "XBTUSD", "limit", "buy", 5, 10,
                                                   new StubbedOrderIdGenerator());

            order.OrderState = OrderState.Complete;
            string         id    = DateTime.Now.ToString();
            OrderReadModel model = ReadModelAdapter.GetOrderReadModel(order);

            _persistanceRepository.SaveOrUpdate(model);
            IList <OrderReadModel> getReadModel = _orderRepository.GetClosedOrders("999");

            Assert.NotNull(getReadModel);
            Assert.AreEqual(getReadModel.Count, 1);
            AssertAreEqual(getReadModel[0], model);
        }
Exemplo n.º 8
0
        public void SaveTradeReadModel_IfSaveOrUpdateMethodIsCalled_ItShouldGetSavedInTheDatabase()
        {
            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);
            TradeReadModel model = ReadModelAdapter.GetTradeReadModel(trade);

            _persistanceRepository.SaveOrUpdate(model);
            TradeReadModel getSavedModel = _tradeRepository.GetById(trade.TradeId.Id.ToString());

            Assert.NotNull(getSavedModel);
            AssertAreEqual(getSavedModel, model);
        }
Exemplo n.º 9
0
        public void GetRecentTrades_IfRecentTradesRequestArrives_ItShouldReturnRecentTrades()
        {
            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);
            TradeReadModel model = ReadModelAdapter.GetTradeReadModel(trade);

            _persistanceRepository.SaveOrUpdate(model);
            IList <object> getTrades = _tradeRepository.GetRecentTrades("", "XBTUSD");

            Assert.NotNull(getTrades);
            Assert.AreEqual(getTrades.Count, 1);
            object[] received = getTrades[0] as object[];
            Assert.AreEqual(received[1], 100);
            Assert.AreEqual(received[2], 10);
        }
        /// <summary>
        /// Handles the event of change in orders
        /// </summary>
        /// <param name="order"></param>
        void OnOrderStatusChanged(Order order)
        {
            _persistanceRepository.SaveOrUpdate(ReadModelAdapter.GetOrderReadModel(order));

            // If the order has been cancelled, send the info to Funds BC so that the funds can be updated
            if (order.OrderState == OrderState.Cancelled)
            {
                // First split the curreny pair into base and quote currency
                Tuple <string, string> splittedCurrencyPair =
                    CurrencySplitterService.SplitCurrencyPair(order.CurrencyPair);
                if (!string.IsNullOrEmpty(splittedCurrencyPair.Item1) &&
                    !string.IsNullOrEmpty(splittedCurrencyPair.Item2))
                {
                    // Send to the Infrastructure service which will communicate cross Bounded Context
                    _balanceValidationService.OrderCancelled(splittedCurrencyPair.Item1, splittedCurrencyPair.Item2,
                                                             order.TraderId.Id, order.OrderSide.ToString(),
                                                             order.OrderId.Id,
                                                             order.OpenQuantity.Value, order.Price.Value);
                }
            }
        }
Exemplo n.º 11
0
        public void GetTradesOfTrader_IfTraderIdIsProvided_AllTradesOfTraderShouldReturn()
        {
            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);
            TradeReadModel model = ReadModelAdapter.GetTradeReadModel(trade);

            _persistanceRepository.SaveOrUpdate(model);
            //model.TradeId = DateTime.Now.Millisecond.ToString();
            var getTrades = _tradeRepository.GetTraderTradeHistory("1234");

            Assert.NotNull(getTrades);
            Assert.AreEqual(getTrades.Count, 1);
            object[] received = getTrades[0] as object[];
            Assert.AreEqual(received[2], 100);
            Assert.AreEqual(received[3], 10);
            Assert.AreEqual(received[4], "XBTUSD");
        }
Exemplo n.º 12
0
        public void GetTradesOfTrader_IfTraderIdIsProvided_AllTradesOfTraderShouldBeDateTimeSortedDesc()
        {
            Order buyOrder = OrderFactory.CreateOrder("1234", "XBTUSD", "market", "buy", 5, 0,
                                                      new StubbedOrderIdGenerator());
            Order sellOrder = OrderFactory.CreateOrder("1234", "XBTUSD", "market", "sell", 5, 0,
                                                       new StubbedOrderIdGenerator());
            ManualResetEvent resetEvent = new ManualResetEvent(false);
            //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);

            resetEvent.WaitOne(2000);
            resetEvent.Reset();
            Trade trade1 = TradeFactory.GenerateTrade("XBTUSD", new Price(100), new Volume(10), buyOrder, sellOrder);

            resetEvent.WaitOne(2000);
            resetEvent.Reset();
            Trade trade2 = TradeFactory.GenerateTrade("XBTUSD", new Price(100), new Volume(10), buyOrder, sellOrder);

            resetEvent.WaitOne(2000);
            resetEvent.Reset();
            TradeReadModel model = ReadModelAdapter.GetTradeReadModel(trade);

            _persistanceRepository.SaveOrUpdate(model);
            TradeReadModel model1 = ReadModelAdapter.GetTradeReadModel(trade1);

            _persistanceRepository.SaveOrUpdate(model1);
            TradeReadModel model2 = ReadModelAdapter.GetTradeReadModel(trade2);

            _persistanceRepository.SaveOrUpdate(model2);
            IList <object> getTrades = _tradeRepository.GetTraderTradeHistory("1234");

            Assert.NotNull(getTrades);
            Assert.AreEqual(getTrades.Count, 3);
            object[] received  = getTrades[0] as object[];
            object[] received1 = getTrades[1] as object[];
            object[] received2 = getTrades[2] as object[];
            Assert.True(Convert.ToDateTime(received[1]) > Convert.ToDateTime(received1[1]));
            Assert.True(Convert.ToDateTime(received1[1]) > Convert.ToDateTime(received2[1]));
        }