private Task SecondaryMessageReceived_AssetQuote(AssetQuote assetQuote) { //Message received, update timestamp. _secondaryStreamLastMessageTimeStamp = DateTime.UtcNow; // Filter Asset from Primary Stream Configuration File if (!_secondaryGameInstruments.Contains(assetQuote.AssetPair)) { // Not in allowed assets list, discard entry return(Task.FromResult(0)); } else { return(ProcessAssetQuote(assetQuote)); } }
private Task ProcessAssetQuote(AssetQuote assetQuote) { // If Price is zero publish exception to support slack channel if (assetQuote.Price <= 0) { //LogWarning("ProcessAssetQuote", string.Format("Received AssetQuote with price zero [0], AssetQuote discarded. {0}", assetQuote)); return(Task.FromResult(0)); } // Get Asset from cache InstrumentPrice assetbid = (from a in _assetCache where a.Instrument == assetQuote.AssetPair select a).FirstOrDefault(); if (assetbid == null) { // AssetPair is not in cache // Add AssetQuote to cache assetbid = new InstrumentPrice() { Instrument = assetQuote.AssetPair, Source = "AssetQuote", Date = assetQuote.Timestamp, Ask = assetQuote.IsBuy == Statics.ASK ? assetQuote.Price : 0, Bid = assetQuote.IsBuy == Statics.ASK ? 0 : assetQuote.Price, ReceiveDate = DateTime.UtcNow }; _assetCache.Add(assetbid); } else { // AssetPair is in cache // Update Bid Quote if (assetQuote.IsBuy == Statics.ASK) { assetbid.Ask = assetQuote.Price; } else { assetbid.Bid = assetQuote.Price; } } // Only publish if bid and ask prices have changed since last publish bool publish = false; if (!_lastPrices.ContainsKey(assetbid.Instrument)) { // Asset not in history, add it and set publish = true _lastPrices.Add(assetQuote.AssetPair, (InstrumentPrice)assetbid.ClonePrice()); publish = true; } else { if (_lastPrices[assetbid.Instrument].Ask > 0 && _lastPrices[assetbid.Instrument].Bid > 0 && (_lastPrices[assetbid.Instrument].Ask == assetbid.Ask || _lastPrices[assetbid.Instrument].Bid == assetbid.Bid)) { // One price (Ask or Bid) has not changed. do not publish it // Must only be published when both Ask Bid prices have changed publish = false; } else { // Both prices have changed publish it and assign this new bid to history publish = true; _lastPrices[assetbid.Instrument] = (InstrumentPrice)assetbid.ClonePrice(); } } if (assetbid.Ask <= 0 || assetbid.Bid <= 0) { publish = false; } if (publish) { OnMessageReceived(assetbid); } return(Task.FromResult(0)); }