/// <summary> /// Initializes the new instance internal market stream reference. /// </summary> /// <param name="marketName">Market name.</param> /// <param name="exchangeState">Exchange initial states.</param> /// <param name="exchangeDelta">Exchange delta stream identifier.</param> /// <param name="marketDelta">Market delta stream identifier.</param> public MarketStream(string marketName, ExchangeStateProvider exchangeState, int exchangeDelta, int marketDelta) { Name = marketName; UpdateExchangeDelta(exchangeState); ExchangeDeltaStream = exchangeDelta; MarketDeltaStream = marketDelta; }
/// <summary> /// Updates the received changes in exchange data. /// </summary> /// <param name="exchangeDelta">Exchange delta stream data.</param> public void UpdateExchangeDelta(ExchangeStateProvider exchangeDelta) { if (exchangeDelta != null && Name == exchangeDelta.MarketName) { _lock.EnterWriteLock(); try { if (exchangeDelta.Bids is IList <MarketOrderEntry> bids) { foreach (var bid in bids) { switch (bid.Frame) { case OrderBookFrame.Create: _bidsReferentTotal += bid.Quantity * bid.Rate; _bidsCurrencyTotal += bid.Quantity; _bids.Add(bid); break; case OrderBookFrame.Update: if (_bids.FindIndex(x => x.Rate == bid.Rate) is var update && update >= 0) { var delta = bid.Quantity - _bids[update].Quantity; _bidsReferentTotal += delta * bid.Rate; _bidsCurrencyTotal += delta; _bids[update] = bid; } break; case OrderBookFrame.Remove: if (_bids.FindIndex(x => x.Rate == bid.Rate) is var remove && remove >= 0) { var delta = bid.Quantity - _bids[remove].Quantity; _bidsReferentTotal += delta * bid.Rate; _bidsCurrencyTotal += delta; _bids.RemoveAt(remove); } break; } } } if (exchangeDelta.Asks is IList <MarketOrderEntry> asks) { foreach (var ask in asks) { switch (ask.Frame) { case OrderBookFrame.Create: _asksReferentTotal += ask.Quantity * ask.Rate; _asksCurrencyTotal += ask.Quantity; _asks.Add(ask); break; case OrderBookFrame.Update: if (_asks.FindIndex(x => x.Rate == ask.Rate) is var update && update >= 0) { var delta = ask.Quantity - _asks[update].Quantity; _asksReferentTotal += delta * ask.Rate; _asksCurrencyTotal += delta; _asks[update] = ask; } break; case OrderBookFrame.Remove: if (_asks.FindIndex(x => x.Rate == ask.Rate) is var remove && remove >= 0) { var delta = ask.Quantity - _asks[remove].Quantity; _asksReferentTotal += delta * ask.Rate; _asksCurrencyTotal += delta; _asks.RemoveAt(remove); } break; } } } Console.WriteLine($"Market Depth {Name}: BID={_bidsCurrencyTotal} / ASK={_asksCurrencyTotal} - [Base: BID={_bidsReferentTotal} / ASK={_asksReferentTotal}]"); } finally { _lock.ExitWriteLock(); } } }