Exemplo n.º 1
0
        /// <summary>
        ///     Common order book parsing method, most exchanges use "asks" and "bids" with
        ///     arrays of length 2 for price and amount (or amount and price)
        /// </summary>
        /// <param name="token">Token</param>
        /// <param name="symbol"></param>
        /// <param name="asks">Asks key</param>
        /// <param name="bids">Bids key</param>
        /// <param name="timestampType"></param>
        /// <param name="maxCount">Max count</param>
        /// <param name="sequence"></param>
        /// <returns>Order book</returns>
        internal static ExchangeDepth ParseDepthFromJTokenArrays
        (
            this JToken token,
            Symbol symbol,
            string asks                 = "asks",
            string bids                 = "bids",
            string sequence             = "ts",
            TimestampType timestampType = TimestampType.None,
            int maxCount                = 100
        )
        {
            var book = new ExchangeDepth
            {
                SequenceId     = token[sequence].ConvertInvariant <long>(),
                LastUpdatedUtc = CryptoUtility.ParseTimestamp(token[sequence], timestampType),
                Symbol         = symbol
            };

            foreach (JArray array in token[asks])
            {
                var depth = new ExchangeOrderPrice
                {
                    Price  = array[0].ConvertInvariant <decimal>(),
                    Amount = array[1].ConvertInvariant <decimal>()
                };
                book.Asks[depth.Price] = depth;
                if (book.Asks.Count == maxCount)
                {
                    break;
                }
            }

            foreach (JArray array in token[bids])
            {
                var depth = new ExchangeOrderPrice
                {
                    Price  = array[0].ConvertInvariant <decimal>(),
                    Amount = array[1].ConvertInvariant <decimal>()
                };
                book.Bids[depth.Price] = depth;
                if (book.Bids.Count == maxCount)
                {
                    break;
                }
            }

            return(book);
        }
Exemplo n.º 2
0
        /// <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>
        internal static ExchangeDepth ParseDepthFromJTokenDictionaries
        (
            this JToken token,
            string asks     = "asks",
            string bids     = "bids",
            string price    = "price",
            string amount   = "amount",
            string sequence = "ts",
            int maxCount    = 100
        )
        {
            var book = new ExchangeDepth {
                SequenceId = token[sequence].ConvertInvariant <long>()
            };

            foreach (var 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 (var 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);
        }
Exemplo n.º 3
0
        /// <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);
            }
        }