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; } }
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); } }