Exemple #1
0
        public static void GetStockQuoteAsync(string symbol)
        {
            Bloomberg.StockQuote result = null;
            var sw = new Stopwatch();
            sw.Start();

            try
            {
                var quote = new StockQuoteServiceClient();
                quote.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(20);
                result = quote.GetStockQuote(symbol.Trim());
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            finally
            {
                sw.Stop ();
                var last = 0.0;
                var qresult = double.TryParse (result.Last, out last);
                var qlast = qresult ? last.ToString ("C") : "N/A";
                Console.WriteLine ("{0, -40} | Symbol [{1, -6}] | Last [{2, -10}] | Elapsed time: [{3}ms]",
                    string.IsNullOrEmpty (result.Name) ? "Market Quote " : result.Name, result.Symbol, qlast, sw.ElapsedMilliseconds);
            }
        }
Exemple #2
0
        public StockQuote GetStockQuote(string symbol, int timeoutInMinutes = 3)
        {
            StockQuote quote = null;
            var sw = new Stopwatch();

            var quotesvs = new StockQuoteServiceClient();
            quotesvs.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(timeoutInMinutes);

            sw.Start();
            quote = quotesvs.GetStockQuote(symbol);
            sw.Stop();

            if(null != quote)
                quote.ElapsedMilliseconds = sw.ElapsedMilliseconds.ToString();

            return quote;
        }
        private async void DoIt()
        {
            var quotes = new List<StockQuote>();
            var reuters = new StockQuoteServiceClient();
            reuters.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(20);
            reuters.Open();                        

            using (var db = new StockQuotesDbContext())
            {
                var r = Parallel.ForEach(SymbolLibrary.StockSymbols, (x)=> {

                    var quote = reuters.GetStockQuote(x);
                    if (quote.Name != "N/A")
                        quotes.Add(quote);
                });
                //foreach (var symbol in SymbolLibrary.StockSymbols)
                //{
                //    var quote = await reuters.GetStockQuoteAsync(symbol);
                //    if (quote.Name == "N/A")
                //        continue;
                //    quotes.Add(quote);
                //}
                while (!r.IsCompleted) ;

                db.StockQuotes.AddRange(quotes);                
                await db.SaveChangesAsync();

                var query = from b in db.StockQuotes
                            orderby b.Name
                            select b;
                if (query.Any())
                    Tag = true;
            }

            reuters.Close();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            bool useInput = true;
            if (args != null && args.Length > 0 && string.Compare(args[0], "test") == 0)
            {
                useInput = false;
            }

            StockQuoteServiceClient client = new StockQuoteServiceClient();

            Console.WriteLine("Press Enter to start sending requests");
            if (useInput)
            {
                Console.ReadLine();
            }

            DailyStockQuote quote;
            Random rand = new Random();
            Stopwatch sw = new Stopwatch();

            while(!Console.KeyAvailable)
            {
                int action = rand.Next() % 4;

                switch (action)
                {
                    case 0:
                        Console.WriteLine("Getting quote for MSFT.");
                        sw.Reset();
                        sw.Start();
                        quote = client.GetQuote("msft");
                        sw.Stop();
                        PrintQuote(quote);
                        break;
                    case 1:
                        Console.WriteLine("Getting quote for AAPL");
                        sw.Reset();
                        sw.Start();
                        quote = client.GetQuote("aapl");
                        sw.Stop();
                        PrintQuote(quote);
                        break;
                    case 2:
                        Console.WriteLine("Buying MSFT...");
                        sw.Reset();
                        sw.Start();
                        client.PurchaseStock("msft", 100);
                        sw.Stop();
                        break;
                    case 3:
                        Console.WriteLine("Buying AAPL...");
                        sw.Reset();
                        sw.Start();
                        client.PurchaseStock("aapl", 15);
                        sw.Stop();
                        break;
                }

                Console.WriteLine("{0}: Done. Duration = {1}", DateTime.Now.ToLongTimeString(), sw.Elapsed);
                Console.WriteLine("-----------------------------------------------------");
            }

            // Always close the client.
            client.Close();

            Console.WriteLine();
            if (useInput)
            {
                Console.WriteLine("Press Enter to exit.");
                Console.ReadLine();
            }
            else
            {
                Thread.Sleep(90000);
            }
        }