Beispiel #1
0
        private TradeLog MakeMatch(BidOrder bid, AskOrder ask)
        {
            decimal price  = bid.Date < ask.Date ? bid.Price : ask.Price;
            decimal volume = bid.Remain() <= ask.Remain() ? bid.Remain() : ask.Remain();
            var     taker  = bid.Date < ask.Date ? TradeParty.Seller : TradeParty.Buyer;

            bid.Volume += volume;
            ask.Volume += volume;

            if (ask.Remain() == 0)
            {
                TradeQueue.Instance.Remove(ask);
            }

            if (bid.Remain() == 0)
            {
                TradeQueue.Instance.Remove(bid);
            }

            var log = new TradeLog
            {
                Ask    = ask,
                Bid    = bid,
                Price  = price,
                Volume = volume,
                Taker  = taker,
                Time   = DateTime.Now
            };

            TradeLogger.Instance.Update(log);

            return(log);
        }
Beispiel #2
0
        public void Should_Do_Success_WhenBidOrderPartialTransaction()
        {
            var bid  = new BidOrder(10001, 10, 100);
            var ask1 = new AskOrder(10002, 9, 50);
            var ask2 = new AskOrder(10003, 9, 45);

            TradeQueue.Instance.Enqueue(bid);
            TradeQueue.Instance.Enqueue(ask1);
            TradeQueue.Instance.Enqueue(ask2);

            var result = maker.Do();

            Assert.AreEqual(TradeStatus.None, result.Status);

            result = maker.Do();
            Assert.AreEqual(TradeStatus.Success, result.Status);
            Assert.AreEqual(1, result.Logs.Count);
            Assert.AreEqual(10M, result.Logs[0].Price);
            Assert.AreEqual(50, result.Logs[0].Volume);
            Assert.AreEqual(TradeParty.Seller, result.Logs[0].Taker);

            result = maker.Do();
            Assert.AreEqual(TradeStatus.Success, result.Status);
            Assert.AreEqual(1, result.Logs.Count);
            Assert.AreEqual(10M, result.Logs[0].Price);
            Assert.AreEqual(45, result.Logs[0].Volume);
            Assert.AreEqual(TradeParty.Seller, result.Logs[0].Taker);

            Assert.AreEqual(5, bid.Remain());
        }
Beispiel #3
0
        private TradeResult MakeMatch(BidOrder bid, List <AskOrder> asks)
        {
            asks = asks.OrderBy(e => e.Price).ThenBy(e => e.Date).ToList();

            var items = new List <TradeLog>();

            foreach (var ask in asks)
            {
                if (bid.Remain() == 0)
                {
                    break;
                }

                var item = MakeMatch(bid, ask);

                items.Add(item);
            }

            return(items.Count > 0 ? TradeResult.Success(items) : TradeResult.None());
        }