private void BuildClients()
        {
            //Hook up a logger for the CIAPI.CS libraries
            LogManager.CreateInnerLogger = (logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat)
                         => new SimpleDebugAppender(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat);

            Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating rpc client"));
            RpcClient = new Client(RpcUri, StreamingUri, "CI-WP7");
            RpcClient.BeginLogIn(UerName, Password, ar =>
            {
                try
                {
                    RpcClient.EndLogIn(ar);
                    //RpcClient.MagicNumberResolver.PreloadMagicNumbersAsync();
                    RpcClient.MagicNumberResolver.PreloadMagicNumbers();
                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating listeners"));
                        StreamingClient = RpcClient.CreateStreamingClient();
                        PriceListener = StreamingClient.BuildPricesListener(MarketId);
                        PriceListener.MessageReceived += OnMessageReceived;

                        Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting account info"));
                        RpcClient.AccountInformation.BeginGetClientAndTradingAccount(ar2 =>
                        {
                            Account = RpcClient.AccountInformation.EndGetClientAndTradingAccount(ar2);

                            Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting market info"));
                            RpcClient.Market.BeginGetMarketInformation(MarketId.ToString(), ar3 =>
                            {
                                Market = RpcClient.Market.EndGetMarketInformation(ar3).MarketInformation;
                                Dispatcher.BeginInvoke(() => Button1.IsEnabled = true);
                            }, null);
                        }, null);
                    });
                }
                catch (Exception ex)
                {
                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("exception caught: " + ex));
                }
            }, null);
        }
Exemple #2
0
        private void BuildClients()
        {
            //Hook up a logger for the CIAPI.CS libraries
            LogManager.CreateInnerLogger = (logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat)
                                           => new SimpleDebugAppender(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat);

            Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating rpc client"));
            RpcClient = new Client(RpcUri, StreamingUri, "CI-WP7");
            RpcClient.BeginLogIn(UerName, Password, ar =>
            {
                try
                {
                    RpcClient.EndLogIn(ar);
                    //RpcClient.MagicNumberResolver.PreloadMagicNumbersAsync();
                    RpcClient.MagicNumberResolver.PreloadMagicNumbers();
                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating listeners"));
                        StreamingClient = RpcClient.CreateStreamingClient();
                        PriceListener   = StreamingClient.BuildPricesListener(MarketId);
                        PriceListener.MessageReceived += OnMessageReceived;

                        Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting account info"));
                        RpcClient.AccountInformation.BeginGetClientAndTradingAccount(ar2 =>
                        {
                            Account = RpcClient.AccountInformation.EndGetClientAndTradingAccount(ar2);

                            Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting market info"));
                            RpcClient.Market.BeginGetMarketInformation(MarketId.ToString(), ar3 =>
                            {
                                Market = RpcClient.Market.EndGetMarketInformation(ar3).MarketInformation;
                                Dispatcher.BeginInvoke(() => Button1.IsEnabled = true);
                            }, null);
                        }, null);
                    });
                }
                catch (Exception ex)
                {
                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("exception caught: " + ex));
                }
            }, null);
        }
        public static void Main(string[] args)
        {
            try
            {
                LogAssembliesBeingUsed();
                Connect();

                Console.WriteLine("Listening for client account margin");
                var marginListener = _streamingClient.BuildClientAccountMarginListener();
                _listeners.Add(marginListener);
                marginListener.MessageReceived += (s, e) =>
                {
                    Console.WriteLine("ClientAccountMargin, equity: {0}", e.Data.NetEquity);
                };

                Console.WriteLine("Listening for quotes");
                var quotesListener = _streamingClient.BuildQuotesListener();
                _listeners.Add(quotesListener);
                quotesListener.MessageReceived += (s, e) =>
                {
                    Console.WriteLine("Quote approved at {0}", e.Data.ApprovalDateTimeUTC);
                };

                Console.WriteLine("Listening for prices");
                var priceListener = _streamingClient.BuildPricesListener(Convert.ToInt32(ConfigurationManager.AppSettings["MarketId"]));
                _listeners.Add(priceListener);
                priceListener.MessageReceived += (s, message) => {
                    Console.WriteLine("Price at {0:O} is {1}", message.Data.TickDate, message.Data.Price);
                };

                Console.WriteLine("Listening for 120 seconds - check the number of open connections");
                var gate = new ManualResetEvent(false);
                gate.WaitOne(TimeSpan.FromSeconds(120));
            }
            finally
            {
                Disconnect();
            }
        }
        public override void Execute()
        {
            if (string.IsNullOrEmpty(ServerUrl))
                throw new ApplicationException("StreamingLatencyMonitor: ServerUrl is not set");

            lock (_sync)
            {
                if (_client == null)
                {
                    _client = new Client(new Uri(ServerUrl), new Uri(StreamingServerUrl), "{API_KEY}", 1);

                    _client.LogIn(UserName, Password);

                    _streamingClient = _client.CreateStreamingClient();
                    _streamingStartTime = DateTime.UtcNow;

                    _listener = _streamingClient.BuildPricesListener(MarketId);

                    _listener.MessageReceived += OnPriceUpdate;
                }
            }
        }
        private void BuildClients()
        {
            Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating rpc client"));
            RpcClient = new Client(RpcUri, StreamingUri, "CI-WP7");
            RpcClient.BeginLogIn(UerName, Password, ar =>
            {
                try
                {
                    RpcClient.EndLogIn(ar);

                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating listeners"));
                        StreamingClient = RpcClient.CreateStreamingClient();
                        PriceListener = StreamingClient.BuildPricesListener(MarketId);
                        PriceListener.MessageReceived += OnMessageReceived;

                        Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting account info"));
                        RpcClient.AccountInformation.BeginGetClientAndTradingAccount(ar2 =>
                        {
                            Account = RpcClient.AccountInformation.EndGetClientAndTradingAccount(ar2);

                            Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting market info"));
                            RpcClient.Market.BeginGetMarketInformation(MarketId.ToString(), ar3 =>
                            {
                                Market = RpcClient.Market.EndGetMarketInformation(ar3).MarketInformation;
                                Dispatcher.BeginInvoke(() => Button1.IsEnabled = true);
                            }, null);
                        }, null);
                    });
                }
                catch (Exception ex)
                {
                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("exception caught: " + ex));
                }
            }, null);
        }
        /// <exception cref="OverflowException">The dictionary already contains the maximum number of elements, <see cref="F:System.Int32.MaxValue" />.</exception>
        /// <exception cref="NotSupportedException">Condition. </exception>
        public void HandleRequest(Request request)
        {
            switch (request.RequestType)
            {
            case "NEWS":
                if (!String.IsNullOrEmpty(request.Parameters))
                {
                    var listenerNewsHeadlines = _streamingClient.BuildNewsHeadlinesListener(request.UserName, request.SessionToken, request.Parameters);
                    var newsEventHandler      = new NewsEventHandler(listenerNewsHeadlines);

                    if (!_subscriptions.TryAdd(request, newsEventHandler))
                    {
                        _logger.Error("Can't subscribe to NEWS datafeed");
                    }
                }

                _logger.Error("Can't subscribe to NEWS datafeed. News category wasn't provided");
                break;

            case "PRICES":
                var splitted = request.Parameters.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                if (splitted.Any())
                {
                    var listenerPrices    = _streamingClient.BuildPricesListener(request.UserName, request.SessionToken, splitted);
                    var priceEventHandler = new PriceEventHandler(listenerPrices);

                    if (!_subscriptions.TryAdd(request, priceEventHandler))
                    {
                        _logger.Error("Can't subscribe to PRICES datafeed");
                    }
                }

                _logger.Error("Can't subscribe to PRICES datafeed. Market ids weren't provided.");
                break;

            case "QUOTES":
                var listenerQuotes    = _streamingClient.BuildQuotesListener(request.UserName, request.SessionToken);
                var quoteEventHandler = new QuoteEventHandler(listenerQuotes);

                if (!_subscriptions.TryAdd(request, quoteEventHandler))
                {
                    _logger.Error("Can't subscribe to QUOTES datafeed");
                }
                break;

            case "CLIENTACCOUNTMARGIN":
                var listenerClientAccountMargin     = _streamingClient.BuildClientAccountMarginListener(request.UserName, request.SessionToken);
                var clientAccountMarginEventHandler = new ClientAccountMarginEventHandler(listenerClientAccountMargin);

                if (!_subscriptions.TryAdd(request, clientAccountMarginEventHandler))
                {
                    _logger.Error("Can't subscribe to CLIENTACCOUNTMARGIN datafeed");
                }
                break;

            case "ORDERS":
                var listenerOrders    = _streamingClient.BuildOrdersListener(request.UserName, request.SessionToken);
                var orderEventHandler = new OrderEventHandler(listenerOrders);

                if (!_subscriptions.TryAdd(request, orderEventHandler))
                {
                    _logger.Error("Can't subscribe to ORDERS datafeed");
                }
                break;

            case "DEFAULTPRICES":
                if (!String.IsNullOrEmpty(request.Parameters))
                {
                    int accountOperatorId;
                    Int32.TryParse(request.Parameters, out accountOperatorId);

                    if (accountOperatorId != 0)
                    {
                        var listenerDefaultPrices = _streamingClient.BuildDefaultPricesListener(request.UserName, request.SessionToken, accountOperatorId);
                        var priceEventHandler     = new PriceEventHandler(listenerDefaultPrices);

                        if (!_subscriptions.TryAdd(request, priceEventHandler))
                        {
                            _logger.Error("Can't subscribe to DEFAULTPRICES datafeed");
                        }
                    }
                    _logger.Error("Can't subscribe to DEFAULTPRICES datafeed. Can't parse account operator Id.");
                }
                else
                {
                    _logger.Error("Can't subscribe to DEFAULTPRICES datafeed. Account operator Id wasn't provided.");
                }
                break;

            case "TRADEMARGIN":
                var listenerTradeMargin     = _streamingClient.BuildTradeMarginListener(request.UserName, request.SessionToken);
                var tradeMarginEventHandler = new TradeMarginEventHandler(listenerTradeMargin);

                if (!_subscriptions.TryAdd(request, tradeMarginEventHandler))
                {
                    _logger.Error("Can't subscribe to TRADEMARGIN datafeed");
                }
                break;

            case "TRADINGACCOUNTMARGIN":
                var listenerTradingAccountMargin     = _streamingClient.BuildTradingAccountMarginListener(request.UserName, request.SessionToken);
                var tradingAccountMarginEventHandler = new TradingAccountMarginEventHandler(listenerTradingAccountMargin);

                if (!_subscriptions.TryAdd(request, tradingAccountMarginEventHandler))
                {
                    _logger.Error("Can't subscribe to TRADINGACCOUNTMARGIN datafeed");
                }
                break;

            default:
                throw new NotSupportedException();
            }
        }