private List <StockListObjectData> GetStockPrices(ConfigSettingsObject settings) { List <StockListObjectData> toReturn = new List <StockListObjectData>(); // To array creates a copy of the list, so that alteration withing the UI // doesn't cause forloop iteration problems whilst altering foreach (var stock in settings.watchedStock.ToArray()) { try { StockListObjectData newData = IEXHelper.GetStockData(stock, settings.iexCloudApiKey); if (settings.onlyPopupDuringTradingHours == true) { if (newData.StockStockMarketOpen == true) { toReturn.Add(newData); } } else { toReturn.Add(newData); } } catch (Exception ex) { if (ex.Message.StartsWith("Invalid status response:")) { //Invalid symbol likely Debug.WriteLine(stock + ":" + ex.Message); } else { throw ex; } } } return(toReturn); }
/// <summary> /// returns a percentage ( as Decimal ) for a stock ticker /// </summary> /// <param name="symbol"></param> /// <returns>StockListObject</returns> public static StockListObjectData GetStockData(string symbol, string apiKey) { StockListObjectData toReturn = new StockListObjectData(); toReturn.PercentageChange = 0; toReturn.StockName = symbol; toReturn.StockStockMarketOpen = false; // Build query to call against API: string paramaters = $"stock/{symbol}/quote?token={apiKey}&displayPercent=true"; string objectAsString = APICall(paramaters); var data = (JObject)JsonConvert.DeserializeObject(objectAsString); if (data["changePercent"] != null && data["changePercent"].Type != JTokenType.Null) { toReturn.PercentageChange = data["changePercent"].Value <Decimal>(); } // Close by default. string closedValueString = "close"; if (data["latestSource"] != null && data["latestSource"].Type != JTokenType.Null) { closedValueString = data["latestSource"].Value <String>(); } if (closedValueString.ToLower().Contains("close")) { toReturn.StockStockMarketOpen = false; } else { toReturn.StockStockMarketOpen = true; } return(toReturn); }