예제 #1
0
        /// <summary>
        /// Creates from string.
        /// </summary>
        /// <param name="rawMessage">The raw message.</param>
        /// <returns></returns>
        public static TradeMessage CreateFromString(string rawMessage)
        {
            var message = KrakenDataMessageHelper.EnsureRawMessage(rawMessage);

            return(new TradeMessage()
            {
                ChannelId = Convert.ToInt64(message.First),
                Trades = ((JArray)message[1]).OfType <JArray>().Select(tradeJArray => TradeValues.CreateFromJArray(tradeJArray)).ToArray()
            });
        }
예제 #2
0
        /// <summary>
        /// Creates from string.
        /// </summary>
        /// <param name="rawBookSnapshotMessage">The raw book snapshot message.</param>
        /// <returns></returns>
        public static BookSnapshotMessage CreateFromString(string rawBookSnapshotMessage)
        {
            var bookSnapshotTokens = KrakenDataMessageHelper.EnsureRawMessage(rawBookSnapshotMessage);
            var detailTokens       = (JObject)bookSnapshotTokens[1];

            return(new BookSnapshotMessage
            {
                ChannelId = Convert.ToInt64(bookSnapshotTokens.First),
                Asks = ((JArray)detailTokens["as"]).OfType <JArray>().Select(level => PriceLevel.CreateFromJArray(level)).ToArray(),
                Bids = ((JArray)detailTokens["bs"]).OfType <JArray>().Select(level => PriceLevel.CreateFromJArray(level)).ToArray()
            });
        }
예제 #3
0
        /// <summary>
        /// Creates from string.
        /// </summary>
        /// <param name="rawSpreadMessage">The raw spread message.</param>
        /// <returns></returns>
        public static SpreadMessage CreateFromString(string rawSpreadMessage)
        {
            var spreadMessage = KrakenDataMessageHelper.EnsureRawMessage(rawSpreadMessage);
            var spreadTokens  = spreadMessage[1] as JArray;

            return(new SpreadMessage
            {
                ChannelId = Convert.ToInt64(spreadMessage.First),
                Bid = Convert.ToDecimal(spreadTokens[0]),
                Ask = Convert.ToDecimal(spreadTokens[1]),
                Time = Convert.ToDecimal(spreadTokens[2]),
            });
        }
예제 #4
0
        /// <summary>
        /// Creates from string.
        /// </summary>
        /// <param name="rawBookUpdateMessage">The raw book update message.</param>
        /// <returns></returns>
        public static BookUpdateMessage CreateFromString(string rawBookUpdateMessage)
        {
            var bookUpdateMessage = KrakenDataMessageHelper.EnsureRawMessage(rawBookUpdateMessage);

            var asks = bookUpdateMessage.Skip(1).OfType <JObject>().FirstOrDefault(x => x.ContainsKey("a"));
            var bids = bookUpdateMessage.Skip(1).OfType <JObject>().FirstOrDefault(x => x.ContainsKey("b"));

            return(new BookUpdateMessage
            {
                ChannelId = Convert.ToInt64(bookUpdateMessage.First),
                Asks = asks != null ? asks["a"].OfType <JArray>().Select(level => PriceLevel.CreateFromJArray(level)).ToArray() : null,
                Bids = bids != null ? bids["b"].OfType <JArray>().Select(level => PriceLevel.CreateFromJArray(level)).ToArray() : null,
            });
        }
예제 #5
0
        /// <summary>
        /// Creates a new <see cref="OhlcMessage" /> from well-formed string.
        /// </summary>
        /// <param name="rawOhlcMessage">The raw ohlc message.</param>
        /// <returns></returns>
        public static OhlcMessage CreateFromString(string rawOhlcMessage)
        {
            var ohlcMessage = KrakenDataMessageHelper.EnsureRawMessage(rawOhlcMessage);
            var dataArray   = ohlcMessage.Last as JArray;

            return(new OhlcMessage
            {
                ChannelId = Convert.ToInt64(ohlcMessage.First),
                Time = Convert.ToDecimal(dataArray[0]),
                EndTime = Convert.ToDecimal(dataArray[1]),
                Open = Convert.ToDecimal(dataArray[2]),
                High = Convert.ToDecimal(dataArray[3]),
                Low = Convert.ToDecimal(dataArray[4]),
                Close = Convert.ToDecimal(dataArray[5]),
                Vwap = Convert.ToDecimal(dataArray[6]),
                Volume = Convert.ToDecimal(dataArray[7]),
                Count = Convert.ToInt64(dataArray[8])
            });
        }
예제 #6
0
        /// <summary>
        /// Creates the <see cref="OwnTradesMessage"/> from it's from string representation coming from the api.
        /// </summary>
        /// <param name="rawMessage">The raw message.</param>
        /// <returns></returns>
        public static OwnTradesMessage CreateFromString(string rawMessage)
        {
            var message = KrakenDataMessageHelper.EnsureRawMessage(rawMessage);
            var trades  = message[0]
                          .Select(x => (x as JObject)?.ToObject <Dictionary <string, JObject> >())
                          .Select(items => items != null && items.Count == 1 ?
                                  new
            {
                TradeId     = items.Keys.ToArray()[0],
                TradeObject = items.Values.ToArray()[0]
            } :
                                  null)
                          .Where(x => x != null)
                          .Select(x => TradeObject.CreateFromJObject(x.TradeId, x.TradeObject))
                          .ToList();

            return(new OwnTradesMessage
            {
                Trades = trades,
                Name = message[1].ToString()
            });
        }
예제 #7
0
        /// <summary>
        /// Creates from string.
        /// </summary>
        /// <param name="rawMessage">The raw message.</param>
        /// <returns></returns>
        internal static OpenOrdersMessage CreateFromString(string rawMessage)
        {
            var message = KrakenDataMessageHelper.EnsureRawMessage(rawMessage);
            var orders  = message[0]
                          .Select(x => (x as JObject)?.ToObject <Dictionary <string, JObject> >())
                          .Select(items => items != null && items.Count == 1 ?
                                  new
            {
                TradeId     = items.Keys.ToArray()[0],
                TradeObject = items.Values.ToArray()[0]
            } :
                                  null)
                          .Where(x => x != null)
                          .Select(x => Order.CreateFromJObject(x.TradeId, x.TradeObject))
                          .ToList();

            return(new OpenOrdersMessage()
            {
                Orders = orders,
                ChannelName = message[1].Value <string>()
            });
        }
예제 #8
0
        /// <summary>
        /// Creates a new <see cref="TickerMessage"/> insdtance from string.
        /// </summary>
        /// <param name="tickerRawMessage">The ticker raw message.</param>
        /// <param name="subscription">The subscription.</param>
        /// <returns></returns>
        public static TickerMessage CreateFromString(string tickerRawMessage, SubscriptionStatus subscription)
        {
            var tokenArray = KrakenDataMessageHelper.EnsureRawMessage(tickerRawMessage);
            var channelId  = (int)tokenArray.First;
            var message    = tokenArray.Last;

            var tickerMessage = new TickerMessage()
            {
                ChannelId = channelId,
                Pair      = subscription.Pair,
                Ask       = BestPriceVolume.CreateFromJArray(message["a"] as JArray),
                Bid       = BestPriceVolume.CreateFromJArray(message["b"] as JArray),
                Close     = ClosePriceVolume.CreateFromJArray(message["c"] as JArray),
                Volume    = TodayAnd24HourValue <decimal> .CreateFromJArray(message["v"] as JArray),
                VolumeWeightedAveragePrice = TodayAnd24HourValue <decimal> .CreateFromJArray(message["p"] as JArray),
                Trades    = TodayAnd24HourValue <int> .CreateFromJArray(message["t"] as JArray),
                LowPrice  = TodayAnd24HourValue <decimal> .CreateFromJArray(message["l"] as JArray),
                HighPrice = TodayAnd24HourValue <decimal> .CreateFromJArray(message["h"] as JArray),
                OpenPrice = TodayAnd24HourValue <decimal> .CreateFromJArray(message["o"] as JArray)
            };

            return(tickerMessage);
        }