/// <summary> /// Read from a binary reader /// </summary> /// <param name="reader">Binary reader</param> public void FromBinary(BinaryReader reader) { Asks.Clear(); Bids.Clear(); int askCount = reader.ReadInt32(); int bidCount = reader.ReadInt32(); while (askCount-- > 0) { var exchangeOrderPrice = new ExchangeOrderPrice(reader); Asks.Add(exchangeOrderPrice.Price, exchangeOrderPrice); } while (bidCount-- > 0) { var exchangeOrderPrice = new ExchangeOrderPrice(reader); Bids.Add(exchangeOrderPrice.Price, exchangeOrderPrice); } }
protected override IWebSocket OnGetOrderBookDeltasWebSocket(Action <ExchangeOrderBook> callback, int maxCount = 20, params string[] symbols) { if (callback == null || symbols == null || !symbols.Any()) { return(null); } string combined = string.Join("/", symbols.Select(s => this.NormalizeSymbol(s).ToLowerInvariant() + "@depth")); return(ConnectWebSocket($"/stream?streams={combined}", (msg, _socket) => { try { string json = msg.UTF8String(); var update = JsonConvert.DeserializeObject <BinanceMultiDepthStream>(json); string symbol = update.Data.Symbol; ExchangeOrderBook book = new ExchangeOrderBook { SequenceId = update.Data.FinalUpdate, Symbol = symbol }; foreach (List <object> ask in update.Data.Asks) { var depth = new ExchangeOrderPrice { Price = ask[0].ConvertInvariant <decimal>(), Amount = ask[1].ConvertInvariant <decimal>() }; book.Asks[depth.Price] = depth; } foreach (List <object> bid in update.Data.Bids) { var depth = new ExchangeOrderPrice { Price = bid[0].ConvertInvariant <decimal>(), Amount = bid[1].ConvertInvariant <decimal>() }; book.Bids[depth.Price] = depth; } callback(book); } catch { } })); }
/// <summary>Common order book parsing method, checks for "amount" or "quantity" and "price" /// elements</summary> /// <param name="token">Token</param> /// <param name="asks">Asks key</param> /// <param name="bids">Bids key</param> /// <param name="price">Price key</param> /// <param name="amount">Quantity key</param> /// <param name="sequence">Sequence key</param> /// <param name="maxCount">Max count</param> /// <returns>Order book</returns> public static ExchangeOrderBook ParseOrderBookFromJTokenDictionaries( JToken token, string asks = "asks", string bids = "bids", string price = "price", string amount = "amount", string sequence = "ts", int maxCount = 100) { var book = new ExchangeOrderBook { SequenceId = token[sequence].ConvertInvariant <long>() }; foreach (JToken ask in token[asks]) { var depth = new ExchangeOrderPrice { Price = ask[price].ConvertInvariant <decimal>(), Amount = ask[amount].ConvertInvariant <decimal>() }; book.Asks[depth.Price] = depth; if (book.Asks.Count == maxCount) { break; } } foreach (JToken bid in token[bids]) { var depth = new ExchangeOrderPrice { Price = bid[price].ConvertInvariant <decimal>(), Amount = bid[amount].ConvertInvariant <decimal>() }; book.Bids[depth.Price] = depth; if (book.Bids.Count == maxCount) { break; } } return(book); }
protected override IWebSocket OnGetOrderBookDeltasWebSocket( Action <ExchangeOrderBook> callback, int maxCount = 20, params string[] symbols) { if (callback == null || symbols == null || !symbols.Any()) { return(null); } void innerCallback(string json) { #region sample json /* * { * MarketName : string, * Nonce : int, * Buys: * [ * { * Type : int, * Rate : decimal, * Quantity : decimal * } * ], * Sells: * [ * { * Type : int, * Rate : decimal, * Quantity : decimal * } * ], * Fills: * [ * { * FillId : int, * OrderType : string, * Rate : decimal, * Quantity : decimal, * TimeStamp : date * } * ] * } */ #endregion var ordersUpdates = JsonConvert.DeserializeObject <BittrexStreamUpdateExchangeState>(json); var book = new ExchangeOrderBook(); foreach (BittrexStreamOrderBookUpdateEntry ask in ordersUpdates.Sells) { var depth = new ExchangeOrderPrice { Price = ask.Rate, Amount = ask.Quantity }; book.Asks[depth.Price] = depth; } foreach (BittrexStreamOrderBookUpdateEntry bid in ordersUpdates.Buys) { var depth = new ExchangeOrderPrice { Price = bid.Rate, Amount = bid.Quantity }; book.Bids[depth.Price] = depth; } book.Symbol = ordersUpdates.MarketName; book.SequenceId = ordersUpdates.Nonce; callback(book); } return(this.SocketManager.SubscribeToExchangeDeltas(innerCallback, symbols)); }