bool IOrderLogMarketDepthBuilder.Update(ExecutionMessage item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (item.ExecutionType != ExecutionTypes.OrderLog) { throw new ArgumentException(nameof(item)); } if (item.OrderPrice == 0) { return(false); } var changed = false; var quotes = item.Side == Sides.Buy ? _bids : _asks; try { if (item.IsOrderLogRegistered()) { if (item.OrderId != null) { var quote = quotes.SafeAdd(item.OrderPrice, key => new QuoteChange(item.Side, key, 0)); var id = item.OrderId.Value; if (item.OrderVolume != null) { var volume = item.OrderVolume.Value; if (_orders.TryGetValue(id, out var prevVolume)) { quote.Volume += (volume - prevVolume); _orders[id] = volume; } else { quote.Volume += volume; _orders.Add(id, volume); } changed = true; } } } else if (item.IsOrderLogMatched()) { if (item.OrderId != null) { var id = item.OrderId.Value; var volume = item.TradeVolume ?? item.OrderVolume; if (volume != null) { if (_orders.TryGetValue(id, out var prevVolume)) { var quote = quotes.TryGetValue(item.OrderPrice); if (quote != null) { quote.Volume -= volume.Value; if (quote.Volume <= 0) { quotes.Remove(item.OrderPrice); } } _orders[id] = prevVolume - volume.Value; changed = true; } } } } else if (item.IsOrderLogCanceled()) { if (item.OrderId != null) { var id = item.OrderId.Value; if (_orders.TryGetValue(id, out var prevVolume)) { var quote = quotes.TryGetValue(item.OrderPrice); if (quote != null) { quote.Volume -= prevVolume; if (quote.Volume <= 0) { quotes.Remove(item.OrderPrice); } } _orders.Remove(id); changed = true; } } } } finally { if (changed) { _depth.ServerTime = item.ServerTime; _depth.LocalTime = item.LocalTime; _depth.Bids = _bids.Values.ToArray(); _depth.Asks = _asks.Values.ToArray(); } } return(changed); }
QuoteChangeMessage IOrderLogMarketDepthBuilder.Update(ExecutionMessage item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (item.ExecutionType != ExecutionTypes.OrderLog) { throw new ArgumentException(nameof(item)); } if (item.OrderPrice == 0) { return(null); } _depth.ServerTime = item.ServerTime; _depth.LocalTime = item.LocalTime; QuoteChange?changedQuote = null; var quotes = item.Side == Sides.Buy ? _bids : _asks; if (item.IsOrderLogRegistered()) { if (item.OrderVolume != null) { QuoteChange ProcessRegister <T>(T id, Dictionary <T, decimal> orders) { var quote = quotes.SafeAdd(item.OrderPrice, key => new QuoteChange(key, 0)); var volume = item.OrderVolume.Value; if (orders.TryGetValue(id, out var prevVolume)) { quote.Volume += (volume - prevVolume); orders[id] = volume; } else { quote.Volume += volume; orders.Add(id, volume); } quotes[item.OrderPrice] = quote; return(quote); } if (item.OrderId != null) { changedQuote = ProcessRegister(item.OrderId.Value, _ordersByNum); } else if (!item.OrderStringId.IsEmpty()) { changedQuote = ProcessRegister(item.OrderStringId, _ordersByString); } } } else if (item.IsOrderLogMatched()) { var volume = item.TradeVolume ?? item.OrderVolume; if (volume != null) { QuoteChange?ProcessMatched <T>(T id, Dictionary <T, decimal> orders) { if (orders.TryGetValue(id, out var prevVolume)) { orders[id] = prevVolume - volume.Value; if (quotes.TryGetValue(item.OrderPrice, out var quote)) { quote.Volume -= volume.Value; if (quote.Volume <= 0) { quotes.Remove(item.OrderPrice); } quotes[item.OrderPrice] = quote; return(quote); } } return(null); } if (item.OrderId != null) { changedQuote = ProcessMatched(item.OrderId.Value, _ordersByNum); } else if (!item.OrderStringId.IsEmpty()) { changedQuote = ProcessMatched(item.OrderStringId, _ordersByString); } } } else if (item.IsOrderLogCanceled()) { QuoteChange?ProcessCanceled <T>(T id, Dictionary <T, decimal> orders) { if (orders.TryGetAndRemove(id, out var prevVolume)) { if (quotes.TryGetValue(item.OrderPrice, out var quote)) { quote.Volume -= prevVolume; if (quote.Volume <= 0) { quotes.Remove(item.OrderPrice); } quotes[item.OrderPrice] = quote; return(quote); } } return(null); } if (item.OrderId != null) { changedQuote = ProcessCanceled(item.OrderId.Value, _ordersByNum); } else if (!item.OrderStringId.IsEmpty()) { changedQuote = ProcessCanceled(item.OrderStringId, _ordersByString); } } if (changedQuote == null) { return(null); } var increment = new QuoteChangeMessage { ServerTime = item.ServerTime, LocalTime = item.LocalTime, SecurityId = _depth.SecurityId, State = QuoteChangeStates.Increment, }; var q = changedQuote.Value; if (item.Side == Sides.Buy) { increment.Bids = new[] { q } } ; else { increment.Asks = new[] { q } }; return(increment); } }