Example #1
0
        private void InvokeDataReceived(KrakenMessageEventArgs krakenMessageEventArgs)
        {
            var dataReceivedHandler = DataReceived;

            if (dataReceivedHandler == null)
            {
                return;
            }

            InvokeAllHandlers(dataReceivedHandler.GetInvocationList(), krakenMessageEventArgs);
        }
        private void HandlePrivateContent(KrakenMessageEventArgs eventArgs)
        {
            // handle private content
            if (eventArgs.RawContent.Contains(@"""ownTrades"""))
            {
                var ownTrades = OwnTradesMessage.CreateFromString(eventArgs.RawContent);
                OwnTradesReceived.InvokeAll(this, new KrakenPrivateEventArgs <OwnTradesMessage>(ownTrades));
            }

            if (eventArgs.RawContent.Contains(@"""openOrders"""))
            {
                var openOrders = OpenOrdersMessage.CreateFromString(eventArgs.RawContent);
                OpenOrdersReceived.InvokeAll(this, new KrakenPrivateEventArgs <OpenOrdersMessage>(openOrders));
            }
        }
        private void HandleIncomingMessage(object sender, KrakenMessageEventArgs eventArgs)
        {
            logger.LogDebug("Handling incoming '{@event}' message", eventArgs.Event);

            switch (eventArgs.Event)
            {
            case Heartbeat.EventName:
                HandleEvent(eventArgs, HeartbeatReceived);
                break;

            case SystemStatus.EventName:
                SystemStatus = HandleEvent(eventArgs, SystemStatusChanged);
                break;

            case SubscriptionStatus.EventName:
                SynchronizeSubscriptions(HandleEvent(eventArgs, SubscriptionStatusChanged));
                break;

            case AddOrderStatusEvent.EventName:
                HandleEvent(eventArgs, AddOrderStatusReceived);
                break;

            case CancelOrderStatusEvent.EventName:
                HandleEvent(eventArgs, CancelOrderStatusReceived);
                break;

            case "private":
                HandlePrivateContent(eventArgs);
                break;

            case "data":
                SubscriptionStatus subscription;
                lock (Subscriptions)
                    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;
                }

                HandleData(eventArgs, subscription);
                break;

            default:
                logger.LogWarning("Could not handle incoming message: {message}", eventArgs.RawContent);
                break;
            }
        }
        private TEvent HandleEvent <TEvent>(KrakenMessageEventArgs eventArgs, EventHandler <KrakenMessageEventArgs <TEvent> > eventHandler)
            where TEvent : KrakenMessage, new()
        {
            try
            {
                var eventObject = serializer.Deserialize <TEvent>(eventArgs.RawContent);
                logger.LogTrace("Event '{eventType}' received: {@event}", eventObject.GetType().Name, eventObject);

                eventHandler.InvokeAll(this, eventObject);
                return(eventObject);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Failed to deserialize addOrderStatus: {message}", eventArgs.RawContent);
                return(null);
            }
        }
        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));
                }
            }
        }
Example #6
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;
            }
        }
Example #7
0
 public KrakenMessageEventArgsTests()
 {
     eventString = "Test";
     testJson    = @"{""event"":""Test""}";
     instance    = new KrakenMessageEventArgs(eventString, testJson);
 }