///<inheritdoc /> public Order AddOrder(Order orderToAdd) { if (_companyContext.GetCompany(orderToAdd.CompanySymbol) == null) { return(null); } var outstandingOrdersForSymbolInPriceRange = _orderContext.GetOutstandingOrders() .Where(x => x.CompanySymbol == orderToAdd.CompanySymbol) // Match orders for company which matches .Where(x => x.OrderType != orderToAdd.OrderType) // Only show buy orders if current is sell and vice versa .Where(x => x.MinOrderPrice <= orderToAdd.MaxOrderPrice && x.MaxOrderPrice >= orderToAdd.MinOrderPrice) // Price ranges overlap .OrderBy(x => x.Created); // Fill oldest orders first foreach (var outstandingOrder in outstandingOrdersForSymbolInPriceRange) { // Find the lower value of quantity remaining var numberOfShares = Math.Min(orderToAdd.QuantityRemaining, outstandingOrder.QuantityRemaining); // Update the remaining quantity on both orders outstandingOrder.QuantityRemaining -= numberOfShares; orderToAdd.QuantityRemaining -= numberOfShares; // Update existing order _orderContext.UpdateOrder(outstandingOrder); // Exit if no more can be filled if (orderToAdd.QuantityRemaining == 0) { break; } } return(_orderContext.AddOrder(orderToAdd)); }
///<inheritdoc /> public Company GetCompany(string companySymbol) { return(_companyContext.GetCompany(companySymbol)); }