//We update an orderBook in memory and publish protected void UpdateOrderBook(WebSocketSubscriptionEvent subscrEvent) { if (subscrEvent is WebSocketErrorMessage) { ProcessSubscrError((WebSocketErrorMessage)subscrEvent); return; } WebSocketOrderBookL2Event orderBookEvent = (WebSocketOrderBookL2Event)subscrEvent; try { zHFT.InstructionBasedMarketClient.BitMex.Common.DTO.OrderBookEntry[] bids = orderBookEvent.data.Where(x => x.IsBuy()).OrderByDescending(x => x.price).ToArray(); zHFT.InstructionBasedMarketClient.BitMex.Common.DTO.OrderBookEntry[] asks = orderBookEvent.data.Where(x => x.IsSell()).OrderBy(x => x.price).ToArray(); SendOrderBookEntries(orderBookEvent.action, bids, asks); OrderBookHandler.DoUpdateOrderBooks(orderBookEvent.action, bids, asks); //string symbol = ""; //if (bids.Length > 0) // symbol = bids[0].symbol; //else if (asks.Length > 0) // symbol = asks[0].symbol; //else // return; //if (ActiveSecuritiesQuotes.Values.Any(x => x.Symbol == symbol) && OrderBookHandler.OrderBooks.ContainsKey(symbol)) //{ // Security sec = ActiveSecuritiesQuotes.Values.Where(x => x.Symbol == symbol).FirstOrDefault(); // OrderBookDictionary dict = OrderBookHandler.OrderBooks[symbol]; // zHFT.InstructionBasedMarketClient.BitMex.Common.DTO.OrderBookEntry bestBid = dict.Entries.Values.Where(x => x.IsBuy() && x.size > 0).OrderByDescending(x => x.price).FirstOrDefault(); // zHFT.InstructionBasedMarketClient.BitMex.Common.DTO.OrderBookEntry bestAsk = dict.Entries.Values.Where(x => x.IsSell() && x.size > 0).OrderBy(x => x.price).FirstOrDefault(); // sec.MarketData.BestBidPrice = Convert.ToDouble(bestBid.price); // sec.MarketData.BestBidSize = Convert.ToInt64(bestBid.size); // sec.MarketData.BestAskPrice = Convert.ToDouble(bestAsk.price); // sec.MarketData.BestAskSize = Convert.ToInt64(bestAsk.size); // //DoLog(string.Format("@{5}:Publishing Market Data for symbol {0}: Best Bid Size={1} Best Bid Price={2} Best Ask Size={3} Best Ask Price={4}", // // symbol, bestBid.size.ToString("##.########"), bestBid.price.ToString("##.##"), // // bestAsk.size.ToString("##.########"), bestAsk.price.ToString("##.##"), // // BitmexConfiguration.Name), Main.Common.Util.Constants.MessageType.Information); // MarketDataWrapper wrapper = new MarketDataWrapper(sec, GetConfig()); // OnMessageRcv(wrapper); //} } catch (Exception ex) { DoLog(string.Format("@{0}:Error processing order book :{1}", BitmexConfiguration.Name, ex.Message), Main.Common.Util.Constants.MessageType.Error); } }
protected override void DoRunLoopSubscriptions(string resp) { WebSocketSubscriptionEvent eventSubscr = JsonConvert.DeserializeObject <WebSocketSubscriptionEvent>(resp); if (ResponseRequestSubscriptions.ContainsKey(eventSubscr.GetSubscriptionEvent())) { if (eventSubscr.table == _ORDERBOOK_L2 && EventSubscriptions.ContainsKey(eventSubscr.GetSubscriptionEvent())) { WebSocketOrderBookL2Event orderBookL2Event = JsonConvert.DeserializeObject <WebSocketOrderBookL2Event>(resp); WebSocketSubscriptionEvent subscrEvent = EventSubscriptions[eventSubscr.GetSubscriptionEvent()]; subscrEvent.RunEvent(orderBookL2Event); } else if (eventSubscr.table == _TRADE && EventSubscriptions.ContainsKey(eventSubscr.GetSubscriptionEvent())) { WebSocketTradeEvent tradeEvent = JsonConvert.DeserializeObject <WebSocketTradeEvent>(resp); WebSocketSubscriptionEvent subscrEvent = EventSubscriptions[eventSubscr.GetSubscriptionEvent()]; subscrEvent.RunEvent(tradeEvent); } else if (eventSubscr.table == _QUOTE && EventSubscriptions.ContainsKey(eventSubscr.GetSubscriptionEvent())) { WebSocketQuoteEvent quoteEvent = JsonConvert.DeserializeObject <WebSocketQuoteEvent>(resp); WebSocketSubscriptionEvent subscrEvent = EventSubscriptions[eventSubscr.GetSubscriptionEvent()]; subscrEvent.RunEvent(quoteEvent); } else if (eventSubscr.table == _1_DAY_TRADE_BINS && EventSubscriptions.ContainsKey(eventSubscr.GetSubscriptionEvent())) { //TODO: I have to ask BitMex why aren't they retrieveing high and low for the 1 day bin //and they are for the 1 hour bin } else { //Log what we are receiving here because we are getting events that we didn't expect } } }