private void handleTradeMessage(
            Object sender,
            MsgHandlerEventArgs eventArgs)
        {
            var message = deserializeBytes <JsonStreamTrade>(
                eventArgs.Message.Data);

            TradeReceived?.Invoke(message);
        }
Beispiel #2
0
        /// <inheritdoc/>
        protected override void OnMessageReceived(
            String message)
        {
            try
            {
                var rootArray = JArray.Parse(message);

                foreach (var root in rootArray)
                {
                    var stream = root["ev"].ToString();

                    switch (stream)
                    {
                    case TradesChannel:
                        TradeReceived?.Invoke(root.ToObject <JsonStreamTrade>());
                        break;

                    case QuotesChannel:
                        QuoteReceived?.Invoke(root.ToObject <JsonStreamQuote>());
                        break;

                    case MinuteAggChannel:
                        MinuteAggReceived?.Invoke(root.ToObject <JsonStreamAgg>());
                        break;

                    case SecondAggChannel:
                        SecondAggReceived?.Invoke(root.ToObject <JsonStreamAgg>());
                        break;

                    case StatusMessage:
                        handleAuthorization(root.ToObject <JsonConnectionStatus>());
                        break;

                    default:
                        HandleError(new InvalidOperationException(
                                        $"Unexpected message type '{stream}' received."));
                        break;
                    }
                }
            }
            catch (Exception exception)
            {
                HandleError(exception);
            }
        }
        private void HandleData(KrakenMessageEventArgs eventArgs, SubscriptionStatus subscription)
        {
            var dataType = subscription.Subscription.Name;

            if (dataType == SubscribeOptionNames.Ticker)
            {
                var tickerMessage = TickerMessage.CreateFromString(eventArgs.RawContent, subscription);
                TickerReceived.InvokeAll(this, new TickerEventArgs(subscription.ChannelId.Value, subscription.Pair, tickerMessage));
            }
            if (dataType == SubscribeOptionNames.OHLC)
            {
                var ohlcMessage = OhlcMessage.CreateFromString(eventArgs.RawContent);
                OhlcReceived.InvokeAll(this, new OhlcEventArgs(subscription.ChannelId.Value, subscription.Pair, ohlcMessage));
            }
            if (dataType == SubscribeOptionNames.Trade)
            {
                var tradeMessage = TradeMessage.CreateFromString(eventArgs.RawContent);
                TradeReceived.InvokeAll(this, new TradeEventArgs(subscription.ChannelId.Value, subscription.Pair, tradeMessage));
            }
            if (dataType == SubscribeOptionNames.Spread)
            {
                var spreadMessage = SpreadMessage.CreateFromString(eventArgs.RawContent);
                SpreadReceived.InvokeAll(this, new SpreadEventArgs(subscription.ChannelId.Value, subscription.Pair, spreadMessage));
            }
            if (dataType == SubscribeOptionNames.Book)
            {
                if (eventArgs.RawContent.Contains(@"""as"":") && eventArgs.RawContent.Contains(@"""bs"":"))
                {
                    var bookSnapshotMessage = BookSnapshotMessage.CreateFromString(eventArgs.RawContent);
                    BookSnapshotReceived.InvokeAll(this, new KrakenDataEventArgs <BookSnapshotMessage>(eventArgs.ChannelId.Value, subscription.Pair, bookSnapshotMessage));
                }
                if (eventArgs.RawContent.Contains(@"""a"":") || eventArgs.RawContent.Contains(@"""b"":"))
                {
                    var bookUpdateMessage = BookUpdateMessage.CreateFromString(eventArgs.RawContent);
                    BookUpdateReceived.InvokeAll(this, new KrakenDataEventArgs <BookUpdateMessage>(eventArgs.ChannelId.Value, subscription.Pair, bookUpdateMessage));
                }
            }
        }
Beispiel #4
0
 private void handleTradesChannel(
     JToken token) =>
 TradeReceived.DeserializeAndInvoke <IStreamTrade, JsonStreamTrade>(token);
Beispiel #5
0
        private void HandleIncomingMessage(object sender, KrakenMessageEventArgs eventArgs)
        {
            logger.LogDebug("Handling incoming '{event}' message", eventArgs.Event);

            switch (eventArgs.Event)
            {
            case Heartbeat.EventName:
                var heartbeat = serializer.Deserialize <Heartbeat>(eventArgs.RawContent);
                logger.LogTrace("Heartbeat received: {heartbeat}", heartbeat);
                HeartbeatReceived.InvokeAll(this, heartbeat);
                break;

            case SystemStatus.EventName:
                var systemStatus = serializer.Deserialize <SystemStatus>(eventArgs.RawContent);
                logger.LogTrace("System status changed: {systemStatus}", systemStatus);
                SystemStatus = systemStatus;
                SystemStatusChanged.InvokeAll(this, systemStatus);
                break;

            case SubscriptionStatus.EventName:
                var subscriptionStatus = serializer.Deserialize <SubscriptionStatus>(eventArgs.RawContent);
                logger.LogTrace("Subscription status changed: {subscriptionStatus}", subscriptionStatus);

                SynchronizeSubscriptions(subscriptionStatus);
                SubscriptionStatusChanged.InvokeAll(this, subscriptionStatus);

                break;

            case "data":

                var subscription = Subscriptions.ContainsKey(eventArgs.ChannelId.Value) ? Subscriptions[eventArgs.ChannelId.Value] : null;
                if (subscription == null)
                {
                    logger.LogWarning("Didn't find a subscription for channelId {channelId}", eventArgs.ChannelId);
                    break;
                }

                var dataType = subscription.Subscription.Name;

                if (dataType == SubscribeOptionNames.Ticker)
                {
                    var tickerMessage = TickerMessage.CreateFromString(eventArgs.RawContent, subscription);
                    TickerReceived.InvokeAll(this, new TickerEventArgs(subscription.ChannelId.Value, subscription.Pair, tickerMessage));
                }
                if (dataType == SubscribeOptionNames.OHLC)
                {
                    var ohlcMessage = OhlcMessage.CreateFromString(eventArgs.RawContent);
                    OhlcReceived.InvokeAll(this, new OhlcEventArgs(subscription.ChannelId.Value, subscription.Pair, ohlcMessage));
                }
                if (dataType == SubscribeOptionNames.Trade)
                {
                    var tradeMessage = TradeMessage.CreateFromString(eventArgs.RawContent);
                    TradeReceived.InvokeAll(this, new TradeEventArgs(subscription.ChannelId.Value, subscription.Pair, tradeMessage));
                }
                if (dataType == SubscribeOptionNames.Spread)
                {
                    var spreadMessage = SpreadMessage.CreateFromString(eventArgs.RawContent);
                    SpreadReceived.InvokeAll(this, new SpreadEventArgs(subscription.ChannelId.Value, subscription.Pair, spreadMessage));
                }
                if (dataType == SubscribeOptionNames.Book)
                {
                    if (eventArgs.RawContent.Contains(@"""as"":") && eventArgs.RawContent.Contains(@"""bs"":"))
                    {
                        var bookSnapshotMessage = BookSnapshotMessage.CreateFromString(eventArgs.RawContent);
                        BookSnapshotReceived.InvokeAll(this, new KrakenDataEventArgs <BookSnapshotMessage>(eventArgs.ChannelId.Value, subscription.Pair, bookSnapshotMessage));
                    }
                    if (eventArgs.RawContent.Contains(@"""a"":") || eventArgs.RawContent.Contains(@"""b"":"))
                    {
                        var bookUpdateMessage = BookUpdateMessage.CreateFromString(eventArgs.RawContent);
                        BookUpdateReceived.InvokeAll(this, new KrakenDataEventArgs <BookUpdateMessage>(eventArgs.ChannelId.Value, subscription.Pair, bookUpdateMessage));
                    }
                }

                break;

            default:
                logger.LogWarning("Could not handle incoming message: {message}", eventArgs.RawContent);
                break;
            }
        }
Beispiel #6
0
 void IXSpi.ProcessQryTrade(TradeField trade, bool last)
 {
     TradeReceived?.Invoke(this, trade, last);
 }