public IActionResult OrderDepthView() { var resultModel = new OrderDepthViewModel(); return(View(resultModel)); }
public async Task <IActionResult> OrderDepthView(OrderDepthViewModel orderForm) { using var client = new HttpClient(); var resultModel = new OrderDepthViewModel(); var res = await client.GetAsync("https://localhost:44346/api/stockorders"); // gets current order depth string str = await res.Content.ReadAsStringAsync(); List <OrderDepthViewModel> model = JsonConvert.DeserializeObject <List <OrderDepthViewModel> >(str); var historyResultModel = new OrderDepthViewResultModel { Histories = new List <OrderDepthViewModel>(model) }; // separate the buy-orders from sell-orders List <OrderDepthViewModel> buyOrders = model.FindAll( delegate(OrderDepthViewModel stock) { return(stock.BuySell == 0); } ); List <OrderDepthViewModel> sellOrders = model.FindAll( delegate(OrderDepthViewModel stock) { return(stock.BuySell == 1); } ); // grouping buy var ordersGroupedByPriceBuy = buyOrders.GroupBy(od => od.PriceLimit); List <OrderDepthViewModel> buyOrdersOut = new List <OrderDepthViewModel>(); foreach (var group in ordersGroupedByPriceBuy) { int counter = 0; OrderDepthViewModel odv = new OrderDepthViewModel(); foreach (var ord in group) { odv = ord; counter = counter + ord.StockCount; } odv.StockCount = counter; buyOrdersOut.Add(odv); } // grouping sell var ordersGroupedByPrice = sellOrders.GroupBy(od => od.PriceLimit); List <OrderDepthViewModel> sellOrdersOut = new List <OrderDepthViewModel>(); foreach (var group in ordersGroupedByPrice) { int counter = 0; OrderDepthViewModel odv = new OrderDepthViewModel(); foreach (var ord in group) { odv = ord; counter = counter + ord.StockCount; } odv.StockCount = counter; sellOrdersOut.Add(odv); } // sort the two lists by PriceLimit if (buyOrdersOut.Any()) { buyOrdersOut.Sort( delegate(OrderDepthViewModel s1, OrderDepthViewModel s2) { return(s2.PriceLimit.CompareTo(s1.PriceLimit)); } ); } // sort the sell-orders by PriceLimit if (sellOrdersOut.Any()) { sellOrdersOut.Sort( delegate(OrderDepthViewModel s1, OrderDepthViewModel s2) { return(s1.PriceLimit.CompareTo(s2.PriceLimit)); } ); } resultModel.OrderBuy = buyOrdersOut; resultModel.OrderSell = sellOrdersOut; resultModel.OrderDepthResult = historyResultModel; return(View(resultModel)); }