Example #1
0
        private void PrintPriceBook(OrderCollection orders)
        {
            var sellPrices = new SortedDictionary <int, int>(new DescendingComparer <int>());
            var buyPrices  = new SortedDictionary <int, int>(new DescendingComparer <int>());

            foreach (var order in orders.AsEnumerable())
            {
                if (order.Type == OrderType.SELL)
                {
                    AddToOrderSummary(order, sellPrices);
                }
                else if (order.Type == OrderType.BUY)
                {
                    AddToOrderSummary(order, buyPrices);
                }
            }

            OnOutput?.Invoke("SELL:");
            foreach (var item in sellPrices)
            {
                OnOutput?.Invoke($"{item.Key} {item.Value}");
            }

            OnOutput?.Invoke("BUY:");
            foreach (var item in buyPrices)
            {
                OnOutput?.Invoke($"{item.Key} {item.Value}");
            }
        }
Example #2
0
        private void ApplyTrades(OrderCollection orders)
        {
            Order firstOrder, secondOrder;

            while (FindNextTrade(orders, out firstOrder, out secondOrder))
            {
                var tradedQuantity = Math.Min(firstOrder.Quantity, secondOrder.Quantity);

                orders.MergeOrders(firstOrder, secondOrder);

                OnTradeHappened?.Invoke(firstOrder.ID, firstOrder.Price, tradedQuantity,
                                        secondOrder.ID, secondOrder.Price, tradedQuantity);

                OnOutput?.Invoke($"{KW_TRADE} {firstOrder.TradeInfo(tradedQuantity)} {secondOrder.TradeInfo(tradedQuantity)}");
            }
        }
Example #3
0
        private bool FindNextTrade(OrderCollection orders, out Order firstOrder, out Order secondOrder)
        {
            firstOrder = secondOrder = null;

            var sortedBuys  = orders.SortedBuys().ToList();
            var sortedSells = orders.SortedSells().ToList();

            if (sortedBuys.Count == 0 || sortedSells.Count == 0)
            {
                return(false);
            }


            try
            {
                var maxBuy  = sortedBuys[0];
                var minSell = sortedSells[0];

                if (maxBuy.Price < minSell.Price)
                {
                    //we don't have a match at all in this situation
                    return(false);
                }


                if (orders.GetIndex(maxBuy.ID) < orders.GetIndex(minSell.ID)) //earlier orders have lower index
                {
                    firstOrder  = maxBuy;
                    secondOrder = minSell;
                }
                else
                {
                    firstOrder  = minSell;
                    secondOrder = maxBuy;
                }

                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return(false);
            }
        }