Example #1
0
        public static void HandleGFDTrade(BuyPriceBookList buyList, SellPriceBookList sellList)
        {
            var buyBookPeek  = buyList.Peek();
            var sellBookPeek = sellList.Peek();

            if (buyBookPeek == null || sellBookPeek == null)
            {
                return;
            }

            if (buyBookPeek.Price >= sellBookPeek.Price)
            {
                var buy  = buyList.GetFirstBuyBook();
                var sell = sellList.GetFirstBuyBook();

                var quantityTraded = Math.Min(buy.Quantity, sell.Quantity);

                if (buyBookPeek.CurrentTime < sellBookPeek.CurrentTime)
                {
                    Console.WriteLine($"TRADE {buy.OrderId} {buy.Price} {quantityTraded} " +
                                      $"{sell.OrderId} {sell.Price} {quantityTraded}");
                }
                else
                {
                    Console.WriteLine($"TRADE {sell.OrderId} {sell.Price} {quantityTraded} " +
                                      $"{buy.OrderId} {buy.Price} {quantityTraded}");
                }

                if (buy.Quantity - quantityTraded > 0)
                {
                    buy.Quantity = buy.Quantity - quantityTraded;
                    buyList.Add(buy.OrderId, buy);
                }

                if (sell.Quantity - quantityTraded > 0)
                {
                    sell.Quantity = sell.Quantity - quantityTraded;
                    sellList.Add(sell.OrderId, sell);
                }

                HandleGFDTrade(buyList, sellList);
            }
            else
            {
                return;
            }
        }
Example #2
0
        public static void AddBuySellTransaction(BuyPriceBookList buyList, SellPriceBookList sellList, PriceBook book, string priceBookType)
        {
            if (book.Price <= 0 || book.Quantity <= 0 || string.IsNullOrWhiteSpace(book.OrderId))
            {
                return;
            }

            if (priceBookType == "BUY")
            {
                buyList.Add(book.OrderId, book);
            }

            if (priceBookType == "SELL")
            {
                sellList.Add(book.OrderId, book);
            }
        }
Example #3
0
        static void Somethind(string[] args)
        {
            var list = new List <Sample>();

            for (int i = 0; i < int.MaxValue; i++)
            {
                list.Add(new Sample {
                    A = i, B = i
                });
            }

            var a        = "";
            var b        = "asa";
            var c        = a ?? b;
            var buyList  = new BuyPriceBookList();
            var sellList = new SellPriceBookList();

            while (true)
            {
                var str = Console.ReadLine();
                RunOrderMatchingEngine(str, buyList, sellList);
            }
        }
Example #4
0
 public static void Print(BuyPriceBookList buyList, SellPriceBookList sellList)
 {
     sellList.Dump();
     buyList.Dump();
 }
Example #5
0
        public static void RunOrderMatchingEngine(string str, BuyPriceBookList buyList, SellPriceBookList sellList)
        {
            var strArray = str.Split(' ');

            switch (strArray[0])
            {
            case "BUY":
            {
                var orderId = strArray[4];
                var book    = PriceBook.CreatePriceBook(strArray[1], int.Parse(strArray[2]), int.Parse(strArray[3]), strArray[4]);
                //buyList.Add(orderId, book);

                AddBuySellTransaction(buyList, sellList, book, "BUY");

                HandleGFDTrade(buyList, sellList);
                if (book.OrderType == OrderType.IOC)
                {
                    buyList.Remove(book.OrderId);
                }
                break;
            }

            case "SELL":
            {
                var orderId = strArray[4];
                var book    = PriceBook.CreatePriceBook(strArray[1], int.Parse(strArray[2]), int.Parse(strArray[3]), strArray[4]);
                //sellList.Add(orderId, book);

                AddBuySellTransaction(buyList, sellList, book, "SELL");
                HandleGFDTrade(buyList, sellList);
                if (book.OrderType == OrderType.IOC)
                {
                    sellList.Remove(book.OrderId);
                }
                break;
            }

            case "CANCEL":
            {
                var orderId = strArray[1];
                if (buyList.ContainsKey(orderId))
                {
                    buyList.Remove(orderId);
                }

                if (sellList.ContainsKey(orderId))
                {
                    sellList.Remove(orderId);
                }

                break;
            }

            case "MODIFY":
            {
                var orderId     = strArray[1];
                var buyOrSell   = strArray[2];
                var newPrice    = int.Parse(strArray[3]);
                var newQuantity = int.Parse(strArray[4]);

                PriceBook book = null;

                if (buyList.ContainsKey(orderId))
                {
                    book = buyList.Get(orderId);
                    buyList.Remove(orderId);
                }

                if (sellList.ContainsKey(orderId))
                {
                    book = sellList.Get(orderId);
                    sellList.Remove(orderId);
                }

                book.CurrentTime = DateTime.Now;
                book.Price       = int.Parse(strArray[3]);
                book.Quantity    = int.Parse(strArray[4]);

                var newBook = PriceBook.CreatePriceBook("GFD", newPrice, newQuantity, orderId);

                AddBuySellTransaction(buyList, sellList, newBook, strArray[2]);
                HandleGFDTrade(buyList, sellList);
                break;
            }

            case "PRINT":
            {
                Print(buyList, sellList);
                break;
            }

            default:
                break;
            }
        }