public void UpdateAsk(Ask ask) { ThreadSafeAsk tAsk; if (OpenAsks.TryGetValue(ask.AskID, out tAsk)) { if (ask.SellQuantity > 0 || ask.BuyQuantity > 0) { tAsk.Update(ask, AskRepository); ExecuteAsk(tAsk); } else { OpenAsks.TryRemove(ask.AskID, out tAsk); foreach (var buyID in tAsk.CommodityBuyID) { foreach (var sellID in tAsk.CommoditySellID) { OpenAsksByBuyerCommdoity[buyID][sellID].TryRemove(ask.AskID, out tAsk); } } } } }
private IEnumerable <Order> ExecuteAsk(ThreadSafeAsk ask, int maxLegDepth = 5) { long orderQuantity; var orders = GetQuote(ask, out orderQuantity, maxLegDepth, true); ThreadSafeAsk tAsk; if (ask.AskID == 0 || !OpenAsks.TryGetValue(ask.AskID, out tAsk)) { this.AskRepository.AddAsk(ask.Ask); tAsk = CreateAsk(ask); this.OpenAsks[ask.AskID] = tAsk; foreach (var buyID in tAsk.CommodityBuyID) { foreach (var sellID in tAsk.CommoditySellID) { this.OpenAsksByBuyerCommdoity[buyID][sellID][ask.AskID] = tAsk; } } } List <Ask> orderLegAsks = new List <Ask>(); List <AskLeg> legs = new List <AskLeg>(); if (orders != null) { if (!tAsk.FillOrder(orderQuantity)) { orders = null; } tAsk.RealizeLockExecuteQuantiy(); if (tAsk.AskQuantity <= 0) { if (this.OpenAsks.TryRemove(tAsk.AskID, out tAsk)) { foreach (var buyID in tAsk.CommodityBuyID) { foreach (var sellID in tAsk.CommoditySellID) { this.OpenAsksByBuyerCommdoity[buyID][sellID].TryRemove(tAsk.AskID, out tAsk); } } } } if (orders != null) { foreach (var order in orders) { order.AskID = tAsk.AskID; order.OrderID = Guid.NewGuid(); foreach (var orderLeg in order.OrderLegs) { orderLeg.OrderID = order.OrderID; orderLeg.OrderLegID = Guid.NewGuid(); ThreadSafeAsk orderAsk; if (OpenAsks.TryGetValue(orderLeg.AskID, out orderAsk)) { orderAsk.RealizeLock(orderAsk.IsBuy ? orderLeg.SellQuantity : orderLeg.BuyQuantity); if (orderAsk.Legs.Count > 0) { var leg = orderAsk.Legs.Where(l => orderAsk.IsBuy ? l.CommodityID == orderLeg.CommodityBuyID : l.CommodityID == orderLeg.CommoditySellID).Single(); leg.RealizeLock(orderAsk.IsBuy ? orderLeg.BuyQuantity : orderLeg.SellQuantity); legs.Add(leg.Leg); } if (orderAsk.AskQuantity <= 0) { if (this.OpenAsks.TryRemove(orderAsk.AskID, out orderAsk)) { foreach (var buyID in tAsk.CommodityBuyID) { foreach (var sellID in tAsk.CommoditySellID) { this.OpenAsksByBuyerCommdoity[buyID][sellID].TryRemove(tAsk.AskID, out orderAsk); } } } if (orderAsk != null) { DisposeAsk(orderAsk); } } if (orderAsk != null) { orderLegAsks.Add(orderAsk.Ask); } } } } } } else { tAsk.RealizeLockExecuteQuantiy(); } AskRepository.ExecuteAsk(ask.Ask, orders, orderLegAsks, legs); return(orders); }