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 CalculateAndPersistOhlc(Trade latestTrade)
        {
            DateTime ohlcdDateTime = latestTrade.ExecutionTime.AddSeconds(-1 * latestTrade.ExecutionTime.Second);
            //IList<TradeReadModel> trades=_tradeRepository.GetTradesBetweenDates(latestTrade.ExecutionTime,ohlcdDateTime);
            OhlcReadModel model = _ohlcRepository.GetOhlcByDateTime(ohlcdDateTime.AddMinutes(1));

            if (model == null)
            {
                //means it is 1st trade of that minute
                OhlcReadModel newOhlcReadModel = new OhlcReadModel(latestTrade.CurrencyPair, ohlcdDateTime.AddMinutes(1), latestTrade.ExecutionPrice.Value,
                                                                   latestTrade.ExecutionPrice.Value,
                                                                   latestTrade.ExecutionPrice.Value, latestTrade.ExecutionPrice.Value, latestTrade.ExecutedVolume.Value);
                _persistanceRepository.SaveOrUpdate(newOhlcReadModel);
            }
            else
            {
                // IList<TradeReadModel> trades = _tradeRepository.GetTradesBetweenDates(latestTrade.ExecutionTime,
                //   ohlcdDateTime);
                //decimal volume = CalculateVolume(trades);
                //update the ohlc
                model.UpdateOhlc(latestTrade);
                _persistanceRepository.SaveOrUpdate(model);
            }
        }