public TradeReadModel GetByIdAndTraderId(string traderId, string tradeId) { TradeReadModel model = CurrentSession.Get <TradeReadModel>(tradeId); if (model.BuyTraderId == traderId || model.SellTraderId == traderId) { return(model); } throw new InvalidOperationException("Not Authorized"); }
private void AssertAreEqual(TradeReadModel expected, TradeReadModel actual) { Assert.AreEqual(expected.TradeId, actual.TradeId); Assert.AreEqual(expected.BuyTraderId, actual.BuyTraderId); Assert.AreEqual(expected.SellTraderId, actual.SellTraderId); Assert.AreEqual(expected.Volume, actual.Volume); Assert.AreEqual(expected.BuyOrderId, actual.BuyOrderId); Assert.AreEqual(expected.SellOrderId, actual.SellOrderId); Assert.AreEqual(expected.Price, actual.Price); //Assert.AreEqual(expected.ExecutionDateTime, actual.ExecutionDateTime); Assert.AreEqual(expected.CurrencyPair, actual.CurrencyPair); }
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); }
/// <summary> /// Trade Details /// </summary> /// <param name="traderId"></param> /// <param name="tradeId"></param> /// <returns></returns> public TradeDetailsRepresentation GetTradeDetails(string traderId, string tradeId) { TradeReadModel model = _tradeRepository.GetByIdAndTraderId(traderId, tradeId); OrderReadModel buyOrder = _orderRepository.GetOrderById(model.BuyOrderId); OrderReadModel sellOrder = _orderRepository.GetOrderById(model.SellOrderId); if (buyOrder.TraderId == traderId && sellOrder.TraderId == traderId) { OrderReadModel fillingOrder = buyOrder.DateTime > sellOrder.DateTime ? buyOrder : sellOrder; return(new TradeDetailsRepresentation(fillingOrder, model.ExecutionDateTime, model.Price, model.Volume, model.TradeId)); } if (buyOrder.TraderId == traderId) { return(new TradeDetailsRepresentation(buyOrder, model.ExecutionDateTime, model.Price, model.Volume, model.TradeId)); } return(new TradeDetailsRepresentation(sellOrder, model.ExecutionDateTime, model.Price, model.Volume, model.TradeId)); }
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); }
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"); }
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])); }
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); }