Exemplo n.º 1
0
        /// <summary>
        /// Subscribe to depth updates
        /// </summary>
        /// <param name="symbol">Symbol to subscribe to</param>
        /// <param name="depth">Depth of the initial order book snapshot. 10, 25, 100, 500 or 1000</param>
        /// <param name="handler">Data handler</param>
        /// <returns>A stream subscription. This stream subscription can be used to be notified when the socket is disconnected/reconnected</returns>
        public async Task <CallResult <UpdateSubscription> > SubscribeToOrderBookUpdatesAsync(string symbol, int depth, Action <DataEvent <KrakenStreamOrderBook> > handler)
        {
            symbol.ValidateKrakenWebsocketSymbol();
            depth.ValidateIntValues(nameof(depth), 10, 25, 100, 500, 1000);
            var subSymbol = SymbolToServer(symbol);

            var innerHandler = new Action <DataEvent <string> >(data =>
            {
                var token = data.Data.ToJToken(log);
                if (token == null || token.Type != JTokenType.Array)
                {
                    log.Write(LogLevel.Warning, "Failed to deserialize stream order book");
                    return;
                }
                var evnt = StreamOrderBookConverter.Convert((JArray)token);
                if (evnt == null)
                {
                    log.Write(LogLevel.Warning, "Failed to deserialize stream order book");
                    return;
                }

                handler(data.As(evnt.Data, symbol));
            });

            return(await SubscribeAsync(new KrakenSubscribeRequest("book", NextId(), subSymbol) { Details = new KrakenDepthSubscriptionDetails(depth) }, null, false, innerHandler).ConfigureAwait(false));
        }
        public void Event_Should_ParseCountOfFour()
        {
            var testObj = StreamOrderBookConverter.Convert(this.fiveElements);

            Assert.AreEqual(2, testObj.Data.Asks.Count());
            Assert.AreEqual(1, testObj.Data.Bids.Count());
            Assert.AreEqual(1234, testObj.ChannelId);
            Assert.AreEqual("book-10", testObj.Topic);
            Assert.AreEqual("XBT/USD", testObj.Symbol);

            Assert.AreEqual("0.40100000", testObj.Data.Asks.ElementAt(1).RawQuantity);
            Assert.AreEqual("5542.50000", testObj.Data.Asks.ElementAt(1).RawPrice);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Subscribe to depth updates
        /// </summary>
        /// <param name="market">Market to subscribe to</param>
        /// <param name="depth">Depth of the initial order book snapshot</param>
        /// <param name="handler">Data handler</param>
        /// <returns>A stream subscription. This stream subscription can be used to be notified when the socket is disconnected/reconnected</returns>
        public async Task <CallResult <UpdateSubscription> > SubscribeToDepthUpdatesAsync(string market, int depth, Action <KrakenSocketEvent <KrakenStreamOrderBook> > handler)
        {
            var innerHandler = new Action <string>(data =>
            {
                var token = data.ToJToken(log);
                if (token == null || token.Type != JTokenType.Array)
                {
                    log.Write(LogVerbosity.Warning, "Failed to deserialize stream order book");
                    return;
                }
                handler(StreamOrderBookConverter.Convert((JArray)token));
            });

            return(await Subscribe(new KrakenSubscribeRequest("book", NextId(), market) { Details = new KrakenDepthSubscriptionDetails(depth) }, null, false, innerHandler).ConfigureAwait(false));
        }