Exemplo n.º 1
0
        public StockOper(double price, int count, OperType type)
        {
            MarketStock stock = new MarketStock();
            stock.UnitPrice = price;
            stock.Count = count;

            TargetStock = stock;
            operType = type;
        }
Exemplo n.º 2
0
        public async Task <List <MarketStock> > SearchMarket(String search)
        {
            List <string>      symbols      = new List <string>();
            int                resultsCount = 0;
            int                calls        = 1;
            List <MarketStock> results      = new List <MarketStock>();

            //Check for a blank search, return null if so
            if (search == "")
            {
                return(null);
            }

            //Get the entire list of stock symbols and only grab the first 100 that match
            var referenceData = await httpClient.GetStringAsync("ref-data/symbols");

            JArray refer = JArray.Parse(referenceData);

            //Capitalize search string
            search = search.ToUpper();

            //Iterate through the list searching for stocks that start with the search string
            //Skip anything with a plus symbol, it causes issues with batch requests
            foreach (JObject stock in refer)
            {
                string str = (string)stock["symbol"];
                if (str.Contains("+") || (string)stock["type"] == "crypto")
                {
                    continue;
                }
                if (str.StartsWith(search))
                {
                    symbols.Add(str);
                    resultsCount++;
                }
            }
            //If no results, then return nothing
            if (symbols.Count() == 0)
            {
                return(null);
            }
            //If results are more than 100, split up the API calls
            calls += (resultsCount - 1) / 100;
            //Get the information needed and trim it for the front end for each call
            for (int i = 0; i < calls; i++)
            {
                int max = 100;
                //If it's the last call, limit the max to the end of the list
                if (i + 1 == calls)
                {
                    max = symbols.Count() % 100;
                }
                List <string> callSymbols  = symbols.GetRange(i * 100, max);
                var           responseBody = await httpClient.GetStringAsync("stock/market/batch?symbols=" + string.Join(",", callSymbols) +
                                                                             "&types=price,logo,previous");

                var market = JObject.Parse(responseBody);
                //Add each stock to the results list
                for (int j = 0; j < max; j++)
                {
                    MarketStock stock = new MarketStock();
                    stock.symbol = symbols.ElementAt(i * 100 + j);
                    //Error check for a missing price or changePercent
                    if ((string)market[stock.symbol]["price"] == null || (string)market[stock.symbol]["previous"]["changePercent"] == null)
                    {
                        continue;
                    }
                    stock.price         = (decimal)market[stock.symbol]["price"];
                    stock.logo          = (string)market[stock.symbol]["logo"]["url"];
                    stock.changePercent = (decimal)market[stock.symbol]["previous"]["changePercent"];
                    results.Add(stock);
                }
            }
            return(results);
        }
Exemplo n.º 3
0
        public async Task <MarketBatch> FetchMarket(String sort)
        {
            MarketBatch marketBatch = new MarketBatch();
            int         calls       = 1;
            //Get the entire list of stock symbols and only grab the first 100

            var referenceData = await httpClient.GetStringAsync("ref-data/symbols");

            JArray        results = JArray.Parse(referenceData);
            List <String> symbols = new List <String>();

            //Sort method
            if (sort == "asc")
            {
                for (int i = 0; i < 500; i++)
                {
                    //Plus Symbols cause errors in URL's
                    string str = (string)results.ElementAt(i)["symbol"];
                    if (str.Contains("+"))
                    {
                        continue;
                    }
                    symbols.Add(str);
                }
            }
            else
            {
                int index = 0;
                int start = results.Count() - 1;
                //Skip crypto at the end
                while ((string)results.ElementAt(start)["type"] == "crypto")
                {
                    start--;
                }
                for (int i = start; i > (start - 500); i--)
                {
                    //Plus Symbols cause errors in URL's
                    string str = (string)results.ElementAt(i)["symbol"];
                    if (str.Contains("+"))
                    {
                        continue;
                    }
                    symbols.Add(str);
                    index++;
                }
            }

            //Split up symbols to separate API calls
            calls += symbols.Count() / 100;
            for (int i = 0; i < calls; i++)
            {
                int max = 100;
                //Limit the max if it's the final call since it may be below 100
                if (i + 1 == calls)
                {
                    max = symbols.Count() % 100;
                }
                List <String> callSymbols  = symbols.GetRange(i * 100, max);
                var           responseBody = await httpClient.GetStringAsync("stock/market/batch?symbols=" + string.Join(",", callSymbols) +
                                                                             "&types=price,logo,previous");

                var market = JObject.Parse(responseBody);
                //Get only the data needed for each stock
                foreach (string symbol in callSymbols)
                {
                    MarketStock x = new MarketStock();
                    x.symbol = symbol;
                    x.logo   = (string)market[x.symbol]["logo"]["url"];
                    //Some stocks may have null price or changePercent
                    if ((string)market[x.symbol]["price"] == null || (string)market[x.symbol]["previous"]["changePercent"] == null)
                    {
                        continue;
                    }
                    else
                    {
                        x.price         = (decimal)market[x.symbol]["price"];
                        x.changePercent = (decimal)market[x.symbol]["previous"]["changePercent"];
                    }
                    marketBatch.stocks.Add(x);
                }
            }

            //Get today's biggest gainers and losers
            var response = await httpClient.GetStringAsync("stock/market/list/gainers");

            var gainers = JArray.Parse(response);

            foreach (JToken stock in gainers)
            {
                MarketStock x = new MarketStock();
                x.symbol        = (string)stock["symbol"];
                x.price         = (decimal)stock["latestPrice"];
                x.changePercent = (decimal)stock["changePercent"];
                marketBatch.gainers.Add(x);
            }
            response = await httpClient.GetStringAsync("stock/market/list/losers");

            var losers = JArray.Parse(response);

            foreach (JToken stock in losers)
            {
                MarketStock x = new MarketStock();
                x.symbol        = (string)stock["symbol"];
                x.price         = (decimal)stock["latestPrice"];
                x.changePercent = (decimal)stock["changePercent"];
                marketBatch.losers.Add(x);
            }

            //Get the logo for gainers and losers
            List <string> list = new List <string>();

            foreach (MarketStock stock in marketBatch.gainers)
            {
                list.Add(stock.symbol);
            }
            foreach (MarketStock stock in marketBatch.losers)
            {
                list.Add(stock.symbol);
            }
            response = await httpClient.GetStringAsync("stock/market/batch?symbols=" + string.Join(",", list) +
                                                       "&types=logo");

            var logos = JObject.Parse(response);

            for (int i = 0; i < gainers.Count(); i++)
            {
                marketBatch.gainers.ElementAt(i).logo = (string)logos[list.ElementAt(i)]["logo"]["url"];
            }
            for (int i = 0; i < losers.Count(); i++)
            {
                marketBatch.losers.ElementAt(i).logo = (string)logos[list.ElementAt(i + gainers.Count())]["logo"]["url"];
            }

            return(marketBatch);
        }