Ejemplo n.º 1
0
        /// <summary>
        /// Handles error messages from IB
        /// </summary>
        private void HandleError(object sender, IB.ErrorEventArgs e)
        {
            Log.Trace(string.Format("InteractiveBrokersBrokerage.HandleError(): Order: {0} ErrorCode: {1} - {2}", e.TickerId, e.ErrorCode, e.ErrorMsg));

            // figure out the message type based on our code collections below
            var brokerageMessageType = BrokerageMessageType.Information;

            if (ErrorCodes.Contains((int)e.ErrorCode))
            {
                brokerageMessageType = BrokerageMessageType.Error;
            }
            else if (WarningCodes.Contains((int)e.ErrorCode))
            {
                brokerageMessageType = BrokerageMessageType.Warning;
            }

            if (InvalidatingCodes.Contains((int)e.ErrorCode))
            {
                Log.Trace(string.Format("InteractiveBrokersBrokerage.HandleError.InvalidateOrder(): Order: {0} ErrorCode: {1} - {2}", e.TickerId, e.ErrorCode, e.ErrorMsg));

                // invalidate the order
                var order      = _orderMapping.GetOrderByBrokerageId(e.TickerId);
                var orderEvent = new OrderEvent(order)
                {
                    Status = OrderStatus.Invalid
                };
                OnOrderEvent(orderEvent);
            }

            OnMessage(new BrokerageMessageEvent(brokerageMessageType, (int)e.ErrorCode, e.ErrorMsg));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles error messages from IB
        /// </summary>
        private void HandleError(object sender, IB.ErrorEventArgs e)
        {
            // https://www.interactivebrokers.com/en/software/api/apiguide/tables/api_message_codes.htm

            // rewrite these messages to be single lined
            e.ErrorMsg = e.ErrorMsg.Replace("\r\n", ". ").Replace("\r", ". ").Replace("\n", ". ");
            Log.Trace(string.Format("InteractiveBrokersBrokerage.HandleError(): Order: {0} ErrorCode: {1} - {2}", e.TickerId, e.ErrorCode, e.ErrorMsg));

            // figure out the message type based on our code collections below
            var brokerageMessageType = BrokerageMessageType.Information;

            if (ErrorCodes.Contains((int)e.ErrorCode))
            {
                brokerageMessageType = BrokerageMessageType.Error;
            }
            else if (WarningCodes.Contains((int)e.ErrorCode))
            {
                brokerageMessageType = BrokerageMessageType.Warning;
            }

            // code 1100 is a connection failure, we'll wait a minute before exploding gracefully
            if ((int)e.ErrorCode == 1100 && !_disconnected1100Fired)
            {
                _disconnected1100Fired = true;
                // wait a minute and see if we've been reconnected
                Task.Delay(TimeSpan.FromMinutes(1)).ContinueWith(task =>
                {
                    if (_disconnected1100Fired)
                    {
                        OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Error, -1, "Connection with Interactive Brokers lost. " +
                                                            "This could be because of internet connectivity issues or a log in from another location."
                                                            ));
                    }
                });
            }
            else if ((int)e.ErrorCode == 1102)
            {
                _disconnected1100Fired = false;
            }

            if (InvalidatingCodes.Contains((int)e.ErrorCode))
            {
                Log.Trace(string.Format("InteractiveBrokersBrokerage.HandleError.InvalidateOrder(): Order: {0} ErrorCode: {1} - {2}", e.TickerId, e.ErrorCode, e.ErrorMsg));

                // invalidate the order
                var order      = _orderMapping.GetOrderByBrokerageId(e.TickerId);
                var orderEvent = new OrderEvent(order)
                {
                    Status = OrderStatus.Invalid
                };
                OnOrderEvent(orderEvent);
            }

            OnMessage(new BrokerageMessageEvent(brokerageMessageType, (int)e.ErrorCode, e.ErrorMsg));
        }